1 . 连接数据库首先就是先要注册驱动,其实
Class.forName("com.mysql.jdbc.Driver");
2 获取连接
// 2.获取连接
// url: 连接数据库的url字符串
// jdbc:mysql://localhost:3306/day16 -> jdbc:mysql:///day16
// 如果是localhost:3306可以省略
// user:账号
// passworc: 密码
// Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/数据库的名字", "root", "root");
Connection conn = DriverManager.getConnection("jdbc:mysql:///数据库的名字", "root", "root");
System.out.println(conn);
local host:3306 可以省略不写,中间要有3条斜杠
3 .获取Statement
Statement stmt = conn.createStatement(); // 连接数据库的通道
stmt.execute(sql语句); // 通常是创建表 stmt.executeUpdate(sql语句); // 更新 对数据库的增删改操作 stmt.executeQuery(sql语句); // 查询 查询
查询语句返回的是一个结果集
ResultSet结果集 while (rs.next()) { 游标,判断有没有下一个 rs.getXxx(列号); rs.getXxx(列名); }
JDBC工具类的编写
JDBC工具类的编程步骤: 1. 将固定字符串定义为常量 2. 在静态代码块中注册驱动(只注册一次) 3. 提供一个获取连接的方法static Connection getConneciton(); 4. 定义关闭资源的方法close(Connection conn, Statement stmt, ResultSet rs) 5. 重载关闭方法close(Connection conn, Statement stmt)
public class JDBCUtils {
// 1. 将固定字符串定义为常量
public static final String DRIVER_CLASS = "com.mysql.jdbc.Driver";
public static final String URL = "jdbc:mysql://localhost:3306/数据库名字";
public static final String USER = "root";
public static final String PASSWORD = "root";
// 2. 在静态代码块中注册驱动(只注册一次)
static {
try {
Class.forName(DRIVER_CLASS);
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
}
// 3. 提供一个获取连接的方法static Connection getConneciton();
public static Connection getConnection() throws SQLException {
Connection conn = DriverManager.getConnection(URL, USER, PASSWORD);
return conn;
}
// 4. 定义关闭资源的方法close(Connection conn, Statement stmt, ResultSet rs)
public static void close(Connection conn, Statement stmt, ResultSet rs) {
if (rs != null) {
try {
rs.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
if (stmt != null) {
try {
stmt.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
if (conn != null) {
try {
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
// 5. 重载关闭方法close(Connection conn, Statement stmt)
public static void close(Connection conn, Statement stmt) {
close(conn, stmt, null);
}
}
查询数据
@Test public void query() throws SQLException { // 1. 得到连接对象 Connection conn = JDBCUtils.getConnection(); // 3. 编写SQL语句 String sql = "SELECT * FROM employee WHERE id < ?;"; // 2. 得到Statement对象,并执行,保存ResultSet PreparedStatement pstmt = conn.prepareStatement(sql); pstmt.setInt(1, 6); ResultSet rs = pstmt.executeQuery(); // 4. 创建一个集合用于封装所有的记录 ArrayList<Employee> list = new ArrayList<>(); // 5. 每次循环封装一个学生对象 while (rs.next()) { int id = rs.getInt("id"); String name = rs.getString("name"); int age = rs.getInt("age"); String address = rs.getString("address"); // 创建员工类 Employee employee = new Employee(id, name, age, address); // 6. 把数据放到集合中 list.add(employee); } // 7. 关闭连接 JDBCUtils.close(conn, pstmt, rs); // 8. 遍历集合,循环输出学生对象 for (Employee e : list) { System.out.println(e); }
增加数据
// 1.获取连接 Connection conn = JDBCUtils.getConnection(); // 2.编写参数化的SQL语句 // String sql = "INSERT INTO employee (name, age, address) VALUES (?, ?, ?)"; String sql = "INSERT INTO employee VALUES (null, ?, ?, ?)"; // 3.获取PreparedStatement,预编译参数化的SQL语句 PreparedStatement pstmt = conn.prepareStatement(sql); // 4.设置参数 pstmt.setString(1, "刘德华"); pstmt.setInt(2, 58); pstmt.setString(3, "香港"); // 5.执行SQL int row = pstmt.executeUpdate(); System.out.println("insert:" + row);
PreparedSatement的好处
-
prepareStatement()
会先将SQL语句发送给数据库预编译。PreparedStatement
会引用着预编译后的结果。可以多次传入不同的参数给PreparedStatement
对象并执行。减少SQL编译次数,提高效率。 -
安全性更高,没有SQL注入的隐患。
-
提高了程序的可读性
PreparedSatement使用步骤?
-
编写SQL语句,未知内容使用?占位
-
获得PreparedStatement对象
-
设置实际参数
-
执行参数化SQL语句
-
关闭资源