java的jdbc簡單封裝方法
學(xué)習(xí)了jdbc一段時間后感覺自己寫一個簡單的封裝來試試,于是參考的一些資料就寫了一下不是多好,畢竟剛學(xué)也不太久
首先寫配置文件:直接在src下建立一個db.properties文件然后寫上內(nèi)容
<span style="font-size:18px;">MysqlDriver=com.mysql.jdbc.Driver MysqlURL=jdbc\:mysql\://localhost\:3306/one User=root Pwd=123456 </span>
之后再寫一個類代碼如下
<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;
/**
* 本例是寫了一個小的封裝
* 對jdbc的封裝練習(xí)
* @author hello
* @version jdk 1.8
*/
public class ReadMain {
static Properties pos=null;//設(shè)置靜態(tài)的在加載類的時候只需要一次
static{
pos=new Properties(); //建立Peoperties用來讀取配置文件
try {//下面是用來讀取配置文件的
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ù)庫連接采用重載的方法便于封裝
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>
之后寫主類代碼如下
<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語句
//在這里 the first parameter is 1, the second is 2, ...
//x the parameter value
//可以看出下標(biāo)是從1開始的
ps.setString(1, name);
//將對應(yīng)的name插入數(shù)據(jù)表中
ps.setString(2, sex);
//將對應(yīng)的sex插入數(shù)據(jù)表中
ps.execute();
//執(zhí)行sql語句并且沒有返回值
System.out.println("插入成功");
} catch (Exception e) {
e.printStackTrace();
}
finally{
ReadMain.Close(res, sta, con);
//依次關(guān)閉連接
}
}
}
</span>
下面是兩張圖是建立db.properties的步驟


以上就是本文的全部內(nèi)容,希望大家可以喜歡。
相關(guān)文章
MybatisPlus出現(xiàn)Error attempting to get col
本文重點分析使用@EnumValue注解轉(zhuǎn)換時遇到的一下錯誤原因,及解決方案,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2023-11-11
SpringSecurity的@EnableWebSecurity注解詳解
這篇文章主要介紹了SpringSecurity的@EnableWebSecurity注解詳解,@EnableWebSecurity是開啟SpringSecurity的默認行為,它的上面有一個Import注解導(dǎo)入了WebSecurityConfiguration類,就是往IOC容器中注入了WebSecurityConfiguration這個類,需要的朋友可以參考下2023-11-11
Java中==與equals()及hashcode()三者之間的關(guān)系詳解
最近也是在讀Hollis的《深入理解Java核心技術(shù)》里面一節(jié)講到了equals()和hashcode()的關(guān)系,對于這個高頻面試點,咱們需要認真理清一下幾者之間的關(guān)系2022-10-10
基于Spring概念模型:PathMatcher 路徑匹配器
這篇文章主要介紹了Spring概念模型:PathMatcher 路徑匹配器,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-09-09
RestTemplate發(fā)送請求時Cookie的影響及注意事項說明
這篇文章主要介紹了RestTemplate發(fā)送請求時Cookie的影響及注意事項說明,具有很好的參考價值,希望對大家有所幫助。2023-07-07

