📄 dbconn.java
字号:
/*
* Created on 2006-3-512:38:57
*
* To change the template for this generated file go to
* Window>Preferences>Java>Code Generation>Code and Comments
*/
package com.hbnu.common;
import java.sql.*;
import java.util.ArrayList;
/**
* @author aim
*
* To change the template for this generated type comment go to
* Window>Preferences>Java>Code Generation>Code and Comments
*/
/**
* 公用数据库访问类
*/
public class DBConn
{
ResultSet rs=null;
/**数据库连接对象*/
private Connection conn = null;
/**数据库执行对象*/
private Statement statement =null;
/**记录数据库列数*/
private int columNum = 0;
/**记录数据库中数据的行数*/
private int velNum = 0;
/**驱动程序地址、用户、密码*/
String sConnStr="jdbc:mysql://127.0.0.1:3306/sjfx?user=root&password=1234&useUnicode=true&characterEncoding=GB2312";
/**
* 连接数据库函数
* @throws SQLException
* @throws ClassNotFoundException
* @throws IllegalAccessException
* @throws InstantiationException
*/
public void start() throws SQLException, InstantiationException, IllegalAccessException, ClassNotFoundException{
Class.forName("org.gjt.mm.mysql.Driver").newInstance();
conn= DriverManager.getConnection(sConnStr);
statement=conn.createStatement();
}
/**
* 删除对象函数
* @param sql SQL执行语句
* @throws Exception
*/
public int execDelete(String sql) throws Exception {
return execInsert(sql);
}
/**
* 修改对象函数
* @param sql SQL执行语句
* @throws Exception
*/
public int execUpdate(String sql) throws SQLException, Exception {
return execInsert(sql);
}
/**
* 插入数据函数
* @param sql SQL执行语句
* @throws Exception
*/
public int execInsert(String sql) throws SQLException, Exception {
PreparedStatement statement = null;
int rstCnt = 0;
try {
statement =conn.prepareStatement(sql);
/* SQL返回聚集 */
rstCnt = statement.executeUpdate();
} catch (SQLException e) {
throw e;
} finally {
/* 判断 */
if (statement != null) {
statement.close();
statement = null;
}
}
return rstCnt;
}
/**
* 关闭数据库连接
* @throws Exception
*/
public void execClose()throws Exception{
statement.close();
conn.close();
}
/**
* 返回数据记录的列数
* @return
*/
public int getColumNum() {
return this.columNum;
}
/**
* 返回数据记录的行数
* @return
*/
public int getvelNum() {
return this.velNum;
}
/**
* 数据查询函数
* @param sql SQL执行语句
* @return 返回数据结果
* @throws Exception
*/
public ArrayList execSelect(String sql) throws Exception {
ResultSet rs=statement.executeQuery(sql);
ArrayList arrayList = new ArrayList();
try {
if ( rs != null ) {
/*创建一个元数据,描述数据库信息或数据库的一部分数据*/
ResultSetMetaData rsmd = rs.getMetaData();
/*获得数据记录列数*/
columNum = rsmd.getColumnCount();
/*建立字符串数组,每个数组存储一条记录*/
String[] strRow = new String[columNum];
velNum = 0;
/*循环访问数据结果*/
while(rs.next()) {
/*把一条记录存储到一个数组中*/
for (int i=1; i<=columNum; i++) {
strRow[i-1] = rs.getString(i);
}
/*把字符串数组加到arraylist对象中*/
arrayList.add(strRow.clone());
velNum++;
}
}
else
velNum = 0;
}
catch (SQLException e) {
throw e;
}
finally {
if (rs != null) {
/*关闭数据结果集*/
rs.close();
rs = null;
}
}
return arrayList;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -