//另一种方式注册驱动
import java.sql.*;
import java.lang.*;
public class JDBCTest02{
public static void main(String[] args){
Statement stmt = null;
Connection conn = null;
try{
//1.注册驱动
//第一种方法:DriverManager.registerDriver(new com.mysql.cj.jdbc.Driver()); 以后不这么写
//第二种方法:常用的,可以用配置文件 .properties
Class.forName("com.mysql.cj.jdbc.Driver");
//2.获取连接
String url = "jdbc:mysql://192.168.1.105:3306/harenmu?serverTimezone = UTC";
String user = "root";
String password = "huang67259@";
conn = DriverManager.getConnection(url,user,password);
System.out.println(conn);
}catch(SQLException e){
e.printStackTrace();
}catch(ClassNotFoundException e){
e.printStackTrace();
}
/*
//3.获取数据库操作对象
stmt = conn.createStatement();
System.out.println(stmt);
//4.执行sql语句
//String sql = "delete from dept where deptno in (50,60)";
String sql = "update dept set dname = 'rengshibu',loc = 'nanjing' where deptno = 60";
int count = stmt.executeUpdate(sql);
System.out.println(count == 1 ? "success":"fail");
//5.处理查询结果集
}catch(SQLException e){
e.printStackTrace();
}finally{
try{
if(stmt != null){
stmt.close();
}
}catch(SQLException e){
e.printStackTrace();
}
try{
if(conn != null){
conn.close();
}
}catch(SQLException e){
e.printStackTrace();
}
}
*/
}
}
JDBC代码3
最新推荐文章于 2022-12-04 13:31:51 发布