JAVA JDBC数据库连接报错,求大神解决
 package com.es.CH30_jdbc;
import java.sql.Connection;
import java.sql.Date;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
public class GetXt16 {
public void insert(String name, int age, String brithday, String gender,
        int math, int chinese, int english) {
    Connection connection=null;
    //Statement st=null;
    PreparedStatement pst=null;
    try {
        Class.forName("com.mysql.jdbc.Driver");
        connection = DriverManager.getConnection(
                "jdbc:mysql:///test", "root", "123456");
// st = connection.createStatement();
// String sql = "insert into xt16 values (null,'" + name + "','" + age
// + "','" + brithday + "','" + gender + "','" + math + "','"
// + chinese + "','" + english + "')";
// int i = st.executeUpdate(sql);
String sql="insert into xt16 values(?,?,?,?,?,?,?,?)";
pst=connection.prepareStatement(sql);
pst.setString(1, null);
pst.setString(2,name);
pst.setInt(3,age);
pst.setString(4, brithday);
pst.setString(5, gender);
pst.setInt(6, math);
pst.setInt(7, chinese);
pst.setInt(8, english);
        int i = pst.executeUpdate();
        if(i>0){
            System.out.println("添加成功");
        }
    } catch (ClassNotFoundException e) {
        e.printStackTrace();
    } catch (SQLException e) {
        e.printStackTrace();
    }finally{
        try {
            connection.close();
            pst.close();
            //st.close();
        } catch (SQLException e) {
            e.printStackTrace();
        }


    }

}

public void update(String name, int id){
    Connection connection = null;
    PreparedStatement pst = null;
    try {
        //加载驱动
        Class.forName("com.mysql.jdbc.Driver");
        //连接数据库
        connection = DriverManager.getConnection("jdbc:mysql:///test", "root", "123456");
        //创建prepareStatement对象,进行sql预编译
        String sql="update xt16 set name=? where id=?";
        pst = connection.prepareStatement(sql);
        pst.setString(1, name);
        pst.setInt(2, id);
        int i = pst.executeUpdate();
        if(i > 0){
            System.out.println("更新成功");
        }
    } catch (ClassNotFoundException e) {
        e.printStackTrace();
    } catch (SQLException e) {
        e.printStackTrace();
    }finally{
        try {
            pst.close();
            connection.close();
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

}

public static void main(String[] args) {
    GetXt16 gx = new GetXt16();
    //gx.xt16();
    //gx.insert("baby", 33, "1976-10-22", "女",67, 87, 56);
    gx.update("周杰伦", 2);
}
}

警告:
Thu Aug 11 16:01:55 CST 2016 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.
换成jdbc:mysql:///test?autoReconnect=true&useSSL=false"后警告没了,但是update SQL还是没被执行。但是insert语句能被执行。
JDBC和mysql都是官网的最新版本

4个回答

"jdbc:mysql:///test", "root", "123456") 为什么是3个/?

MAO_JIN_DAO
MAO_JIN_DAO 回复qq_34456011: 嗯。
3 年多之前 回复
qq_34456011
qq_34456011 test表名,root用户名,123456密码。没错的
3 年多之前 回复

根据MySQL 5.5.45 + 5.6.26 +和5.7.6 +要求必须建立SSL连接默认情况下如果不设置明确的选项。为符合现有的应用程序不使用SSL verifyServerCertificate属性设置为“false”。你需要显式禁用SSL设置useSSL = false,或一组useSSL = true,提供信任存储库服务器证书验证。

qq_34456011
qq_34456011 应该怎么改呢。网上都说改成jdbc:mysql:///test?autoReconnect=true&useSSL=false"。。我改后警告是没了,但是update和delete语句执行不了。可是inset into语句又能执行成功。
3 年多之前 回复

update和delete你这语句没问题吧,你建个新的数据库试下,是你这个数据库问题还是mysql或jdbc问题

Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问