JDBC连接MySQL代码段:
try {
Class.
forName(
"com.mysql.cj.jdbc.Driver")
;
//conn = DriverManager.getConnection("jdbc.mysql://192.168.122.102:3306/test?useUnicode=true&characterEncoding=UTF-8&userSSL=false&serverTimezone=Asia/Shanghai","root","test1z");
conn = DriverManager.
getConnection(
"jdbc:mysql://192.168.122.102:3306/test?useUnicode-true&characterEncoding=UTF-8&useSSL-false&serverTimezone=Asia/Shanghai&zeroDateTimeBehaviorCOVERT_TO_NULL&allowPublicKeyRetrieval-true"
,
"root"
,
"test1z")
;
Statement stmt = conn.createStatement()
;
ResultSet res = stmt.executeQuery(
"
select * from stu
")
;
while(res.next()){
int id = res.getInt(
"id")
;
String name = res.getString(
"name")
;
System.
out.println(
"id == " + id +
"," +
"name == " + name)
;
}
}
catch (SQLException ex){
ex.printStackTrace()
;
}
catch (ClassNotFoundException exx){
exx.printStackTrace()
;
}
连接字符串:
conn = DriverManager.
getConnection(
"jdbc:mysql://192.168.122.102:3306/test?useUnicode-true&characterEncoding=UTF-8&useSSL-false&serverTimezone=Asia/Shanghai&zeroDateTimeBehaviorCOVERT_TO_NULL&allowPublicKeyRetrieval-true"
,
"root"
,
"test1z")
;
来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/9606353/viewspace-2977307/
,如需转载,请注明出处,否则将追究法律责任。