2023-09-01
今天练习JDBC,设置好连接语句,创建好sql,更新都弄好了,一直转圈圈,不出结果。真的是见了鬼了,之前还好好的。疑惑太大,一行一行的看,最后我发现一个问题 建立连接时,用户名起的变量名必须为username,这样才能出来,不知道为啥,大冤种。
下面是正确的
package com.hh; import java.sql.Connection; import java.sql.DriverManager; import java.sql.SQLException; import java.sql.Statement; /** * @author yangLiHua * @version 1.0 * @DATE 2023-09-01 18:45:36 */ public class JDBC01 { public static void main(String[] args) throws ClassNotFoundException, SQLException { //注册驱动 Class.forName("com.mysql.cj.jdbc.Driver"); //得到连接 String url = "jdbc:mysql://localhost:3306/db01?useUnicode=true&characterEncoding=utf-8&useSSL=false&serverTimezone = GMT"; String username = "root"; String password = "123456"; Connection conn = DriverManager.getConnection(url, username, password); System.out.println("conn = " + conn); //编写sql String sql = "update account set money = 2000 where id = 1"; Statement stmt = conn.createStatement(); System.out.println("stmt = " + stmt); // int row = stmt.executeUpdate(sql); System.out.println(row>0?"执行成功":"执行失败"); System.out.println(row); // stmt.close(); conn.close(); } }
标签:username,String,stmt,一行,sql,连接,conn From: https://www.cnblogs.com/isDaHua/p/17672718.html