關(guān)于javabean的數(shù)據(jù)庫連接 和分頁 代碼 效率不是最高的 ,能給初學(xué)的人一點(diǎn)幫助
數(shù)據(jù)庫連接? 和??增刪改 ?
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; /*查詢結(jié)果*/
? public Connection conn;
//設(shè)置構(gòu)造函數(shù)
?
///數(shù)據(jù)庫連接
? 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("---------設(shè)置構(gòu)造函數(shù)--------------");
? ?}
? //設(shè)置查詢參數(shù)
? public void setParam(String[] param)
? {
??? this.param=param;
? }
? //設(shè)置SQL查詢語句
? //public void setQuerystatement(String query_statement)
? //{
? ?//System.out.println(query_statement);
?? // this.query_statement=query_statement;
?? // System.out.println("---------傳入sql語句--------------");
? //}
? //設(shè)置連接參數(shù)
? 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("---------數(shù)據(jù)連接成功--------------");
? }
? /*獲取查詢結(jié)果*/
? 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;
? }
? /*對數(shù)據(jù)庫進(jìn)行增加記錄操作*/
? 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("數(shù)據(jù)插入成功");
??? }
??? catch(Exception e)
??? {
????? System.out.println(e);
????? conn.rollback();
??? }
? }
? /*對數(shù)據(jù)記錄進(jìn)行更新操作*/
? 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("數(shù)據(jù)修改成功");
???? }
???? catch(Exception e)
???? {
?????? System.out.println(e);
?????? conn.rollback();
???? }
? }
? /*刪除數(shù)據(jù)記錄*/
? 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("數(shù)據(jù)刪除成功");
??? }
??? catch(Exception e)
??? {
????? System.out.println(e);
????? conn.rollback();
??? }
?? }
}
searchtjreport.jsp代碼
?
?
?
這段代碼是判斷數(shù)據(jù)庫是否有記錄? 沒有插入,有更新
?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) ;
????????????????????????????????? }