java连接MySQL出现版本错误及连接数据库代码
2018-12-17 18:31
656 查看
今天eclipse连接mysql遇到不少问题。
我用的环境如下(错误的情况下):
eclipse 4.7;
jdk1.7(Java project);
mysql5.6;
mysql-connector-java-8.0.12.jar(需要导入);
部分连接语句(错误的部分):
[code] String driver = "com.mysql.jdbc.Driver"; String url = "jdbc:mysql://localhost:3306/test ";
1 版本错误:Unsupported major.minor version 52.0
JDK版本在官方对应的版本号列表如下(引用https://www.geek-share.com/detail/2687446400.html):
此错误将
[code]JDK 8.0 = 52, JDK 7.0 = 51, JDK 6.0 = 50, JDK 5.0 = 49, JDK 1.4 = 48, JDK 1.3 = 47, JDK 1.2 = 46, JDK 1.1 = 45
将jdk 修改成1.8后这个错误消失
2 连接驱动driver错误
错误提示:
[code]Loading class `com.mysql.jdbc.Driver'. This is deprecated. The new driver class is `com.mysql.cj.jdbc.Driver'. The driver is automatically registered via the SPI and manual loading of the driver class is generally unnecessary.
加入‘cj.’修改为此错误消失:
[code] String driver = "com.mysql.cj.jdbc.Driver";
3 时区没有设置
错误提示:
[code]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:832) at com.mysql.cj.jdbc.ConnectionImpl.<init>(ConnectionImpl.java:456) at com.mysql.cj.jdbc.ConnectionImpl.getInstance(ConnectionImpl.java:240) at com.mysql.cj.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:207) at java.sql.DriverManager.getConnection(DriverManager.java:664) at java.sql.DriverManager.getConnection(DriverManager.java:247) at mysqljdbc1217.main.main(main.java:29) Caused by: com.mysql.cj.exceptions.InvalidConnectionAttributeException: 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 sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method) at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62) at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45) at java.lang.reflect.Constructor.newInstance(Constructor.java:423) at com.mysql.cj.exceptions.ExceptionFactory.createException(ExceptionFactory.java:61) at com.mysql.cj.exceptions.ExceptionFactory.createException(ExceptionFactory.java:85) at com.mysql.cj.util.TimeUtil.getCanonicalTimezone(TimeUtil.java:128) at com.mysql.cj.protocol.a.NativeProtocol.configureTimezone(NativeProtocol.java:2236) at com.mysql.cj.protocol.a.NativeProtocol.initServerSession(NativeProtocol.java:2260) at com.mysql.cj.jdbc.ConnectionImpl.initializePropsFromServer(ConnectionImpl.java:1314) at com.mysql.cj.jdbc.ConnectionImpl.connectOneTryOnly(ConnectionImpl.java:963) at com.mysql.cj.jdbc.ConnectionImpl.createNewIO(ConnectionImpl.java:822) ... 6 more
修改 :
[code]String url = "jdbc:mysql://localhost:3306/test "+ "?serverTimezone=GMT%2B8";
错误全部消失。
全部代码如下:
[code]package mysqljdbc1217; import java.sql.Connection; import java.sql.DriverManager; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement; public class main { public static void main(String[] args) { //声明Connection对象 Connection con; //驱动程序名 String driver = "com.mysql.cj.jdbc.Driver"; //URL指向要访问的数据库名mydata String url = "jdbc:mysql://localhost:3306/test "+ "?serverTimezone=GMT%2B8"; //MySQL配置时的用户名 String user = "root"; //MySQL配置时的密码 String password = "11111"; //遍历查询结果集 try { //加载驱动程序 System.out.println("执行结果如下所示:"); Class.forName(driver); System.out.println("执行结果如下所示:"); //1.getConnection()方法,连接MySQL数据库!! con = DriverManager.getConnection(url,user,password); if(!con.isClosed()) System.out.println("Succeeded connecting to the Database!"); //2.创建statement类对象,用来执行SQL语句!! Statement statement = con.createStatement(); //要执行的SQL语句 String sql = "select * from temporpary_data"; //3.ResultSet类,用来存放获取的结果集!! ResultSet rs = statement.executeQuery(sql); System.out.println("-----------------"); System.out.println("执行结果如下所示:"); System.out.println("-----------------"); System.out.println("姓名" + "\t" + "职称"); System.out.println("-----------------"); String job = null; String id = null; /* while(rs.next()){ //获取stuname这列数据 job = rs.getString("job"); //获取stuid这列数据 id = rs.getString("ename"); //输出结果 System.out.println(id + "\t" + job); } rs.close(); con.close();*/ } catch(ClassNotFoundException e) { //数据库驱动类异常处理 System.out.println("Sorry,can`t find the Driver!"); e.printStackTrace(); } catch(SQLException e) { //数据库连接失败异常处理 e.printStackTrace(); }catch (Exception e) { // TODO: handle exception e.printStackTrace(); }finally{ System.out.println("数据库数据成功获取!!"); } } }
相关文章推荐
- PHPnow更新PHP版本后连接数据库错误(mysql_connect报错)
- java连接Oracle、Mysql、DB2、Sybase、Informix、PostgreSQL数据库代码
- javaweb JDBC 数据库连接mysql 配置代码 (直接可用)
- java与 mysql6.0版本连接代码
- MySql系列:中文写入数据库出现错误java.sql.SQLException: Incorrect string value: '\xE5\xxxx' for column 'xxxx' at r
- 网站出现数据库连接失败,mysql 2003错误(10061) 的解决方法
- Java程序连接数据库(mysql,sql server)以及出现的问题解决
- MySQL连接数据库报时区错误:java.sql.SQLException: The server time zone value
- 连接mysql-front数据库出现‘执行错误1251’的解决办法(有效)
- java 连接数据库时 出现错误;
- mysql4.1以上版本连接时出现Client does not support authentication protocol #1251错误问题解决办法
- org.gjt.mm.mysql.Driver意思和 Java连接数据库代码
- 连接数据库 出现1130错误 Navicat for MySQL(phpStudy环境下)
- java 连接 mysql 数据库代码
- 解决远程连接mysql错误1130 远程连接Mysql服务器的数据库,错误代码是1130,ERROR 1130: Host xxx.xxx.xxx.xxx is not allowed to co
- 【mysql】用navicat连接虚拟机mysql出现错误代码(10038)
- 利物浦 代写程序 代写代码 java 连接 mysql 数据库步骤
- Eclipse 连接 SQL Server 2008数据库,运行JAVA代码时出现:‘sa’用户登录失败
- Java连接mysql时出现错误:java.la…
- 关于c3p0连接数据库时出现的错误:Caused by: java.lang.NoClassDefFoundError: com/mchange/v2/ser/Indirector解决方案