来自 新葡8455操作系统 2020-01-27 03:49 的文章
当前位置: 新葡棋牌京官网app > 新葡8455操作系统 > 正文

DBCONN

public class DBHelper{
 //定义类变量

package Ulike_servlet; //将该类保存到com.tools包中
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 DbConn {
protected PreparedStatement pstm;// 预编译sql
public Connection conn; // 声明Connection对象的实例
public Statement stmt; // 声明Statement对象的实例
public ResultSet rs; // 声明ResultSet对象的实例
private static String dbClassName;// 定义保存数据库驱动的变量
private static String dbUrl;
private static String dbUser;
private static String dbPwd;
public DbConn() { // 定义构造方法
try { // 捕捉异常
dbClassName = "com.mysql.jdbc.Driver"; // 获取数据库驱动
dbUrl = "jdbc:mysql://localhost:3306/btmdb?useUnicode=true&characterEncoding=utf-8"; // 获取URL
dbUser = "root"; // 获取登录用户
dbPwd = "0118"; // 获取密码
} catch (Exception e) {
e.printStackTrace(); // 输出异常信息
}
}

 //构造器
 public DBHelper()
 {
  //类加载
  try
  {
   Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
   System.out.println("类加载成功");
  }catch(Exception ex)
  {
   System.out.println("类加载失败");
   System.out.println(ex.toString());
  }
  //数据库连接
  try
  {
   conn = DriverManager.getConnection("jdbc:odbc:数据源名称");
   System.out.println("数据库连接成功");
  }catch(Exception ce)
  {
   System.out.println("数据库连接失败");
   System.out.println(ce.toString());
  }
 }

/*
* 功能:关闭数据库的连接
*/
protected void closeAll() {
if (rs != null) {
try {
rs.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
if (pstm != null) {
try {
pstm.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
if (stmt != null) {
try {
stmt.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
try {
if (conn != null && conn.isClosed() == false) {
conn.close();
}
} catch (SQLException e) {
e.printStackTrace();
}
}
/**
* 执行sql语句
* */
public int executeSQL(String sql, Object[] param) throws Exception {
int rows = 0;
try {
conn=getConnection();
if (param != null && param.length > 0) {
pstm = conn.prepareStatement(sql);
for (int i = 0; i < param.length; i++) {
pstm.setString(i + 1, param[i].toString());
}
rows = pstm.executeUpdate();
} else {
stmt = conn.createStatement();
rows = stmt.executeUpdate(sql);
}
} finally {
this.closeAll();
}
System.out.println("处理成功!!");
return rows;
}
/**
* 查询sql语句
* */
protected ResultSet executeQuery(String sql, Object[] param) throws Exception {
try {
conn=getConnection();
if (param != null && param.length > 0) {
pstm = conn.prepareStatement(sql);
for (int i = 0; i < param.length; i++) {
pstm.setString(i + 1, param[i].toString());
}
rs = pstm.executeQuery();
} else {
stmt = conn.createStatement();
rs = stmt.executeQuery(sql);
}
} finally {

 //Connection 对象
 Connection conn = null;
 //Statement 对象
 Statemen st = null;
 //ResultSet 对象
 ResultSet rs = null;

  • dbClassName
  • "rn链接位置:"
  • dbUrl
  • "rn用户/密码"
  • dbUser + "/" + dbPwd);
    }
    return conn;
    }

//建包
package pp;

}
System.out.println("处理成功!!");
return rs;
}

/**
*1 类变量
*2 类加载和数据库连接
*3 doSelete方法做查询
*4 doUpdate方法做增加,删除,插入三个方法
*5 释放资源
*/

}

本文由新葡棋牌京官网app发布于新葡8455操作系统,转载请注明出处:DBCONN

关键词: