数据库的执行类:
import java.sql.*;/**
*
* @author user
*/
public class DBOperation {
private String strDbDriver = "sun.jdbc.odbc.JdbcOdbcDriver";
private String strConn = "jdbc:odbc:DBname";
private Connection conn = null;
private ResultSet result = null;
private String strUser = "sa";
private String strPassword = "";
/**
* constructor
*/
public DBOperation() {
try{
Class.forName( strDbDriver );
} catch( ClassNotFoundException ex ) {
System.err.println( ex.getMessage() );
}
}
/**
* constructor
* @param strUser database user
* @param strPassword user password
*/
public DBOperation(String strUser, String strPassword) {
this();
this.strUser = strUser;
this.strPassword = strPassword;
}
/**
* execute query and retrive the data
* @param strSql SQL string( select )
* @return result data
*
* @throws SQLException
*/
public ResultSet executeQuery( String strSql ) throws SQLException {
// clear result set
result = null;
// get Connection
conn = DriverManager.getConnection( strConn, strUser, strPassword );
Statement st = conn.createStatement();
// query
return st.executeQuery( strSql );
}
/**
* modify the data in database
* @param strSql SQL string ( delete, insert and update )
* @return execute result
* -1: error
* other: normal(the count of the data rows modified )
*
* @throws SQLException
*/
public int executeUpdate( String strSql ) throws SQLException {
// return value
int nRetValue = -1;
// get connection
conn = DriverManager.getConnection( strConn, strUser, strPassword );
Statement st = conn.createStatement();
// update
nRetValue = st.executeUpdate( strSql );
// close Statement and Connection
st.close();
conn.close();
return nRetValue;
}
}
import java.sql.*;/**
*
* @author user
*/
public class DBOperation {
private String strDbDriver = "sun.jdbc.odbc.JdbcOdbcDriver";
private String strConn = "jdbc:odbc:DBname";
private Connection conn = null;
private ResultSet result = null;
private String strUser = "sa";
private String strPassword = "";
/**
* constructor
*/
public DBOperation() {
try{
Class.forName( strDbDriver );
} catch( ClassNotFoundException ex ) {
System.err.println( ex.getMessage() );
}
}
/**
* constructor
* @param strUser database user
* @param strPassword user password
*/
public DBOperation(String strUser, String strPassword) {
this();
this.strUser = strUser;
this.strPassword = strPassword;
}
/**
* execute query and retrive the data
* @param strSql SQL string( select )
* @return result data
*
* @throws SQLException
*/
public ResultSet executeQuery( String strSql ) throws SQLException {
// clear result set
result = null;
// get Connection
conn = DriverManager.getConnection( strConn, strUser, strPassword );
Statement st = conn.createStatement();
// query
return st.executeQuery( strSql );
}
/**
* modify the data in database
* @param strSql SQL string ( delete, insert and update )
* @return execute result
* -1: error
* other: normal(the count of the data rows modified )
*
* @throws SQLException
*/
public int executeUpdate( String strSql ) throws SQLException {
// return value
int nRetValue = -1;
// get connection
conn = DriverManager.getConnection( strConn, strUser, strPassword );
Statement st = conn.createStatement();
// update
nRetValue = st.executeUpdate( strSql );
// close Statement and Connection
st.close();
conn.close();
return nRetValue;
}
}
解决方案 »
- 为什么中文读出来是乱码?
- java创建文件后不能删除,请高手指点!!!
- 读文件绝对路径和相对路径问题
- 变量赋值的问题,急急
- 基于debian的java1.5中文显示解决全过程 --供linuxer新手参考
- JTable里双击单元格,然后弹出一个对话框,该怎么实现呢。
- 出现unsuported method:ResultSet.getBlob是什么原因??
- 用jbuilder写会话Bean的问题
- RMI客户端运行时居然抛出ClassCastException错误
- 工作快完了出现了问题,,关于数据库的知识.愿意献出200分
- 如何在List控件里实现鼠标双击事件?
- 大家都来说说自己公司的经理呀,有助身心健康哦!
DBOperation db = new DBOperation("sa",""); try{result = db.executeQuery(SQLString);
while(result.next()){
//在这里输入你要进行操作的代码
}}
SQLString是你的sql语句,是String形的.