SimpleDBConnect.java(from 1292.net)修正版(0分)

  • 主题发起人 主题发起人 xiaoboliu
  • 开始时间 开始时间
X

xiaoboliu

Unregistered / Unconfirmed
GUEST, unregistred user!
SimpleDBConnect.java(from 1292.net)修正版
liuxiaobo 2001-09-26
/**
* Title: SimpleDBConnect.java
* Copyright: Copyright (c) 2001
* @author liuxiaobo
* @author xiaoboliu@cmmail.com
* @version 1.1
*/
package justforum.database;
import java.sql.*;

public class SimpleDBConnect {
private String driver="org.gjt.mm.mysql.Driver";
private String connectString="jdbc:mysql://localhost/forum";
private String user="root";
private String password="";
private Connection conn = null;

public void setParam(String driver,String connectString,String user,String password) {
this.driver=driver;
this.connectString=connectString;
this.user=user;
this.password=password;
}
public Connection getConnection() throws ClassNotFoundException,SQLException {
Class.forName(driver);
conn = DriverManager.getConnection(connectString,user,password);

return conn;
}
public void close() throws Exception {
conn.close();
conn = null;
}
}
 
最好是把连接各个版本的Dbconnect都写出来,
我只能提供一个for sybase11.5的。
/* 注意:pageage的名字必须为目录的名字 文件的名字必须为public class的名字 */
package Bank;
import java.sql.*;
public class DBconn
{String DBDriver="sun.jdbc.odbc.JdbcOdbcDriver";
String ConnStr="jdbc:odbc:sybdsn";
String MyUser="sa";
String MyPassword="mypass";
Connection conn = null;
ResultSet rs = null;
public DBconn()
{try
{Class.forName(DBDriver);
}
catch(java.lang.ClassNotFoundException e)
{System.err.println("DBconn (): " + e.getMessage());
}
}
public ResultSet executeQuery(String sql)
{
rs = null;
try
{conn = DriverManager.getConnection(ConnStr,MyUser,MyPassword);
Statement stmt = conn.createStatement();
rs = stmt.executeQuery(sql);
}
catch(SQLException ex)
{System.err.println("aq.executeQuery:"+ex.getMessage());
}
return rs;
}
public void executeInsert(String sql)
{
try
{
conn=DriverManager.getConnection(ConnStr,MyUser,MyPassword);
Statement stmt = conn.createStatement();
stmt.executeUpdate(sql);
}
catch(SQLException ex)
{
System.err.println("error inert date:"+ex.getMessage());
}
}
public boolean executeDelete(String sql)
{
boolean flag=false;
try
{
conn=DriverManager.getConnection(ConnStr,MyUser,MyPassword);
Statement stmt = conn.createStatement();
stmt.executeUpdate(sql);
}
catch(SQLException ex)
{
System.err.println("delete data error:"+ex.getMessage());
}
return flag;
}
}
 
接受答案了.
 
后退
顶部