小白蛋挞 2022-04-04 09:47 采纳率: 85%
浏览 27
已结题

java连接数据库出现错误,我建了两个库,一个cangku,一个student_db,连接student_db时就没有出错,连接cangku时就出现了异常。


public class Demo01 {
    public static void main(String[] args) {
        Demo01 demo01 = new Demo01();
        try {
            demo01.save(12,"电饭锅","厨具","电磁炉使用",10);
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
    public void  save(int p_no,String p_name,String p_type,String p_remark,int p_num) throws ClassNotFoundException, SQLException {
        Connection conn = null;
        Statement st = null;
        try {
            Class.forName("com.mysql.cj.jdbc.Driver");
            String url = "jdbc:mysql://127.0.0.0:3306/cangku?characterEncoding=utf8&useSSL=false&serverTimezone=Asia/Shanghai";
            conn = DriverManager.getConnection(url,"root","123456");
            st = conn.createStatement();
           int row = st.executeUpdate("insert into product(p_no,p_name,p_type,p_remark,p_num)"+"values('"+p_no+"','"+p_name+"','"+p_type+"','"+p_remark+"','"+p_num+"')");
            System.out.println(row);
            System.out.println("123");
        } finally {
            if(st!=null){
                st.close();
            }
            if(conn!=null){
                conn.close();
            }
        }
    }
}

img

img


这是表

img


这个时student_db的表

img

//import java.sql.Connection;
//import java.sql.DriverManager;
//import java.sql.SQLException;
//import java.sql.Statement;
//
//public class Demo01 {
//    public static void main(String[] args) {
//        Demo01 jd2 = new Demo01();
//        try {
//            jd2.savestudent("王萍", "男");
//        } catch (ClassNotFoundException | SQLException e) {
//            e.printStackTrace();
//        }
//    }
//    public void savestudent(String name, String sex) throws ClassNotFoundException, SQLException {
//        Connection connection=null;
//        Statement st=null;
//        try {
//            Class.forName("com.mysql.cj.jdbc.Driver");
//            String  url= "jdbc:mysql://127.0.0.1:3306/school_db?characterEncoding=utf8&useSSL=false&serverTimezone=Asia/Shanghai";
//            connection=DriverManager.getConnection(url,"root","123456");
//            st=connection.createStatement();
//            int row=   st.executeUpdate("insert into t_teacher(name,sex)" +
//                    "values ('"+name+"','"+sex+"')");
//            System.out.println(row);
//        }finally {
//            if(st!=null){
//                st.close();
//            }
//            if(connection!=null){
//                connection.close();
//            }
//        }
//    }
//
//}

我用这个代码就运行成功了。

  • 写回答

3条回答 默认 最新

  • 嘉祐-小萝卜算子 2022-04-04 10:04
    关注

    你把数据库连接url后面那些参数去掉试试

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

问题事件

  • 系统已结题 4月15日
  • 已采纳回答 4月7日
  • 创建了问题 4月4日

悬赏问题

  • ¥15 ats2837 spi2从机的代码
  • ¥200 wsl2 vllm qwen1.5部署问题
  • ¥100 有偿求数字经济对经贸的影响机制的一个数学模型,弄不出来已经快要碎掉了
  • ¥15 这个公式写进SIMULINK中的function模块的代码中应该是什么样的
  • ¥15 javaweb登陆的网页为什么不能正确连接查询数据库
  • ¥15 数学建模数学建模需要
  • ¥15 已知许多点位,想通过高斯分布来随机选择固定数量的点位怎么改
  • ¥20 nao机器人语音识别问题
  • ¥15 怎么生成确定数目的泊松点过程
  • ¥15 layui数据表格多次重载的数据覆盖问题