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

