六狼论坛

 找回密码
 立即注册

QQ登录

只需一步,快速开始

新浪微博账号登陆

只需一步,快速开始

搜索
查看: 75|回复: 0

JAVA通用数据库访问工具类

[复制链接]

升级  39.33%

29

主题

29

主题

29

主题

秀才

Rank: 2

积分
109
 楼主| 发表于 2013-1-13 18:58:47 | 显示全部楼层 |阅读模式
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!");  } }}
您需要登录后才可以回帖 登录 | 立即注册 新浪微博账号登陆

本版积分规则

快速回复 返回顶部 返回列表