当前位置: 技术问答>java相关
bean的问题,请教,只有10’了。
来源: 互联网 发布时间:2015-11-21
本文导语: bean的原码: package bean;//this is the package' name import java.sql.*; public class DBOperator { private String sDBDriver = "sun.jdbc.odbc.JdbcOdbcDriver"; //the driver's name private String sConn = "jdbc:odbc:ms_sql";//cs is dsn private String sUser...
bean的原码:
package bean;//this is the package' name
import java.sql.*;
public class DBOperator
{
private String sDBDriver = "sun.jdbc.odbc.JdbcOdbcDriver";
//the driver's name
private String sConn = "jdbc:odbc:ms_sql";//cs is dsn
private String sUsername = "sa";//username
private String sPassword = "sa";//password
public Connection conn = null;
public Statement stmt = null;
public ResultSet rs = null;
public DBOperator()
{
try
{
Class.forName(sDBDriver);//load jdbc-odbc driver
}
catch (java.lang.ClassNotFoundException e) {
System.err.println("OpenDB():"+e.getMessage());
}
}
//for query
public ResultSet executeQuery(String sql)
{
try
{
conn = DriverManager.getConnection(sConn);//create connection
stmt = conn.createStatement();//create statement
rs = stmt.executeQuery(sql);//execute sql
}
catch (java.sql.SQLException e)
{
System.err.println("OpenDB.executeQuery():"+e.getMessage());
}
return rs;
}
//for insert,update,delete
public int executeUpdate(String sql)
{
int rowscount=0;
try {
conn = DriverManager.getConnection(sConn);//create connection
Statement stmt = conn.createStatement();//create statement
rowscount = stmt.executeUpdate(sql);//execute sql
}
catch (java.sql.SQLException e)
{
System.err.println("OpenDB.executeUpdate():"+e.getMessage());
}
return rowscount;
}
public void destroy()
{
try
{
stmt.close();
conn.close();
}
catch(Exception e)
{
System.err.println("DBOperate.destroy():"+e.getMessage());
}
}
}
页面的原码
package bean;//this is the package' name
import java.sql.*;
public class DBOperator
{
private String sDBDriver = "sun.jdbc.odbc.JdbcOdbcDriver";
//the driver's name
private String sConn = "jdbc:odbc:ms_sql";//cs is dsn
private String sUsername = "sa";//username
private String sPassword = "sa";//password
public Connection conn = null;
public Statement stmt = null;
public ResultSet rs = null;
public DBOperator()
{
try
{
Class.forName(sDBDriver);//load jdbc-odbc driver
}
catch (java.lang.ClassNotFoundException e) {
System.err.println("OpenDB():"+e.getMessage());
}
}
//for query
public ResultSet executeQuery(String sql)
{
try
{
conn = DriverManager.getConnection(sConn);//create connection
stmt = conn.createStatement();//create statement
rs = stmt.executeQuery(sql);//execute sql
}
catch (java.sql.SQLException e)
{
System.err.println("OpenDB.executeQuery():"+e.getMessage());
}
return rs;
}
//for insert,update,delete
public int executeUpdate(String sql)
{
int rowscount=0;
try {
conn = DriverManager.getConnection(sConn);//create connection
Statement stmt = conn.createStatement();//create statement
rowscount = stmt.executeUpdate(sql);//execute sql
}
catch (java.sql.SQLException e)
{
System.err.println("OpenDB.executeUpdate():"+e.getMessage());
}
return rowscount;
}
public void destroy()
{
try
{
stmt.close();
conn.close();
}
catch(Exception e)
{
System.err.println("DBOperate.destroy():"+e.getMessage());
}
}
}
页面的原码