📄 cdbconnection.java
字号:
package com.sevenEleven.javaBean;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
public class CDBConnection {
protected static Connection con = null;
/**
* @uml.property name="stmt"
*/
Statement stmt = null;
/**
* @uml.property name="sql"
*/
protected String sql = "";
/**
* @uml.property name="rs"
*/
protected ResultSet rs = null;
/** ************************************************************ */
private static final String DRIVE = "com.microsoft.jdbc.sqlserver.SQLServerDriver";
// 暂时使用jdbc-odbc连接//"com.microsoft.jdbc.sqlserver.SQLServerDriver";
/**
* @uml.property name="uSERNAME"
*/
private final String USERNAME = "sa";
/**
* @uml.property name="pASSWORD"
*/
private final String PASSWORD = "yjs";
private static final String url = "jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=school";
/***************************************************************************
* @return
**************************************************************************/
// 暂时使用jdbc-odbc数据源
/*
* private static final String CONNECTION_STRING = "jdbc:odbc:renshi";
*
* //"jdbc:microsoft:sqlserver://localhost;1433;";
*
* public static String getRootPath() {
*
* return rootpath; }
*/
public static void DBConn() { // 加载驱动
try {
Class.forName(DRIVE).newInstance();
}
catch (ClassNotFoundException e) {
System.err.println("DBConn():" + e.toString());
}
catch (Exception e) {
System.err.println("DBConn():" + e.toString());
}
}
public Connection getConnection() { // 得到连接
try {
String strUrl = url;
con = DriverManager.getConnection(strUrl, this.USERNAME,
this.PASSWORD);
}
catch (Exception e) {
con = null;
}
return con;
}
public void dropConnection() { // 关闭连接
try {
closeRs();
closeStmt();
closeConn();
}
catch (Exception ignored) {
}
finally {
con = null;
}
}
public ResultSet executeQuery(String sql) { // 执行sql查询
ResultSet rs = null;
try {
con = getConnection();
stmt = con.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,
ResultSet.CONCUR_READ_ONLY);
rs = stmt.executeQuery(sql);
}
catch (SQLException ex) {
System.err.println("DBConn.executeQuery():" + ex.getMessage());
}
return rs;
}
public int executeUpdate(String sql) { // 执行sql更新语句
int b = 0;
stmt = null;
rs = null;
try {
con = getConnection();
stmt = con.createStatement();
b = stmt.executeUpdate(sql);
stmt.close();
}
catch (SQLException ex) {
System.err.println("DBConn:executeUpdate(0:" + ex.getMessage());
}
return b;
}
public void execute(String sql) { // 执行sql语句
stmt = null;
rs = null;
try {
con = getConnection();
stmt = con.createStatement();
stmt.execute(sql);
stmt.close();
}
catch (SQLException ex) {
System.err.println("DBConn:excute():" + ex.getMessage());
}
}
public void closeStmt() { // 关闭sql连接
try {
stmt.close();
}
catch (SQLException e) {
e.printStackTrace();
}
}
public void closeConn() { // 关闭sql连接
try {
con.close();
}
catch (SQLException e) {
e.printStackTrace();
}
}
public void closeRs() { // 关闭sql连接
try {
rs.close();
}
catch (SQLException e) {
e.printStackTrace();
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -