不好意思改动了一下
package sql;import java.sql.*;
import java.lang.*;
import java.io.*;
import java.util.*;
import sun.io.*;/**
* 数据库操作通用类
* 包括对[Access;SQL Server;Oracle;DB2;Informix;Sybase;PostgreSQL;Mysql....]等数据库的操作
* @see
* <pre>
* EG:对Access数据库进行操作
* table: userinfo
* field: 1.username
* 2.age
*
* public static void main(String[] agrs) throws SQLException{
* CommonSQL commonsql = new CommonSQL();
* 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!");
* }
* }
* </pre>
*/
public class CommonSQL{
private Connection sqlCon;
private ResultSet rstSql;
private Statement stmS;
private String strCon;
private String driveName;
private String strSql;
private boolean status;
private long rowcount; /**
* 设置连接驱动名称
* @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] org.gjt.mm.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:/mhj.mdb</p>
* <p>[SQL Server] jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=pubs</p>
* <p>[Oracle] jdbc:oracle:thin:@localhost:1521:mhj_db </p>
* <p>[DB2] jdbc:db2://localhost:5000/mhj_db </p>
* <p>[Informix] jdbc:informix-sqli://127.0.0.1:1533/mhj_db:INFORMIXSERVER=myserver;user=mhj;password=123 </p>
* <p>[Sybase] jdbc:sybase:Tds:localhost:5007/mhj_db </p>
* <p>[PostgreSQL] jdbc:postgresql://localhost/mhj_db </p>
* <p>[Mysql] jdbc:mysql://127.0.0.1/mhj_db </p>
*/
public void setConnectName(String conname){
this.strCon = conname;
} /**
* 连接数据库
* @param username 帐号
* @param password 密码
* @return
*/
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"),"8859_1");
return temp;
} catch(Exception e){
return s;
}
} /**
* 在放入数据库前进行汉字字符编码
* @param s 要编码的串
* @return
*/
public static String writeChinese(String s){
char[] orig =s.toCharArray();
byte[] dest =new byte[orig.length];
for(int i=0;i<orig.length;i++)
dest[i] =(byte)(orig[i]&0xFF);
try{
ByteToCharConverter toChar =ByteToCharConverter.getConverter("gb2312");
return new String(toChar.convertAll(dest));
}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 getRs(){
return this.rstSql;
}/*
public static void main(String[] agrs) throws SQLException{
CommonSQL commonsql = new CommonSQL();
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!");
}
}
*/
}
package sql;import java.sql.*;
import java.lang.*;
import java.io.*;
import java.util.*;
import sun.io.*;/**
* 数据库操作通用类
* 包括对[Access;SQL Server;Oracle;DB2;Informix;Sybase;PostgreSQL;Mysql....]等数据库的操作
* @see
* <pre>
* EG:对Access数据库进行操作
* table: userinfo
* field: 1.username
* 2.age
*
* public static void main(String[] agrs) throws SQLException{
* CommonSQL commonsql = new CommonSQL();
* 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!");
* }
* }
* </pre>
*/
public class CommonSQL{
private Connection sqlCon;
private ResultSet rstSql;
private Statement stmS;
private String strCon;
private String driveName;
private String strSql;
private boolean status;
private long rowcount; /**
* 设置连接驱动名称
* @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] org.gjt.mm.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:/mhj.mdb</p>
* <p>[SQL Server] jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=pubs</p>
* <p>[Oracle] jdbc:oracle:thin:@localhost:1521:mhj_db </p>
* <p>[DB2] jdbc:db2://localhost:5000/mhj_db </p>
* <p>[Informix] jdbc:informix-sqli://127.0.0.1:1533/mhj_db:INFORMIXSERVER=myserver;user=mhj;password=123 </p>
* <p>[Sybase] jdbc:sybase:Tds:localhost:5007/mhj_db </p>
* <p>[PostgreSQL] jdbc:postgresql://localhost/mhj_db </p>
* <p>[Mysql] jdbc:mysql://127.0.0.1/mhj_db </p>
*/
public void setConnectName(String conname){
this.strCon = conname;
} /**
* 连接数据库
* @param username 帐号
* @param password 密码
* @return
*/
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"),"8859_1");
return temp;
} catch(Exception e){
return s;
}
} /**
* 在放入数据库前进行汉字字符编码
* @param s 要编码的串
* @return
*/
public static String writeChinese(String s){
char[] orig =s.toCharArray();
byte[] dest =new byte[orig.length];
for(int i=0;i<orig.length;i++)
dest[i] =(byte)(orig[i]&0xFF);
try{
ByteToCharConverter toChar =ByteToCharConverter.getConverter("gb2312");
return new String(toChar.convertAll(dest));
}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 getRs(){
return this.rstSql;
}/*
public static void main(String[] agrs) throws SQLException{
CommonSQL commonsql = new CommonSQL();
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!");
}
}
*/
}
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货