qq_34456011 2016-08-11 08:10 采纳率: 0%
浏览 2780

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

  • MAO_JIN_DAO 2016-08-11 08:55
    关注

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

    评论

报告相同问题?

悬赏问题

  • ¥15 高德地图点聚合中Marker的位置无法实时更新
  • ¥15 DIFY API Endpoint 问题。
  • ¥20 sub地址DHCP问题
  • ¥15 delta降尺度计算的一些细节,有偿
  • ¥15 Arduino红外遥控代码有问题
  • ¥15 数值计算离散正交多项式
  • ¥30 数值计算均差系数编程
  • ¥15 redis-full-check比较 两个集群的数据出错
  • ¥15 Matlab编程问题
  • ¥15 训练的多模态特征融合模型准确度很低怎么办