关于javabean的数据库连接 和分页 代码 效率不是最高的 ,能给初学的人一点帮助
扫描二维码
随时随地手机看文章
数据库连接 和 增删改
QueryBean.java
package weblistbean;
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://10.0.0.21:1433;DatabaseName=lngprs;SelectMethod=Cursor","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);
// 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);
////
// Stm = conn.createStatement();
////
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
{
//System.out.println(query_statement);
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
{
//System.out.println(query_statement);
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();
}
}
}
searchtjreport.jsp代码
这段代码是判断数据库是否有记录 没有插入,有更新
SITE_ID_ASK_NOWDATA_DATE="SELECT site_id FROM ASK_NOWDATA_DATE WHERE (site_id = '"+S_FZ_GX_TABLE_SITE_ID+"')";
Record_ASK_NOWDATA_DATE=query.getResult(SITE_ID_ASK_NOWDATA_DATE);
if(!Record_ASK_NOWDATA_DATE.isBeforeFirst()){
System.out.println("无记录");
SQL_TIME = new String(SQL_TIME.getBytes("ISO8859_1"));
sql_insert="insert into ASK_NOWDATA_DATE(site_id,rdatetime) values('"+S_FZ_GX_TABLE_SITE_ID+"','"+SQL_TIME+"')";
System.out.println("sql_update:="+sql_insert);
query.insertRecord(sql_insert) ;
}else{
//输出记录
SQL_TIME = new String(SQL_TIME.getBytes("ISO8859_1"));
sql_update="update ASK_NOWDATA_DATE set rdatetime ='"+SQL_TIME+"' where site_id = "+S_FZ_GX_TABLE_SITE_ID+"";
System.out.println("sql_update:="+sql_update);
query.updateRecord(sql_update) ;
}