zoukankan      html  css  js  c++  java
  • JabaBean对数据库的操作增删改查

    View Code
    import java.sql.Connection;
    import java.sql.DriverManager;
    import java.sql.PreparedStatement;
    import java.sql.ResultSet;
    import java.sql.SQLException;
    import java.sql.Statement;


    public class DBUtil {
    /**
    * 获取一个数据库连接
    *
    @return SQLException
    *
    @throws InstantiationException
    *
    @throws IllegalAccessException
    *
    @throws ClassNotFoundException
    */

    public Connection getConnection() throws SQLException,
    IllegalAccessException,InstantiationException,ClassNotFoundException{
    Connection conn=null;
    //加载数据库驱动
    Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver");
    //数据库连接URL
    String url = "jdbc:microsoft:sqlserver://localhost:1433;Database=pubs";
    //数据库用户名
    String user="sa";
    //数据库密码
    String password="";
    //根据数据库参数获得一个数据库连接
    conn = DriverManager.getConnection(url, user, password);
    return conn;
    }

    /**
    * 根据传入的SQL语句返回一个结果集
    *
    @param sql
    *
    @return
    *
    @throws Exception
    */
    public ResultSet select(String sql)throws Exception{
    Connection conn = null;
    Statement stmt = null;
    ResultSet rs = null;
    try{
    conn = getConnection();
    stmt = conn.createStatement();
    rs = stmt.executeQuery(sql);
    return rs;
    }catch (SQLException sqle) {
    throw new SQLException("select data exception: "+sqle.getMessage());
    }catch(Exception e){
    throw new SQLException("System e exception: "+ e.getMessage());
    }
    }

    /**
    * 根据传入的SQL语句向数据库增加一条记录
    *
    @param sql
    *
    @throws Exception
    */
    public void insert(String sql) throws Exception{
    Connection conn = null;
    PreparedStatement ps = null;
    try{
    conn = getConnection();
    ps = conn.prepareStatement(sql);
    ps.executeUpdate();
    }catch(SQLException sqle){
    throw new Exception("insert data exception: "+ sqle.getMessage());
    }finally{

    try{
    if(ps!=null){
    ps.close();
    }
    }catch (Exception e) {
    throw new Exception("ps close exception: "+e.getMessage());
    }
    }
    try{
    if(conn != null){
    conn.close();
    }
    }catch (Exception e) {
    throw new Exception("connection close exception: "+e.getMessage());
    }
    }

    /**
    * 根据传入的SQL语句更新数据库记录
    *
    @param sql
    *
    @throws Exception
    */
    public void update(String sql) throws Exception{
    Connection conn = null;
    PreparedStatement ps = null;
    try{
    conn = getConnection();
    ps = conn.prepareStatement(sql);
    ps.executeUpdate();
    }catch(SQLException sqle){
    throw new Exception("usdate exception: "+sqle.getMessage());
    }finally{

    try{
    if(ps!=null){
    ps.close();
    }
    }catch (Exception e) {
    throw new Exception("ps close exception: "+e.getMessage());
    }
    }
    try{
    if(conn != null){
    conn.close();
    }
    }catch(Exception e){
    throw new Exception("connection close exception: "+e.getMessage());
    }
    }

    /**
    * 根据传入的SQL语句删除一条数据库记录
    *
    */
    public void delete(String sql) throws Exception{
    Connection conn = null;
    PreparedStatement ps = null;
    try{
    conn = getConnection();
    ps = conn.prepareStatement(sql);
    ps.executeUpdate();
    }catch(SQLException sqle){
    throw new Exception("delete exception: "+sqle.getMessage());
    }finally{

    try{
    if(ps!=null){
    ps.close();
    }
    }catch (Exception e) {
    throw new Exception("ps close exception: "+e.getMessage());
    }
    }
    try{
    if(conn != null){
    conn.close();
    }
    }catch(Exception e){
    throw new Exception("connection close exception: "+e.getMessage());
    }
    }
    }

  • 相关阅读:
    【剑指offer】3-数组中重复的数字
    自定义管道遇到问题解决方案
    vue的双向绑定示例
    子组件传递给父组件数据
    vue里的共享对象示例
    mysql里的explain介绍
    mysql索引
    修改MySQL字符集
    v-model修饰符示例
    vue下拉列表示例
  • 原文地址:https://www.cnblogs.com/FCWORLD/p/2195801.html
Copyright © 2011-2022 走看看