[CODE]import java.sql.*;
import java.io.*;
public class QueryBean
{
public String param[]; /*查询条件*/
public ResultSet result=null; /*查询结果*/
public Connection conn;
//设置构造函数
///数据库连接
public QueryBean() throws Exception{
System.out.println("--------^^^^^--------------");
this.setConnection("com.microsoft.jdbc.sqlserver.SQLServerDriver","jdbc:microsoft:sqlserver://127.0.0.1:1433;DatabaseName=testDatabase","sa","sa");
System.out.println("---------设置构造函数--------------");
}
//设置查询参数
public void setParam(String[] param)
{
this.param=param;
}
//设置SQL查询语句
//public void setQuerystatement(String query_statement)
//{
//System.out.println(query_statement);
// this.query_statement=query_statement;
// System.out.println("---------传入sql语句--------------");
//}
//设置连接参数
public void setConnection(String driverName,String jdbcURL,String username,String passwd) throws Exception
{
Connection conn1;
Class.forName(driverName).newInstance(); ;
// conn = DriverManager.getConnection("jdbc:microsoft:sqlserver://"+p.getProperty("dbserver")+":1433;DatabaseName="+p.getProperty("dbname")+";charset=GB2312",p.getProperty("user"),p.getProperty("password"));
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;
}
}[/CODE]