JDBC(Java Database Connectivity) Java连接数据库的规范,可以使用Java语言连接数据库完成CRUD操作,即create、read、update、delete,就是数据库的增上改查。
前提:有MySQL数据库驱动
mysql-connector-java-5.1.XX使用5.X版本的数据库
mysql-connector-java-8.0..X 使用8.X版本的数据库
步骤:
1. 注册驱动
Class.forName("com.mysql.jdbc.Driver");
2. 连接数据库
Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/test01?serverTimezone=Asia/Shanghai", "root", "1234");//协议+IP+端口号+数据库+时区
当数据库驱动版本过高时,可以添加useSSL=false,这样低版本的数据库叶可以使用搞版本的驱动器
jdbc:mysql://localhost:3306/test01?serverTimezone=Asia/Shanghai&useSSL=false
3. 获取发送SQL的对象
SQL对象总体上可分两种:Statement和PreparedStatement;
Statement:每次执行时,把SQL语句发送到MySQL上执行,这样不仅效率低,而且存在SQL注入问题。在MySQL中注释用 ' # ' 表示,当SQL语句中存在 ' # ' 时,后面的部分就被注释。
PreparedStatement:预编译,先把SQL语句发送的MySQL,然后填充参数,最后执行语句。
SQL注入:用户输入的数据中有SQL关键字或语法,并且参与了SQL语句的编译,导致SQL语句编译后的条件含义为true,一直得到正确的结果,这种现象称为SQL注入。
4. 执行SQL语句
查询语句使用executQuery,添加、删除、更新使用executeUpdate
5. 处理结果
主要时处理查询的处理结果,查询返回的结果集 resultSet ,开始指针=-1,调用 next() 指针向后移动一位,当下一位空 null 时,指针=-1,利用这个规律,可以使用 while 循环处理
while (resultSet.next()) {
System.out.println(resultSet.getString(1) + "------------" +
resultSet.getString(2) + "--------------" + resultSet.getString(3));
}
6. 释放资源
resultSet.close(); statement.close(); connection.close();
查询---QueryDemo
public class ReadDemo {
public static void main(String[] args) throws ClassNotFoundException, SQLException {
Class.forName("com.mysql.jdbc.Driver");
Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/test01?serverTimezone=Asia/Shanghai&useSSL=false", "root", "1234");
Statement statement = con.createStatement();
ResultSet resultSet = statement.executeQuery("select * from person ;");
while (resultSet.next()) {
System.out.println(resultSet.getString(1) + "------------" + resultSet.getString(2) + "--------------" + resultSet.getString(3));
}
resultSet.close();
statement.close();
con.close();
}
}
增加--CreateDemo
public class CreateDemo {
public static void main(String[] args) throws ClassNotFoundException, SQLException {
Class.forName("com.mysql.jdbc.Driver");
Connection connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/test01", "root", "1234");
PreparedStatement ps = connection.prepareStatement("insert into person (username,age) values (?,?);");
int effRow = 0;
for (int i = 0; i < 10; i++) {
ps.setString(1, "name" + i);
ps.setInt(2, 20 + i);
effRow = ps.executeUpdate();
}
System.out.println("effRow = " + effRow);
ps.close();
connection.close();
}
}
更新--UpdateDemo
public class UpdateDemo {
public static void main(String[] args) throws SQLException, ClassNotFoundException {
Class.forName("com.mysql.jdbc.Driver");
Connection connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/test01", "root", "1234");
PreparedStatement ps = connection.prepareStatement("update person set age = ? where username=?;");
ps.setInt(1, 30);
ps.setString(2, "张三");
int effRow = ps.executeUpdate();
System.out.println("effRow = " + effRow);
ps.close();
connection.close();
}
}
删除--DeleteDemo
public class DeleteDemo {
public static void main(String[] args) throws ClassNotFoundException, SQLException {
Class.forName("com.mysql.jdbc.Driver");
Connection connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/test01", "root", "1234");
PreparedStatement ps = connection.prepareStatement("delete from person where id=?");
ps.setInt(1, 3);
int effRow = ps.executeUpdate();
System.out.println("effRow = " + effRow);
ps.close();
connection.close();
}
}



