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

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 win2012磁盘空间不足,c盘正常,d盘无法写入
  • ¥15 用土力学知识进行土坡稳定性分析与挡土墙设计
  • ¥70 PlayWright在Java上连接CDP关联本地Chrome启动失败,貌似是Windows端口转发问题
  • ¥15 帮我写一个c++工程
  • ¥30 Eclipse官网打不开,官网首页进不去,显示无法访问此页面,求解决方法
  • ¥15 关于smbclient 库的使用
  • ¥15 微信小程序协议怎么写
  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启