2016-7-8 JDBC for Mysql
时间:2021-07-01 10:21:17
帮助过:2人阅读
com.mysql.cj.api.jdbc.Statement;
public class test {
public static void main(String[] args) {
try {
Class.forName("com.mysql.cj.jdbc.Driver"
).newInstance();
Connection conn =
null;
String urlString = "jdbc:mysql://localhost:3306/javademo?user=root&password=123"
+ "&characterEncoding=utf8"
+ "&useSSL=true&serverTimezone=UTC"
;
conn =
DriverManager.getConnection(urlString);
Statement sat =
(Statement) conn.createStatement();
String sql = "select * from student"
;
ResultSet result =
sat.executeQuery(sql);
int numCol =
result.getMetaData().getColumnCount();
while (result.next()) {
for (
int i = 1; i <= numCol; i++
) {
System.out.println(result.getString(i));
}
}
} catch (Exception ex) {
ex.printStackTrace();
}
}
}
更新:
1.Class.forName("com.mysql.cj.jdbc.Driver").newInstance();
2."&useSSL=true&serverTimezone=UTC" 必须滴
2016-7-8 JDBC for Mysql
标签: