欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

java的jdbc簡(jiǎn)單封裝方法

 更新時(shí)間:2015年07月28日 09:05:16   作者:internet2013  
本篇文章是對(duì)java的jdbc簡(jiǎn)單封裝方法進(jìn)行了詳細(xì)的分析介紹,需要的朋友參考下

學(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)文章

最新評(píng)論