首页 > 编程 > Java > 正文

java的jdbc简单封装方法

2019-11-26 15:03:35
字体:
来源:转载
供稿:网友

学习了jdbc一段时间后感觉自己写一个简单的封装来试试,于是参考的一些资料就写了一下不是多好,毕竟刚学也不太久

首先写配置文件:直接在src下建立一个db.properties文件然后写上内容

<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的封装练习  * @author hello  * @version jdk 1.8  */ public class ReadMain {   static Properties pos=null;//设置静态的在加载类的时候只需要一次   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 {//关闭数据库连接采用重载的方法便于封装       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 {//关闭ResultSet Connection       if(rs!=null)         rs.close();       if(co!=null)         co.close();     } catch (Exception e) {       e.printStackTrace();     }   }   public static  void Close(Connection co){     try { //关闭Connection       if(co!=null)         co.close();     } catch (Exception e) {       e.printStackTrace();     }   } } //程序结束</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       //可以看出下标是从1开始的       ps.setString(1, name);       //将对应的name插入数据表中       ps.setString(2, sex);       //将对应的sex插入数据表中       ps.execute();       //执行sql语句并且没有返回值       System.out.println("插入成功");     } catch (Exception e) {       e.printStackTrace();     }     finally{       ReadMain.Close(res, sta, con);       //依次关闭连接     }    }  } </span> 

下面是两张图是建立db.properties的步骤


以上就是本文的全部内容,希望大家可以喜欢。

发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表