JDBC的四個步驟和程式碼實現
阿新 • • 發佈:2019-01-31
JDBC是從JAVA中連線MySQL資料庫的驅動,我們可以使用程式語言來實現它,其實它的實現是固定的,只用按照這個思路和步驟就可以了。
實現分為下面四個步驟:
1.註冊驅動
2.獲得連線物件
3.獲取statement物件
因為操作資料庫無外乎增刪改查這些,如果單單每一種操作都重新寫程式碼的話,會顯得冗餘複雜,太LOW了,所以可以適當的對程式碼進行優化,具體的實現如下:
測試用得學生類如下:
將四個步驟中的1、2、4封裝成cn.edu360.utils如下:
增刪改查封裝成cn.edu360.dao如下:
實現分為下面四個步驟:
1.註冊驅動
2.獲得連線物件
3.獲取statement物件
4.釋放資源
在我們程式設計時如果需要操作資料庫只需要按照這四個步驟就可以了。
我事先在Navicat for MySQL中建了一個store資料庫,並且建立了一個有四個欄位的學生表,如下圖:
因為操作資料庫無外乎增刪改查這些,如果單單每一種操作都重新寫程式碼的話,會顯得冗餘複雜,太LOW了,所以可以適當的對程式碼進行優化,具體的實現如下:
測試用得學生類如下:
package cn.edu360.entity; public class Student { private Integer id; private String name; private String gender; private int age; public Student() { } public Student(Integer id, String name, String gender, int age) { super(); this.id = id; this.name = name; this.gender = gender; this.age = age; } @Override public String toString() { return "Student [id=" + id + ", name=" + name + ", gender=" + gender + ", age=" + age + "]"; } public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getGender() { return gender; } public void setGender(String gender) { this.gender = gender; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } }
將四個步驟中的1、2、4封裝成cn.edu360.utils如下:
package cn.edu360.utils; import java.sql.Connection; import java.sql.DriverManager; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement; /* * JDBC四個步驟 */ public class JDBCUtils { private static String driverName = "com.mysql.jdbc.Driver"; private static String jdbc_url = "jdbc:mysql://localhost:3306/store"; private static String username = "root"; private static String password = "12580"; // 1.註冊驅動,只需要註冊一次即可(在靜態程式碼中執行一次即可) static { try { Class.forName(driverName); } catch (ClassNotFoundException e) { e.printStackTrace(); } } // 2.獲得連線物件,以後需要多次獲得 public static Connection getConnection() { try { return DriverManager.getConnection(jdbc_url, username, password); } catch (SQLException e) { e.printStackTrace(); } return null; } // 3.獲取statement物件,需要反覆執行,不需要定義這個方法了 // 4.釋放資源,需要反覆執行的(使用方法的過載來定義兩個不同引數列表的釋放方法) public static void release(Statement stmt, Connection connection) { if (stmt != null) { try { stmt.close(); } catch (SQLException e) { e.printStackTrace(); } stmt = null; } if (connection != null) { try { connection.close(); } catch (SQLException e) { e.printStackTrace(); } connection = null; } } public static void release(ResultSet rs, Statement stmt, Connection connection) { if (rs != null) { try { rs.close(); } catch (SQLException e) { e.printStackTrace(); } rs = null; } if (stmt != null) { try { stmt.close(); } catch (SQLException e) { e.printStackTrace(); } stmt = null; } if (connection != null) { try { connection.close(); } catch (SQLException e) { e.printStackTrace(); } connection = null; } } }
增刪改查封裝成cn.edu360.dao如下:
package cn.edu360.dao; import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; import java.util.ArrayList; import java.util.List; import cn.edu360.entity.Student; import cn.edu360.utils.JDBCUtils; public class StudentDao { /** * 新增資料到MySQL中 * * @param student */ public void save(Student student) { // 獲得connection Connection connection = JDBCUtils.getConnection(); PreparedStatement prepareStatement = null; // 建立Statement物件 // 定義一個sql String sql = "INSERT INTO student VALUES (?, ?, ?, ?)"; try { prepareStatement = connection.prepareStatement(sql); prepareStatement.setInt(1, student.getId()); prepareStatement.setString(2, student.getName()); prepareStatement.setString(3, student.getGender()); prepareStatement.setInt(4, student.getAge()); // 執行sql語句 prepareStatement.executeUpdate(); } catch (SQLException e) { e.printStackTrace(); } // 釋放資源 JDBCUtils.release(prepareStatement, connection); } /** * 根據ID號刪除資料 * * @param id */ public void deleteById(Integer id) { // 獲得connection Connection connection = JDBCUtils.getConnection(); PreparedStatement prepareStatement = null; // 建立Statement物件 // 定義一個sql String sql = "DELETE FROM student WHERE id = ?"; try { prepareStatement = connection.prepareStatement(sql); prepareStatement.setInt(1, id); // 執行sql語句 prepareStatement.executeUpdate(); } catch (SQLException e) { e.printStackTrace(); } // 釋放資源 JDBCUtils.release(prepareStatement, connection); } /** * 更新學生資訊 * * @param student */ public void update(Student student) { // 獲得connection Connection connection = JDBCUtils.getConnection(); PreparedStatement prepareStatement = null; // 建立Statement物件 // 定義一個sql String sql = "UPDATE student SET name = ? , gender = ? , age = ? WHERE id = ?"; try { prepareStatement = connection.prepareStatement(sql); prepareStatement.setString(1, student.getName()); prepareStatement.setString(2, student.getGender()); prepareStatement.setInt(3, student.getAge()); prepareStatement.setInt(4, student.getId()); // 執行sql語句 prepareStatement.executeUpdate(); } catch (SQLException e) { e.printStackTrace(); } // 釋放資源 JDBCUtils.release(prepareStatement, connection); } /** * 根據ID號查詢學生資訊 * * @param id * @return */ public Student getById(Integer id) { Student s = null; // 獲得connection Connection connection = JDBCUtils.getConnection(); PreparedStatement prepareStatement = null; ResultSet rs =null; // 建立Statement物件 // 定義一個sql String sql = "SELECT * FROM student WHERE id = ?"; try { prepareStatement = connection.prepareStatement(sql); prepareStatement.setInt(1,id); // 執行sql語句 rs = prepareStatement.executeQuery(); // 從rs中得到資料 rs.next(); Integer sid = rs.getInt(1); String name = rs.getString(2); String gender = rs.getString(3); int age = rs.getInt(4); s = new Student(); s.setId(sid); s.setName(name); s.setGender(gender); s.setAge(age); } catch (SQLException e) { e.printStackTrace(); } // 釋放資源 JDBCUtils.release(rs,prepareStatement, connection); return s; } /** * 查詢所有學生資訊 * * @return */ public List<Student> findAll() { ArrayList<Student> list = new ArrayList<Student>(); // 獲得connection Connection connection = JDBCUtils.getConnection(); PreparedStatement prepareStatement = null; ResultSet rs = null; // 建立Statement物件 // 定義一個sql String sql = "SELECT * FROM student"; try { prepareStatement = connection.prepareStatement(sql); // 執行sql語句 rs = prepareStatement.executeQuery(); while (rs.next()) { // 從rs中得到資料 Integer sid = rs.getInt(1); String name = rs.getString(2); String gender = rs.getString(3); int age = rs.getInt(4); Student s = new Student(); s.setId(sid); s.setName(name); s.setGender(gender); s.setAge(age); list.add(s); } } catch (SQLException e) { e.printStackTrace(); } // 釋放資源 JDBCUtils.release(rs,prepareStatement, connection); return list; } }
測試程式碼如下:
package cn.edu360.test;
import java.util.List;
import org.junit.Test;
import cn.edu360.dao.StudentDao;
import cn.edu360.entity.Student;
public class TestDao {
@Test
public void testSave() {
StudentDao dao = new StudentDao();
Student s = new Student(11, "tom", "f", 20);
dao.save(s);
}
@Test
public void testDelete() {
StudentDao dao = new StudentDao();
dao.deleteById(11);
}
@Test
public void testUpdate() {
StudentDao dao = new StudentDao();
Student s = new Student(1, "zhaozhoa", "m", 18);
dao.update(s);
}
@Test
public void testGetById() {
StudentDao dao = new StudentDao();
Student s = dao.getById(2);
System.out.println(s);
}
@Test
public void testFindAll() {
StudentDao dao = new StudentDao();
List<Student> list = dao.findAll();
for (Student s : list) {
System.out.println(s);
}
}
}