JDBC的五個步驟及基本CRUD操作
阿新 • • 發佈:2018-12-10
使用jdbc對資料庫的crud操作的五個基本步驟:
1、載入驅動
2、建立連線
3、編寫sql
4、預編譯sql
5、返回結果集
具體程式碼示例如下,我使用的是oracle資料庫:
1、建立一個DBUtils工具類
package com.zzy.util; import java.io.IOException; import java.io.InputStream; import java.sql.Connection; import java.sql.DriverManager; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement; import java.util.Properties; public class DBUtils { private static String driver = null; private static String url = null; private static String username = null; private static String password = null; static{ try { //載入配置檔案 InputStream in = DBUtils.class.getClassLoader().getResourceAsStream("DB.properties"); Properties properties = new Properties(); properties.load(in); driver = properties.getProperty("driver"); url = properties.getProperty("url"); username = properties.getProperty("username"); password = properties.getProperty("password"); //載入驅動 Class.forName(driver); } catch (IOException e) { e.printStackTrace(); } catch (ClassNotFoundException e) { e.printStackTrace(); } } /** * 獲取連線 * @return Connection * @throws SQLException */ public static Connection getConnection() throws SQLException{ return DriverManager.getConnection(url,username,password); } /** * 關閉資源 * @param resultSet * @param statement * @param connection */ public static void releaseResources(ResultSet resultSet,Statement statement,Connection connection){ try { if(resultSet != null){ resultSet.close(); } } catch (SQLException e) { e.printStackTrace(); }finally{ resultSet = null; try { if(statement != null){ statement.close(); } } catch (SQLException e) { e.printStackTrace(); }finally{ statement = null; try { if(connection != null){ connection.close(); } } catch (SQLException e) { e.printStackTrace(); }finally{ connection = null; } } } } }
2、DB.properties配置檔案內容
driver=oracle.jdbc.driver.OracleDriver
url=jdbc:oracle:thin:@127.0.0.1:1521:orcl
username=orcl
password=orcl
3、使用PreparedStatement物件完成對資料庫的CRUD操作
package com.zzy.test; import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; import org.junit.Test; import com.zzy.bean.News; import com.zzy.util.DBUtils; public class JDBCTest { Connection conn = null; PreparedStatement pst = null; ResultSet rs = null; /** * 從資料庫中查詢一條語句 */ @Test public void select(){ try { //獲取資料庫連線 conn = DBUtils.getConnection(); //編寫sql語句 String sql = "select * from news where newsid = ?"; //預編譯語句 pst = conn.prepareStatement(sql); //為sql中的引數賦值,索引從1開始 pst.setInt(1, 21); //執行查詢語句 rs = pst.executeQuery(); News news = null; if(rs.next()){ news = new News(); news.setNewsId(rs.getInt(1)); news.setTitle(rs.getString(2)); news.setContent(rs.getString(3)); news.setAuthor(rs.getString(4)); news.setCreateTime(rs.getString(5)); news.setThemeId(rs.getInt(6)); System.out.println(news); } } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); }finally{ DBUtils.releaseResources(rs, pst, conn); } } /** * 往資料庫中插入一條資料 */ @Test public void insert(){ try { //獲取資料庫連線 conn = DBUtils.getConnection(); //編寫sql語句 String sql = "insert into news (newsid,title,content,author,createtime,themeid) values(seq_news.nextval,?,?,?,?,?)"; //預編譯語句 pst = conn.prepareStatement(sql); //為sql中的引數賦值,索引從1開始 pst.setString(1, "新聞標題"); pst.setString(2, "新聞內容"); pst.setString(3, "新聞作者"); pst.setString(4, "建立時間");//pst.setDate(4, new java.sql.Date(new Date().getTime()));//如果createtime是date型別 pst.setInt(5, 1); //執行插入操作,executeUpdate方法返回成功的條數 int num = pst.executeUpdate(); if(num>0){ System.out.println("成功插入"+num+"條資料"); } } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); }finally{ DBUtils.releaseResources(rs, pst, conn); } } /** * 修改表中的一條資料 */ @Test public void update(){ try { conn = DBUtils.getConnection(); String sql = "update news set title=?,content=? where newsid=?"; pst = conn.prepareStatement(sql); pst.setString(1, "新聞標題"); pst.setString(2, "新聞內容"); pst.setInt(3, 21); int num = pst.executeUpdate(); if(num>0){ System.out.println("成功修改"+num+"條資料"); } } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); }finally{ DBUtils.releaseResources(rs, pst, conn); } } /** * 刪除表中的一條資料 */ @Test public void delete(){ try { conn = DBUtils.getConnection(); String sql = "delete from news where newsid=?"; pst = conn.prepareStatement(sql); pst.setInt(1, 1); int num = pst.executeUpdate(); if(num > 0){ System.out.println("成功刪除"+num+"條資料"); } } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); }finally{ DBUtils.releaseResources(rs, pst, conn); } } }