java的jdbc簡(jiǎn)單封裝方法
學(xué)習(xí)了jdbc一段時(shí)間后感覺(jué)自己寫(xiě)一個(gè)簡(jiǎn)單的封裝來(lái)試試,于是參考的一些資料就寫(xiě)了一下不是多好,畢竟剛學(xué)也不太久
首先寫(xiě)配置文件:直接在src下建立一個(gè)db.properties文件然后寫(xiě)上內(nèi)容
<span style="font-size:18px;">MysqlDriver=com.mysql.jdbc.Driver MysqlURL=jdbc\:mysql\://localhost\:3306/one User=root Pwd=123456 </span>
之后再寫(xiě)一個(gè)類代碼如下
<span style="font-size:18px;">package cn.java.ad; import java.io.IOException; import java.sql.Connection; import java.sql.DriverManager; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement; import java.util.Properties; /** * 本例是寫(xiě)了一個(gè)小的封裝 * 對(duì)jdbc的封裝練習(xí) * @author hello * @version jdk 1.8 */ public class ReadMain { static Properties pos=null;//設(shè)置靜態(tài)的在加載類的時(shí)候只需要一次 static{ pos=new Properties(); //建立Peoperties用來(lái)讀取配置文件 try {//下面是用來(lái)讀取配置文件的 pos.load(Thread.currentThread().getContextClassLoader().getResourceAsStream("db.properties")); } catch (IOException e) { e.printStackTrace(); } } public static Connection getcon(){//建立Connection連接 try { Class.forName(pos.getProperty("MysqlDriver"));//加載com.mysql.jdbc.Driver }catch (ClassNotFoundException e) { e.printStackTrace(); } try {//加載URL ,User,password return DriverManager.getConnection(pos.getProperty("MysqlURL"), pos.getProperty("User"),pos.getProperty("Pwd")); } catch (SQLException e) { e.printStackTrace(); } return null; } public static void Close(ResultSet rs,Statement st,Connection co){ try {//關(guān)閉數(shù)據(jù)庫(kù)連接采用重載的方法便于封裝 if(rs!=null) rs.close(); if(st!=null) st.close(); if(co!=null) co.close(); } catch (Exception e) { e.printStackTrace(); } } public static void Close(ResultSet rs,Connection co){ try {//關(guān)閉ResultSet Connection if(rs!=null) rs.close(); if(co!=null) co.close(); } catch (Exception e) { e.printStackTrace(); } } public static void Close(Connection co){ try { //關(guān)閉Connection if(co!=null) co.close(); } catch (Exception e) { e.printStackTrace(); } } } //程序結(jié)束</span>
之后寫(xiě)主類代碼如下
<span style="font-size:18px;">package cn.java.ad; import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.Statement; public class Main { public static void main(String[] args) { Connection con=null; ResultSet res=null; Statement sta=null; String sql=null; String name="李雷"; String sex="男"; PreparedStatement ps=null; try { con=ReadMain.getcon(); sql="insert into student(id,name,sex,phone)VALUES(1235,?,?,15896324131)"; ps=con.prepareStatement(sql);//獲取sql語(yǔ)句 //在這里 the first parameter is 1, the second is 2, ... //x the parameter value //可以看出下標(biāo)是從1開(kāi)始的 ps.setString(1, name); //將對(duì)應(yīng)的name插入數(shù)據(jù)表中 ps.setString(2, sex); //將對(duì)應(yīng)的sex插入數(shù)據(jù)表中 ps.execute(); //執(zhí)行sql語(yǔ)句并且沒(méi)有返回值 System.out.println("插入成功"); } catch (Exception e) { e.printStackTrace(); } finally{ ReadMain.Close(res, sta, con); //依次關(guān)閉連接 } } } </span>
下面是兩張圖是建立db.properties的步驟
以上就是本文的全部?jī)?nèi)容,希望大家可以喜歡。
相關(guān)文章
SpringBoot接口加密與解密的實(shí)現(xiàn)
這篇文章主要介紹了SpringBoot接口加密與解密的實(shí)現(xiàn)2023-10-10java網(wǎng)上商城開(kāi)發(fā)之郵件發(fā)送功能(全)
這篇文章主要介紹了java網(wǎng)上商城開(kāi)發(fā)之郵件發(fā)送功能,第一部分介紹了環(huán)境配置,第二部分則介紹了具體實(shí)現(xiàn)代碼,感興趣的小伙伴們可以參考一下2016-03-03SpringCloud?微服務(wù)數(shù)據(jù)權(quán)限控制的實(shí)現(xiàn)
這篇文章主要介紹的是權(quán)限控制的數(shù)據(jù)權(quán)限層面,意思是控制可訪問(wèn)數(shù)據(jù)資源的數(shù)量,對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友參考下吧2021-11-11Java利用MYSQL LOAD DATA LOCAL INFILE實(shí)現(xiàn)大批量導(dǎo)入數(shù)據(jù)到MySQL
Mysql load data的使用,MySQL的LOAD DATAINFILE語(yǔ)句用于高速地從一個(gè)文本文件中讀取行,并裝入一個(gè)表中2018-03-03