栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

java的jdbc简单封装方法

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

java的jdbc简单封装方法

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

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

MysqlDriver=com.mysql.jdbc.Driver 
MysqlURL=jdbc:mysql://localhost:3306/one 
User=root 
Pwd=123456 
 


之后再写一个类代码如下

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; 
 
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(); 
    } 
  } 
} 
//程序结束 

之后写主类代码如下

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); 
      //依次关闭连接 
    } 
 
  } 
 
} 
 

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


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

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/151344.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号