java實現的JDBCTemplate工具

來源:互聯網
上載者:User

標籤:

1.DButil

import java.sql.Connection;import java.sql.DriverManager;import java.sql.ResultSet;import java.sql.SQLException;import java.sql.Statement;import java.util.ResourceBundle;/** * 資料庫操作工具類 * @author xrhou * */public class DBUtils {//資料庫連接地址public static String URL;//使用者名稱public static String USERNAME;//密碼public static String PASSWORD;//mysql的驅動類public static String DRIVER;private static ResourceBundle rb = ResourceBundle.getBundle("com.xrhou.util.db.db-config");private DBUtils(){}//使用靜態塊載入驅動程式static{URL = rb.getString("jdbc.url");USERNAME = rb.getString("jdbc.username");PASSWORD = rb.getString("jdbc.password");DRIVER = rb.getString("jdbc.driver");try {Class.forName(DRIVER);} catch (ClassNotFoundException e) {e.printStackTrace();}}//定義一個擷取資料庫連接的方法public static Connection getConnection(){Connection conn = null;try {conn = DriverManager.getConnection(URL, USERNAME, PASSWORD);} catch (SQLException e) {e.printStackTrace();System.out.println("擷取串連失敗");}return conn;}/** * 關閉資料庫連接 * @param rs * @param stat * @param conn */public static void close(ResultSet rs,Statement stat,Connection conn){try {if(rs!=null)rs.close();if(stat!=null)stat.close();if(conn!=null)conn.close();} catch (SQLException e) {e.printStackTrace();}}}

 2.JdbcTemplete

import java.sql.Connection;import java.sql.PreparedStatement;import java.sql.ResultSet;import java.sql.SQLException;public class JdbcTemplete {/** * 實現增刪改的抽象 * @param sql * @param args */public int update(String sql,Object... args){Connection conn = null;PreparedStatement ps = null;try{conn = DBUtils.getConnection();ps = conn.prepareStatement(sql);//設定預留位置的參數if(args!=null){for (int i = 0; i < args.length; i++) {ps.setObject(i+1, args[i]);}}return ps.executeUpdate();}catch(SQLException e){e.printStackTrace();return -1;}finally{DBUtils.close(null, ps, conn);}}/** * 查詢方法的抽象 * @param sql * @param handler * @param args * @return */public Object query(String sql,ResultSetHandler handler,Object... args){Connection conn  = null;PreparedStatement ps = null;ResultSet rs = null;try {conn = DBUtils.getConnection();ps = conn.prepareStatement(sql);if(args!=null){for (int i = 0; i < args.length; i++) {ps.setObject(i+1, args[i]);}}rs = ps.executeQuery();return handler.doHandler(rs);} catch (SQLException e) {e.printStackTrace();return null;}}}

  3.ResultSetHandler處理

import java.sql.ResultSet;import java.sql.SQLException;public interface ResultSetHandler {public Object doHandler(ResultSet rs) throws SQLException;}

  4.屬性檔案

jdbc.driver=com.mysql.jdbc.Driverjdbc.url=jdbc:mysql://localhost:3306/friendsjdbc.username=rootjdbc.password=root

  

java實現的JDBCTemplate工具

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.