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 求大家看看Nonce如何配置
  • ¥15 Matlab怎么求解含参的二重积分?
  • ¥15 苹果手机突然连不上wifi了?
  • ¥15 cgictest.cgi文件无法访问
  • ¥20 删除和修改功能无法调用
  • ¥15 kafka topic 所有分副本数修改
  • ¥15 小程序中fit格式等运动数据文件怎样实现可视化?(包含心率信息))
  • ¥15 如何利用mmdetection3d中的get_flops.py文件计算fcos3d方法的flops?
  • ¥40 串口调试助手打开串口后,keil5的代码就停止了
  • ¥15 电脑最近经常蓝屏,求大家看看哪的问题