JAVA通用数据库访问工具类

import java.sql.*;

/**
* 数据库操作通用类
* 包括对[Access;SQL Server;Oracle;DB2;Informix;Sybase;PostgreSQL;Mysql]等数据库的操作
*/
public class CommonDB {
private Connection sqlCon;
private ResultSet rstSql;
private Statement stmS;
private String strCon;
private String driveName;
private boolean status;
private long rowcount;

/**
* 设置连接驱动名称,EG:
* @param drivename 驱动名称
* <p>[Access] sun.jdbc.odbc.JdbcOdbcDriver </p>
* <p>[SQL Server] com.microsoft.jdbc.sqlserver.SQLServerDriver </p>
* <p>[Oracle] oracle.jdbc.driver.OracleDriver </p>
* <p>[DB2] com.ibm.db2.jdbc.app.DB2Driver </p>
* <p>[Informix] com.informix.jdbc.IfxDriver </p>
* <p>[Sybase] com.sybase.jdbc.SybDriver </p>
* <p>[PostgreSQL] org.postgresql.Driver </p>
* <p>[Mysql] com.mysql.Driver </p>
*/
public void setDriveName(String drivename) {
this.driveName = drivename;
}

/**
* 设置连接源名称,EG:
* @param conname 数据库连接字符串
* <p>[Access] jdbc:odbc:bbs 或 jdbc:odbc:Driver={MicroSoft Access Driver (*.mdb)};DBQ=c:/smdb.mdb</p>
* <p>[SQL Server] jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=smdb</p>
* <p>[Oracle] jdbc:oracle:thin:@localhost:1521:smdb </p>
* <p>[DB2] jdbc:db2://localhost:5000/smdb </p>
* <p>[Informix] jdbc:informix-sqli://127.0.0.1:1533/smdb:INFORMIXSERVER=myserver;user=test;password=test </p>
* <p>[Sybase] jdbc:sybase:Tds:localhost:5007/smdb </p>
* <p>[PostgreSQL] jdbc:postgresql://localhost/smdb </p>
* <p>[Mysql] jdbc:mysql://127.0.0.1/smdb </p>
*/
public void setConnectName(String conname) {
this.strCon = conname;
}

/**
* 连接数据库
* @param username 帐号
* @param password 密码
* @return boolean status连接状态
*/
public boolean connect(String username, String password) {
try {
Class.forName(this.driveName).newInstance();
this.sqlCon = java.sql.DriverManager.getConnection(this.strCon, username, password);
this.status = true;
return true;
} catch (Exception e) {
this.status = false;
return false;
}
}

/**
* 执行一条SQL语句,execute sql(insert,update,delete,...)
* @param s SQL语句
* @return
*/
public boolean execute(String s) {
try {
this.stmS = this.sqlCon.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_UPDATABLE);
this.stmS.executeUpdate(s);
this.status = true;
return true;
} catch (Exception e) {
this.status = false;
return false;
}
}

/**
* 执行一条SQL查询语句
* @param s SQL查询语句
* @return
*/
public boolean query(String s) {
try {
this.rowcount = 0;
this.stmS = this.sqlCon.createStatement();
this.rstSql = this.stmS.executeQuery(s);
while (this.nextrecord()) {
this.rowcount++;
}
this.rstSql = this.stmS.executeQuery(s);
this.status = true;
return true;
} catch (Exception e) {
this.status = false;
return false;
}
}

/**
* 返回查询到的记录数
* @return
*/
public long getrowcount() {
return this.rowcount;
}

/**
* 得到某个字段的值
* @param s 字段名称
* @return
*/
public String getstring(String s) {
try {
return this.rstSql.getString(s);
} catch (Exception ex) {
return "字段不存在!";
}
}

/**
* 得到某个字段的值
* @param s 字段名称
* @return
*/
public int getint(String s) {
try {
return Integer.parseInt(this.rstSql.getString(s));
} catch (Exception e) {
return 0;
}
}

/**
* 记录指针向下移一条
* @return
*/
public boolean nextrecord() {
try {
return this.rstSql.next();
} catch (Exception e) {
return false;
}
}

/**
* 从数据库取出数据后进行汉字字符编码
* @param s 要编码的串
* @return
*/
public String readChinese(String s) {
try {
String temp = new String(s.getBytes("GB2312"), "ISO8859_1");
return temp;
} catch (Exception e) {
return s;
}
}

/**
* 得到数据库操作的状态
* @return
*/
public boolean getstatus() {
return this.status;
}

/**
* 关闭所有数据库操作对象
* @return
*/
public boolean close() {
try {
if (this.sqlCon != null) this.sqlCon.close();
if (this.rstSql != null) this.rstSql.close();
if (this.stmS != null) this.stmS.close();
this.status = true;
return false;
} catch (Exception e) {
this.status = false;
return true;
}
}

/**
* 返回结果集
* @return
*/
public ResultSet getResultLists() {
return this.rstSql;
}

public static void main(String[] agrs) throws SQLException {
CommonDB commonsql = new CommonDB();
commonsql.setDriveName("sun.jdbc.odbc.JdbcOdbcDriver");
commonsql.setConnectName("jdbc:odbc:bbs");
if (commonsql.connect("", "")) {
System.out.print("connect seccessed!");
if (commonsql.query("select * from userinfo")) {
System.out.println(commonsql.getrowcount());
while (commonsql.nextrecord()) {
System.out.println(commonsql.getstring("username"));
System.out.println(commonsql.getint("age"));
}
}
commonsql.close();
} else {
System.err.println("connect failed!");
}
}

}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值