java 连 mysql + 查询数据表

摘要:java 连 mysql + 查询数据表


eclipse的lib放入mysql-connector-java-5.1.29-bin.jar

public static void main(String[] args) {
Connection conn = null;

try {
Class.forName("com.mysql.jdbc.Driver").newInstance();
conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=Big5", 
"root","admin");
System.out.println("Connected to the database");

Statement stmt = conn.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);
String sql = "select * from sakila.actor";
ResultSet rs = stmt.executeQuery(sql);
while(rs.next()) {
System.out.print("actor_id = "+ rs.getString("actor_id"));
System.out.print(" ; first_name = "+ rs.getString("first_name"));
System.out.println(" ; last_name = "+ rs.getString("last_name"));
}

conn.close();
System.out.println("Disconnected from database");
} catch (Exception e) {
e.printStackTrace();
}

}

原文链接 大专栏  https://www.dazhuanlan.com/2019/07/21/java-%e8%bf%9e-mysql-%e6%9f%a5%e8%af%a2%e6%95%b0%e6%8d%ae%e8%a1%a8/

猜你喜欢

转载自www.cnblogs.com/chinatrump/p/11416182.html