jdbc链接数据库

package itheima;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;

public class jdbcdemo {
public static void main(String[] args) throws Exception {

//1.注册驱动
Class.forName("com.mysql.cj.jdbc.Driver");

//2.获取链接
String url="jdbc:mysql://localhost:3306/db1?useSSL=false&serverTimezone=UTC";
String username="root";
String password="123456789";
Connection conn = DriverManager.getConnection(url, username, password);

//定义sql语句
String sql = "update account set money = 3000 where id=1";

//4.获取sql执行的对象 Statement
Statement stmt = conn.createStatement();

//执行sql
int count = stmt.executeUpdate(sql);//受影响的行数

//处理结果
System.out.println(count);

//释放资源
stmt.close();
conn.close();
}
}
posted @ 2022-08-04 16:55  藏进夜里躲在光下  阅读(23)  评论(0)    收藏  举报