以前完整的程序是这样的
[CODE]import java.sql.*;
import java.io.*;
public class QueryBean
{
//public String query_statement; //定义sql语句
public String param[]; //查询条件
public ResultSet result=null; //查询结果
public Connection conn;
//设置构造函数
///数据库连接
public QueryBean() throws Exception{
this.setConnection("com.microsoft.jdbc.sqlserver.SQLServerDriver","jdbc:microsoft:sqlserver://127.0.0.1:8080;DatabaseName=testDatabase",sa","421087");
System.out.println("---------设置构造函数--------------");
}
//设置查询参数
public void setParam(String[] param)
{
this.param=param;
}
//设置连接参数
public void setConnection(String driverName,String jdbcURL,String username,String passwd) throws Exception
{
Connection conn1;
Class.forName(driverName);
conn1=DriverManager.getConnection(jdbcURL,username,passwd);
conn1.setAutoCommit(false);
this.conn = conn1;
System.out.println("---------数据连接成功--------------");
}
//获取查询结果
public ResultSet getResult(String query_statement)
{
try
{
PreparedStatement select_stm=conn.prepareStatement(query_statement,java.sql.ResultSet.TYPE_SCROLL_INSENSITIVE,java.sql.ResultSet.CONCUR_READ_ONLY);
if (param!=null)
for(int i=0;i<param.length;i++)
select_stm.setString(i+1,param[i]);
result=select_stm.executeQuery();
}catch(Exception e){System.out.println(e);}
return result;
}
/*对数据库进行增加记录操作*/
public void insertRecord(String query_statement) throws SQLException,java.io.UnsupportedEncodingException
{
try
{
PreparedStatement insert_stm=conn.prepareStatement(query_statement);
if (param!=null)
for(int i=0;i<param.length;i++)
insert_stm.setString(i+1,param[i]);
insert_stm.executeUpdate();
insert_stm.close();
conn.commit();
System.out.println("数据插入成功");
}
catch(Exception e)
{
System.out.println(e);
conn.rollback();
}
}
//对数据记录进行更新操作
public void updateRecord(String query_statement) throws SQLException,java.io.UnsupportedEncodingException
{
try
{
PreparedStatement update_stm=conn.prepareStatement(query_statement);
if (param!=null)
for (int i=0;i<param.length;i++)
update_stm.setString(i+1,param[i]);
update_stm.executeUpdate();
update_stm.close();
conn.commit();
System.out.println("数据修改成功");
}
catch(Exception e)
{
System.out.println(e);
conn.rollback();
}
}
//删除数据记录*
public void deleteRecord(String query_statement) throws SQLException,java.io.UnsupportedEncodingException
{
try
{
PreparedStatement delete_stm=conn.prepareStatement(query_statement);
if (param!=null)
for (int i=0;i<param.length;i++)
delete_stm.setString(i+1,param[i]);
delete_stm.executeUpdate();
delete_stm.close();
conn.commit();
System.out.println("数据删除成功");
}
catch(Exception e)
{
System.out.println(e);
conn.rollback();
}
}
} [/CODE]
编译这段代码的时候要用:javac -encoding UTF-8 XX.java