Java教程

java连接mysql

本文主要是介绍java连接mysql,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

package com.kuang.test;
​
import com.mysql.cj.jdbc.Driver;
​
import java.sql.*;
​
public class TestJdbc {
    public static void main(String[] args) throws ClassNotFoundException, SQLException {
        //配置信息
        //useUnicode=true&characterEncoding=UTF-8 解决中文乱码
        String url = "jdbc:mysql://localhost:3306/jdbc?serverTimezone=GMT%2B8";
        String username = "root";
        String password = "123456";
        //1.加载驱动
        Class.forName("com.mysql.cj.jdbc.Driver");
        //2.连接数据库
        Connection connection = DriverManager.getConnection(url, username, password);
        //向数据库发送SQL的对象Statement:CRUD
        Statement statement = connection.createStatement();
        //4编写SQL
        String sql = "select * from user";
        //5执行查询SQL
        ResultSet rs = statement.executeQuery(sql);
        while (rs.next()){
            System.out.println("id="+rs.getObject("id"));
            System.out.println("name="+rs.getObject("name"));
            System.out.println("password="+rs.getObject("password"));
            System.out.println("email="+rs.getObject("password"));
            System.out.println("birthday="+rs.getObject("birthday"));
        }
        //关闭连接,释放资源(一定要关)先开后关
        rs.close();
        statement.close();
        connection.close();
​
​
​
    }
}
​

结果:不明白为什么报错,但是结果输出了,网上搜到的方法试了试也没解决(网上说的是mysql版本太高)

E:\jdk-15.0.1\jdk-15.0.1\bin\java.exe "-javaagent:F:\ideaIU\IntelliJ IDEA 2021.3.1\lib\idea_rt.jar=50985:F:\ideaIU\IntelliJ IDEA 2021.3.1\bin" -Dfile.encoding=UTF-8 -classpath F:\ideaIU\java-jdbc\target\classes;D:\environment\apache-maven-3.8.4-bin\apache-maven-3.8.4\maven-repo\mysql\mysql-connector-java\8.0.11\mysql-connector-java-8.0.11.jar;D:\environment\apache-maven-3.8.4-bin\apache-maven-3.8.4\maven-repo\com\google\protobuf\protobuf-java\2.6.0\protobuf-java-2.6.0.jar com.kuang.test.TestJdbc
Wed Jan 26 18:43:23 CST 2022 WARN: Establishing SSL connection without server's identity verification is not recommended. According to MySQL 5.5.45+, 5.6.26+ and 5.7.6+ requirements SSL connection must be established by default if explicit option isn't set. For compliance with existing applications not using SSL the verifyServerCertificate property is set to 'false'. You need either to explicitly disable SSL by setting useSSL=false, or set useSSL=true and provide truststore for server certificate verification.
id=1
name=张三
password=123456
email=123456
birthday=2000-01-01
id=2
name=李四
password=123456
email=123456
birthday=2000-01-01
id=3
name=王五
password=123456
email=123456
birthday=2000-01-01
Wed Jan 26 18:43:24 CST 2022 WARN: Caught while disconnecting...
​
EXCEPTION STACK TRACE:
​
​
​
** BEGIN NESTED EXCEPTION ** 
​
javax.net.ssl.SSLException
MESSAGE: closing inbound before receiving peer's close_notify
​
STACKTRACE:
​
javax.net.ssl.SSLException: closing inbound before receiving peer's close_notify
    at java.base/sun.security.ssl.Alert.createSSLException(Alert.java:133)
    at java.base/sun.security.ssl.Alert.createSSLException(Alert.java:117)
    at java.base/sun.security.ssl.TransportContext.fatal(TransportContext.java:356)
    at java.base/sun.security.ssl.TransportContext.fatal(TransportContext.java:312)
    at java.base/sun.security.ssl.TransportContext.fatal(TransportContext.java:303)
    at java.base/sun.security.ssl.SSLSocketImpl.shutdownInput(SSLSocketImpl.java:817)
    at java.base/sun.security.ssl.SSLSocketImpl.shutdownInput(SSLSocketImpl.java:796)
    at com.mysql.cj.protocol.a.NativeProtocol.quit(NativeProtocol.java:1284)
    at com.mysql.cj.NativeSession.quit(NativeSession.java:182)
    at com.mysql.cj.jdbc.ConnectionImpl.realClose(ConnectionImpl.java:1911)
    at com.mysql.cj.jdbc.ConnectionImpl.close(ConnectionImpl.java:730)
    at com.kuang.test.TestJdbc.main(TestJdbc.java:34)
​
​
** END NESTED EXCEPTION **
​
​
​
Process finished with exit code 0
​

这篇关于java连接mysql的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!