1.jdbc概述
2.建立数据库连接
2.1方式一,直接newDriver类对象
public class test {
// 测试jdbc连接数据库
public void testConnection() throws SQLException {
// 创建驱动,获取Driver实现类对象
Driver driver=new com.mysql.jdbc.Driver();
// test是指连接的test数据库
String url ="jdbc:mysql://localhost:3306/test";
// info里面存放的是键值对,存放用户密码信息
Properties info=new Properties();
// properties类似于hashTable但键值对只能为String类型
info.setProperty("user","root");
info.setProperty("password","123456");
// 建立驱动连接
Connection conn=driver.connect(url,info);
System.out.println(conn);
}
public static void main(String[] args) {
test t=new test();
try {
t.testConnection();
} catch (SQLException throwables) {
throwables.printStackTrace();
}
}
}
2.2方式二,反射连接
public void testConnection2() throws Exception {
// 1.通过反射实例化Driver对象
Class cla=Class.forName("com.mysql.jdbc.Driver");
Driver driver=(Driver) cla.getDeclaredConstructor().newInstance();
// 2.提供要连接的数据库
String url="jdbc:mysql://localhost:3306/test";
// 3.提供账户密码
Properties info=new Properties();
info.setProperty("user","root");
info.setProperty("password","123456");
// 4.获取连接
Connection conn=driver.connect(url,info);
System.out.println(conn);
}
2.3方式三
public void testConnection3() throws Exception {
// 1.通过反射实例化Driver对象
Class clazz=Class.forName("com.mysql.jdbc.Driver");
Driver driver=(Driver) clazz.getDeclaredConstructor().newInstance();
// 2.提供三个连接基本信息
String url="jdbc:mysql://localhost:3306/test";
String user="root";
String password="123456";
// 3.注册驱动
DriverManager.registerDriver(driver);
// 4.获取连接
Connection conn=DriverManager.getConnection(url,user,password);
System.out.println(conn);
}
public void testConnection4() throws Exception {
// 1.提供三个连接基本信息
String url="jdbc:mysql://localhost:3306/test";
String user="root";
String password="123456";
// 2.类加载,这里的静态代码块会自动注册驱动
// static {
// try {
// DriverManager.registerDriver(new Driver());
// } catch (SQLException var1) {
// throw new RuntimeException("Can't register driver!");
// }
// }
Class.forName("com.mysql.jdbc.Driver");
// Driver driver=(Driver) clazz.getDeclaredConstructor().newInstance();
// DriverManager.registerDriver(driver);
// 3.获取连接
Connection conn=DriverManager.getConnection(url,user,password);
System.out.println(conn);
}
2.4方式四(配置文件)
public void testConnection5() throws Exception {
// 1.获取配置文件的基本信息
// 先获取当前类的字节码文件,在获取类加载器转换为输入流
InputStream is=test.class.getClassLoader().getResourceAsStream("jdbc.properties");
Properties pro=new Properties();
pro.load(is);
is.close();
String url=pro.getProperty("url");
String user=pro.getProperty("user");
String password=pro.getProperty("password");
String driverClass=pro.getProperty("driverClass");
// 2.加载驱动
Class.forName(driverClass);
// 3.获取连接
Connection conn=DriverManager.getConnection(url,user,password);
System.out.println(conn);
}
2.5方式五(配置文件)
public void testConnection6() throws Exception {
// 1.获取配置文件的基本信息
// 资源绑定器获取配置文件的信息
ResourceBundle rb=ResourceBundle.getBundle("jdbc");
String url=rb.getString("url");
String user= rb.getString("user");
String password=rb.getString("password");
String driverClass=rb.getString("driverClass");
// 2.加载驱动
Class.forName(driverClass);
// 3.获取连接
Connection conn=DriverManager.getConnection(url,user,password);
System.out.println(conn);
}
jdbc.properties
url=jdbc:mysql://localhost:3306/test
user=root
password=123456
driverClass=com.mysql.jdbc.Driver
3.数据库CRUD操作
3.1 statement操作数据库(有SQL注入问题)
// 使用Statement的弊端:需要拼写sql语句,并且存在SQL注入的问题
public void testLogin() {
Scanner in=new Scanner(System.in);
System.out.print("请输入账户名:");
String userName=in.nextLine();
System.out.print("请输入密码:");
String password=in.nextLine();
String sql=
"SELECT user,password \n" +
"FROM user_table \n" +
"WHERE user='"+userName+"' AND password='"+password+"'";
User returnUser=get(sql,User.class);
if(returnUser!=null)
System.out.println("登录成功");
else System.out.println("登录失败");
}
SQL注入问题
// userName为1’ OR
// password为=‘1’ OR ‘1’ = '1
这时查询语句恒成立
SELECT USER,PASSWORD
FROM user_table
WHERE USER = '1' OR ' AND PASSWORD = '='1' OR '1' = '1';
3.2 preparedStatement(解决SQL注入问题)
3.2.1 添加数据操作
// 向customers表中插入一条数据
public void insertTest() {
// 1.读取配置信息
Connection conn=null;
PreparedStatement ps=null;
try{
ResourceBundle rb=ResourceBundle.getBundle("jdbc");
String url=rb.getString("url");
String user= rb.getString("user");
String password=rb.getString("password");
String driverClass=rb.getString("driverClass");
// 2.加载驱动
Class.forName(driverClass);
// 3.建立连接
conn= DriverManager.getConnection(url,user,password);
System.out.println(conn);
// conn.commit();
// 4.预编译sql语句,返回preparedStatement实例
String sql="insert into customers(name,email,birth)values(?,?,?)";
ps=conn.prepareStatement(sql);
// 5.填充占位符,下标从1开始
ps.setString(1,"sad");
ps.setString(2,"sad@qq.com");
SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
String st="2021-10-25";
java.util.Date d=sdf.parse(st);
ps.setDate(3,new java.sql.Date(d.getTime()));
6.执行操作
ps.execute();
}catch (Exception e){
e.printStackTrace();
}finally {
7.关闭连接
if(ps!=null){
try {
ps.close();
} catch (SQLException throwables) {
throwables.printStackTrace();
}
}
if(conn!=null){
try {
conn.close();
} catch (SQLException throwables) {
throwables.printStackTrace();
}
}
}
}