用Java连接数据库出现错误,求指点

[复制链接]
查看11 | 回复2 | 2021-1-27 05:42:41 | 显示全部楼层 |阅读模式
用java连接数据库,代码如下
importjava.sql.*;
publicclassmysqltest{
staticfinalStringJDBC_DRIVER="com.mysql.cj.jdbc.Driver";
staticfinalStringnewdb_URL="jdbc:mysql://localhost:3306/newdb?serverTimezone=UTC&useUnicode=true&characterEncoding=utf8&useSSL=false";
staticfinalStringUSER="root";
staticfinalStringPASS="root";
publicstaticvoidmain(Stringargs[])
{
Connectionconn=null;
Statementstmt=null;
try{
Class.forName(JDBC_DRIVER);
System.out.println("连接数据库");
conn=DriverManager.getConnection(newdb_URL,USER,PASS);
System.out.println("实例化Statement对象");
stmt=conn.createStatement();
Stringsql;
sql="SELECT名,类型,长度";
ResultSetrs=stmt.executeQuery(sql);
while(rs.next()){
String名=rs.getString("名");
String类型=rs.getString("类型");
int长度=rs.getInt("长度");
System.out.print("名"+名);
System.out.print("类型"+类型);
System.out.print("长度"+长度);
System.out.print("\n");
}
rs.close();
stmt.close();
conn.close();
}catch(SQLExceptionse){
se.printStackTrace();
}catch(Exceptione){
e.printStackTrace();
}finally{
try{
if(stmt!=null)stmt.close();
}catch(SQLExceptionse2){
}
try{
if(conn!=null)conn.close();
}catch(SQLExceptionse){
se.printStackTrace();
}
}
System.out.println("goodbye");
}
}
运行后出现报错
java.sql.SQLException:Theservertimezonevalue'?й???????'isunrecognizedorrepresentsmorethanonetimezone.YoumustconfigureeithertheserverorJDBCdriver(viatheserverTimezoneconfigurationproperty)touseamorespecifctimezonevalueifyouwanttoutilizetimezonesupport.
atcom.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:129)
atcom.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:97)
atcom.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:89)
atcom.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:63)
atcom.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:73)
atcom.mysql.cj.jdbc.exceptions.SQLExceptionsMapping.translateException(SQLExceptionsMapping.java:76)
atcom.mysql.cj.jdbc.ConnectionImpl.createNewIO(ConnectionImpl.java:835)
atcom.mysql.cj.jdbc.ConnectionImpl.(ConnectionImpl.java:455)
atcom.mysql.cj.jdbc.ConnectionImpl.getInstance(ConnectionImpl.java:240)
atcom.mysql.cj.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:199)
atjava.sql.DriverManager.getConnection(UnknownSource)
atjava.sql.DriverManager.getConnection(UnknownSource)
atmysqltest.main(mysqltest.java:14)





分 -->
回复

使用道具 举报

千问 | 2021-1-27 05:42:41 | 显示全部楼层
sql有问题,元数据和字段名最好用英文。查询sql语句:select*from表名称
https://blog.csdn.net/songleong/article/details/105968830
回复

使用道具 举报

千问 | 2021-1-27 05:42:41 | 显示全部楼层
timezone,检查一下数据库的timezone设置和连接串的timezone是否一致。
连接串的是serverTimezone=UTC,数据库的自己查配置文件
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

主题

0

回帖

4882万

积分

论坛元老

Rank: 8Rank: 8

积分
48824836
热门排行