
必须给的信息:
加载注册驱动
填写URL
连接数据库的方式
方式一
public static void testConnection1() throws SQLException {
Driver driver=new com.mysql.cj.jdbc.Driver();
String url="jdbc:mysql://localhost:3306/atguigudb1?serverTimezone=GMT%2B8";
Properties info=new Properties();
info.setProperty("user","root");
info.setProperty("password","llll");
Connection conn=driver.connect(url,info);
System.out.println(conn);
}
方式二
public static void testConnection2() throws Exception {
//1.获取Driver实现类对象,使用反射获得
Class clazz=Class.forName("com.mysql.cj.jdbc.Driver");
Driver driver=(Driver) clazz.newInstance();
String url="jdbc:mysql://localhost:3306/db2?serverTimezone=GMT%2B8";
Properties info=new Properties();
info.setProperty("user","root");
info.setProperty("password","llll");
Connection conn=driver.connect(url,info);
System.out.println(conn);
}
方式三
public static void testConnection3() throws Exception {
Class clazz=Class.forName("com.mysql.cj.jdbc.Driver");
Driver driver=(Driver) clazz.newInstance();
String url="jdbc:mysql://localhost:3306/db2?serverTimezone=GMT%2B8";
String user="root";
String password="llll";
//注册驱动
DriverManager.registerDriver(driver);
Connection conn=DriverManager.getConnection(url,user,password);
System.out.println(conn);
}
方式四
public static void testConnection4() throws Exception {
String url="jdbc:mysql://localhost:3306/db2?serverTimezone=GMT%2B8";
String user="root";
String password="llll";
//2.加载Driver 不用显式的注册驱动 其实加载的过程也可以省略
Class.forName("com.mysql.cj.jdbc.Driver");
Connection con=DriverManager.getConnection(url,user,password);
System.out.println(con);
}
方式五
//把连接数据库的信息放到配置文件中,直接进行连接
public static void testConnection5() throws Exception {
//1.读取配置文件中的基本信息
InputStream is=ConnectionTest.class.getClassLoader().getResourceAsStream("jdbc.properties");
Properties pros=new Properties();
pros.load(is);
String user=pros.getProperty("user");
String password=pros.getProperty("password");
String url=pros.getProperty("url");
String driverClass=pros.getProperty("driverClass");
Class.forName(driverClass);
Connection con=DriverManager.getConnection(url,user,password);
System.out.println(con);
}
利用JDBC操作数据库
PreparedStatement
利用JDBC实现添加(insert)Statement的作用相当于快递员,我们把要执行的SQL写给它,它再把SQL传递给具体的数据库区执行
public static void testInsert() throws IOException, ClassNotFoundException, SQLException {
//1.读取配置文件中的4个基本信息
InputStream is=ClassLoader.getSystemClassLoader().getResourceAsStream("jdbc.properties");
Properties pros=new Properties();
pros.load(is);
String user=pros.getProperty("user");
String password=pros.getProperty("password");
String url=pros.getProperty("url");
String driverClass=pros.getProperty("driverClass");
//2.加载驱动
Class.forName(driverClass);
//3.获取链接
Connection con=DriverManager.getConnection(url,user,password);
//4.预编译Sql语句,返回PreparedStatement的实例
String sql="insert into account(id,name,balance)values(?,?,?)";
PreparedStatement ps=con.prepareStatement(sql);
//5.填充占位符
ps.setInt(1,6);
ps.setString(2,"猪小明");
ps.setDouble(3,50.00);
//6.执行操作
ps.execute();
//7.资源的关闭
ps.close();
}
利用JDBC实现删除(delete)
public static void testDelete() throws Exception{
//1.获取链接
Connection connection=JDBCUtils.getConnection();
//2.预编译sql语句
String sql="delete from account where id=?";
PreparedStatement statement=connection.prepareStatement(sql);
statement.setInt(1,2);
//3.执行
statement.execute();
//4.释放资源
JDBCUtils.closeResource(connection,statement);
}
包装相同的代码,实现修改(update)
我们发现用JDBC操作数据库时(增删改),都有相同部分的代码:获得数据库连接、关闭资源.
可以将这两部分代码包装在一个类中,直接调用,简化代码
封装
public class JDBCUtils {
public static Connection getConnection() throws IOException, ClassNotFoundException, SQLException {
InputStream is=ClassLoader.getSystemClassLoader().getResourceAsStream("jdbc.properties");
Properties pros=new Properties();
pros.load(is);
String user=pros.getProperty("user");
String password=pros.getProperty("password");
String url=pros.getProperty("url");
String driverClass=pros.getProperty("driverClass");
//2.加载驱动
Class.forName(driverClass);
//3.获取链接
Connection con= DriverManager.getConnection(url,user,password);
return con;
}
public static void closeResource(Connection conn, Statement ps) throws SQLException {
ps.close();
conn.close();
}
}
修改
public static void testUpdate() throws SQLException, IOException, ClassNotFoundException {
//1.获取链接
Connection connection= JDBCUtils.getConnection();
//2.预编译sql语句,返回PreparedStatement的实例
String sql="update account set name=? where id=?";
PreparedStatement statement=connection.prepareStatement(sql);
statement.setString(1,"冷檬");
statement.setObject(2,6);
//执行
statement.execute();
//5.关闭资源
JDBCUtils.closeResource(connection,statement);
}
利用JDBC实现一个方法增删改
public static void main(String[] args) throws Exception {
String sql="delete from account where id=?";
update(sql,6);
}
public static void update(String sql,Object...args)throws Exception{
//1.获取数据库的连接
Connection connection=JDBCUtils.getConnection();
//2.预编译sql语句,返回PreparedStatement的实例
PreparedStatement statement=connection.prepareStatement(sql);
//3.填充占位符
for(int i=0;i
statement.setObject(i+1,args[i]);
}
//4.执行
statement.execute();
//5.关闭资源
JDBCUtils.closeResource(connection,statement);
}