用java連接資料庫,代碼如下
import java.sql.*;
public class mysqltest {
static final String JDBC_DRIVER = "com.mysql.cj.jdbc.Driver";
static final String newdb_URL="jdbc:mysql://localhost:3306/newdb?serverTimezone=UTC&useUnicode=true&characterEncoding=utf8&useSSL=false";
static final String USER="root";
static final String PASS="root";
public static void main(String args[])
{
Connection conn=null;
Statement stmt=null;
try{
Class.forName(JDBC_DRIVER);
System.out.println("連接資料庫");
conn=DriverManager.getConnection(newdb_URL,USER,PASS);
System.out.println("實體化Statement物件");
stmt=conn.createStatement();
String sql;
sql="SELECT 名,型別 ,長度";
ResultSet rs=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(SQLException se){
se.printStackTrace();
}catch(Exception e){
e.printStackTrace();
}finally{
try{
if(stmt!=null) stmt.close();
}catch(SQLException se2){
}
try{
if(conn!=null)conn.close();
}catch(SQLException se){
se.printStackTrace();
}
}
System.out.println("goodbye");
}
}
運行后出現報錯
java.sql.SQLException: The server time zone value '?й???????' is unrecognized or represents more than one time zone. You must configure either the server or JDBC driver (via the serverTimezone configuration property) to use a more specifc time zone value if you want to utilize time zone support.
at com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:129)
at com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:97)
at com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:89)
at com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:63)
at com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:73)
at com.mysql.cj.jdbc.exceptions.SQLExceptionsMapping.translateException(SQLExceptionsMapping.java:76)
at com.mysql.cj.jdbc.ConnectionImpl.createNewIO(ConnectionImpl.java:835)
at com.mysql.cj.jdbc.ConnectionImpl.<init>(ConnectionImpl.java:455)
at com.mysql.cj.jdbc.ConnectionImpl.getInstance(ConnectionImpl.java:240)
at com.mysql.cj.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:199)
at java.sql.DriverManager.getConnection(Unknown Source)
at java.sql.DriverManager.getConnection(Unknown Source)
at mysqltest.main(mysqltest.java:14)
uj5u.com熱心網友回復:
sql有問題,元資料和欄位名最好用英文。查詢sql陳述句:select * from 表名稱https://blog.csdn.net/songleong/article/details/105968830
uj5u.com熱心網友回復:
time zone,檢查一下資料庫的 time zone 設定 和 連接串的 time zone是否一致。連接串的是 serverTimezone=UTC , 資料庫的自己查組態檔
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/71438.html
標籤:Java相關
下一篇:嚴重: Servlet.service() for servlet jsp threw exception java.lang.NullPointerExcep
