package Ok;
import java.sql.Connection;
import java.sql.Date;
import java.sql.PreparedStatement;//表示预编译的SQL语句对象
import java.sql.ResultSet;//结果集
import java.util.*;//包含 collection 框架、遗留的 collection 类、事件模型、日期和时间设施、国际化和各种实用工具类(字符串标记生成器、随机数生成器和位数组)。
import java.sql.*;//提供使用 JavaTM 编程语言访问并处理存储在数据源(通常是一个关系数据库)中的数据的 API
public abstract class DBHelper {
private static String driver="com.microsoft.sqlserver.jdbc.SQLServerDriver";
private static String url="jdbc:sqlserver://localhost:1433;databaseName=studentdb;user=sa;password=123456";
//jdbc:sqlserver://localhost:1433; DatabaseName=studentdb
static{
try{
Class.forName(driver);//zaisuoyou程序中根据字符串driver运行驱动以使所有 程序可连接数据库
}
catch(Exception ex){
ex.printStackTrace();//输出标准错误流
}
}
/**
* 获得数据库连接
*
* @return
*/
private static Connection getConnect() {
Connection con = null;
try {
con = DriverManager.getConnection(url);//建立到给定数据库url的连接
} catch (Exception e) {
System.out.println(e);
}
return con;
}
/**
* 用于执行语句(eg:insert语句,update语句,delete语句)
*
* @param String
* @return int,SQL语句影响的行数
*/
public static int executeNonQuery(String cmdtext) {
Statement cmd = null;
Connection conn = null;
int x = 0;
try {
conn = getConnect();//获取连接
cmd = conn.createStatement();//创建一个Statement对象将SQL语句发送到数据库
x = cmd.executeUpdate(cmdtext);//执行SQL语句,可能为INSERT,UPDATE,DELETE或者不返回任何内容的SQL语句
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if (conn != null && (!conn.isClosed()))
conn.close();
} catch (Exception e) {
e.printStackTrace();
}
}
return x;
}
/**
* 用于执行语句(eg:insert语句,update语句,delete语句)
*
* @param String
* cmdtext,SQL语句
* @param OracleParameter[]
* params,参数集合
* @return int,SQL语句影响的行数
*/
public static int executeNonQuery(String cmdtext, Object[] params) {
PreparedStatement pstmt = null;
Connection conn = null;
int x = 0;
try {
conn = getConnect();
pstmt = conn.prepareStatement(cmdtext);//创建一个Statement对象将SQL语句发送到数据库
prepareCommand(pstmt, params);
x = pstmt.executeUpdate();
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if (conn != null && (!conn.isClosed()))
conn.close();
} catch (Exception e) {
e.printStackTrace();
}
}
return x;
}
/**
* 用于获取结果集语句(eg:selete * from table)
*
* @param cmdtext
* @return ResultSet
* @throws Exception
*/
public static Vector<Object> executeReader(String cmdtext) {//获取了一个数组,结果集的
Statement cmd = null;
Connection conn = null;
Vector<Object> al = null;
try {
conn = getConnect();
cmd = conn.createStatement();
ResultSet rs = cmd.executeQuery(cmdtext); //在此 PreparedStatement 对象中执行 SQL 查询,并返回该查询生成的 ResultSet 对象
/* 得到元数据 */
ResultSetMetaData rsmd = rs.getMetaData();//获取结果集元数据
int column = rsmd.getColumnCount();//共多少列
al = new Vector<Object>();//vector可增长的对象数组
while (rs.next()) {
Object[] ob = new Object[column];
for (int i = 1; i <= column; i++) {
ob[i - 1] = rs.getObject(i);
}
al.add(ob);
}
rs.close();
cmd.close();
conn.close();
} catch (Exception e) {
System.out.println(e);
} finally {
try {
if (conn != null && (conn.isClosed()))
conn.close();
} catch (Exception e) {
}
}
return al;
}
/**
* 用于获取结果集语句(eg:selete * from table)
*
* @param cmdtext
* @param params
* @return ResultSet
* @throws Exception
*/
public static Vector<Object> executeReader(String cmdtext,
Object[] params) {
PreparedStatement pstmt = null;
Connection conn = null;
Vector<Object> al = null;
try {
conn = getConnect();
pstmt = conn.prepareStatement(cmdtext);
prepareCommand(pstmt, params);
ResultSet rs = pstmt.executeQuery();// 在此 PreparedStatement 对象中执行 SQL 查询,并返回该查询生成的 ResultSet 对象
al = new Vector<Object>();
ResultSetMetaData rsmd = rs.getMetaData();
/*
* rs = cmd.executeQuery(cmdtext);执行select语句,返回结果集ResultSet,ResultSet
* 指向数据行游标。每调用一次next()方法,游标向下移动一行。最初它位于第一行之前,因此第一次
* 调用next 将把光标置于第一行上,使他成为当前行。随着每次调用next 导致游标向下移动一行,
* 按照从上到下的次序获取ResultSet行
*/
int column = rsmd.getColumnCount();
while (rs.next()) {
Object[] ob = new Object[column];
for (int i = 1; i <= column; i++) {
ob[i - 1] = rs.getObject(i);
}
al.add(ob);
}
rs.close();
pstmt.close();
conn.close();
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if (conn != null && (conn.isClosed()))
conn.close();
} catch (Exception e) {
e.printStackTrace();
}
}
return al;
}
/**
* 用于获取单字段值语句(用名字指定字段)
*
* @param cmdtext
* SQL语句
* @return Object
* @throws Exception
*/
public static Object executeScalar(String cmdtext) {
Statement cmd = null;
Connection conn = null;
ResultSet rs = null;
Object obj = null;
try {
conn = getConnect();
cmd = conn.createStatement();
rs = cmd.executeQuery(cmdtext);
/*
* rs = cmd.executeQuery(cmdtext);执行select语句,返回结果集ResultSet,ResultSet
* 指向数据行游标。每调用一次next()方法,游标向下移动一行。最初它位于第一行之前,因此第一次
* 调用next 将把光标置于第一行上,使他成为当前行。随着每次调用next 导致游标向下移动一行,
* 按照从上到下的次序获取ResultSet行
*/
if (rs.next()) {
obj = rs.getObject(1);//获取当前行中指定列
}
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if (conn != null && !(conn.isClosed()))
conn.close();
} catch (Exception e) {
e.printStackTrace();
}
}
return obj;
}
/**
* 用于获取单字段值语句(用名字指定字段)
*
* @param cmdtext
* SQL语句
* @param params
* OracleParameter[]
* @return Object
* @throws Exception
*/
public static Object executeScalar(String cmdtext, Object[] params) {
PreparedStatement pstmt = null;
Connection conn = null;
ResultSet rs = null;
Object obj = null;
try {
conn = getConnect();
pstmt = conn.prepareStatement(cmdtext);
prepareCommand(pstmt, params);
rs = pstmt.executeQuery();
if (rs.next()) {
obj = rs.getObject(1);
}
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if (conn != null)
conn.close();
} catch (Exception e) {
e.printStackTrace();
}
}
return obj;
}
/**
* 用于获取单字段值语句(用名字指定字段)
*
* @param cmdtext
* SQL语句
* @param name
* 列名
* @param params
* OracleParameter[]
* @return Object
* @throws Exception
*/
public static Object executeScalar(String cmdtext, String name,
Object[] params) throws Exception {
PreparedStatement pstmt = null;
Connection conn = null;
ResultSet rs = null;
try {
conn = getConnect();
pstmt = conn.prepareStatement(cmdtext);
prepareCommand(pstmt, params);
rs = pstmt.executeQuery();
if (rs.next()) {
return rs.getObject(name);
} else {
return null;
}
} catch (Exception e) {
throw new Exception("executeSqlObject方法出错:" + e.getMessage());
} finally {
try {
if (conn != null)
conn.close();
} catch (Exception e) {
throw new Exception("execut
- 1
- 2
- 3
- 4
- 5
- 6
前往页