Home > Back-end >  Using Java to connect to the database error, please advice
Using Java to connect to the database error, please advice

Time:09-25

Using Java to connect to the database, the code below
Import the Java. SQL. *;
Public class mysqltest {
The static final String JDBC_DRIVER=". Com. Mysql. Cj. JDBC Driver ".
The static final String newdb_URL="JDBC: mysql://localhost: 3306/newdb? ServerTimezone=UTC& UseUnicode=true& CharacterEncoding=utf8 & amp; UseSSL=false ";
The static final String USER="root";
The static final String PASS="root";
Public static void main (String args [])
{
Connection conn=null;
The Statement STMT=null;
Try {
Class.forname (JDBC_DRIVER);
System. The out. Println (" connect to the database ");
Conn=DriverManager. GetConnection (newdb_URL, USER, PASS);
System. The out. Println (" instantiation Statement object ");
stmt=conn.createStatement();
String sql;
SQL="SELECT name, type, length".
The ResultSet rs=STMT. ExecuteQuery (SQL);
While (rs), next ()) {
String name=rs. Get String (" ");
Type String=rs. Get String (" type ");
Int length=rs. Get int (" length ");
System. The out. Print (" name "+ name);
System. The out. Print (" type "+ type);
System. The out. Print (" length "+ length);
System. The out. Print (" \ n ");
}
rs.close();
STMT. Close ();
conn.close();
{} the catch (SQLException se)
Se. PrintStackTrace ();
} the catch (Exception e) {
e.printStackTrace();
} the finally {
Try {
If (STMT!=null) STMT. Close ();
{} the catch (SQLException se2)
}
Try {
if(conn!=null) conn. Close ();
{} the catch (SQLException se)
Se. PrintStackTrace ();
}
}
System. The out. Println (" goodbye ");
}
}
After running error
Java. SQL. SQLException: The server time zone value '? й??????????? 'is unrecognized or represents more than one time zone, You must configure either the server or the JDBC driver (via the serverTimezone configuration property) to use a more specifc time zone value if You want to utilize the 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. & lt; init> (ConnectionImpl. Java: 455)
At com. Mysql. Cj. JDBC. ConnectionImpl. GetInstance (ConnectionImpl. Java: 240)
At com. Mysql. Cj. JDBC. NonRegisteringDriver. Connect (NonRegisteringDriver. Java: 199)
The at Java. SQL. DriverManager. GetConnection (Unknown Source)
The at Java. SQL. DriverManager. GetConnection (Unknown Source)
The at mysqltest. Main (mysqltest. Java: 14)








CodePudding user response:

SQL has a problem, metadata, and the field name had better use English, the query SQL statement: select * from table name
https://blog.csdn.net/songleong/article/details/105968830

CodePudding user response:

Time zone, check the database time zone Settings and connection string time zones are consistent,
Database connection string is serverTimezone=UTC, check the configuration file
  • Related