qq_34456011
qq_34456011
2016-08-11 08:10

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条回答