用户注册



邮箱:

密码:

用户登录


邮箱:

密码:
记住登录一个月忘记密码?

发表随想


还能输入:200字
云代码 - java代码库

数据库使用预处理

2014-05-28 作者: 陌上之猪举报

[java]代码库

import java.sql.*;
import java.util.*;
public class Example14_9 {
   public static void main(String args[]) {
     SequencePrepareQuery query=new SequencePrepareQuery();
     String datasorce="star";
     String tableName="employee";
     query.setDatasourceName(datasorce);
     query.setTableName(tableName);
     ArrayList<StringBuffer> result=query.getQueryResult();
     for(StringBuffer str:result) {
        System.out.println(str);  
     }
  }
}

import java.sql.*;
import java.util.*;
public class PreparedQuery {
   String datasourceName="";        //数据源名
   String tableName="";            //表名
   String SQL;                     //SQL语句
   ArrayList <StringBuffer> queryResult;  //查询结果
   public PreparedQuery() {
      try{  Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
      }
      catch(ClassNotFoundException e) {
         System.out.print(e);
      }
   }
   public void setDatasourceName(String s) {
      datasourceName=s.trim();
   }
   public void setTableName(String s) {
      tableName=s.trim();
   }
   public void setSQL(String SQL) {
      this.SQL=SQL;
   }
   public ArrayList <StringBuffer> getQueryResult() {
      queryResult=new ArrayList<StringBuffer>(); 
      Connection con;
      Statement sql; 
      ResultSet rs;
      try { 
           String uri="jdbc:odbc:"+datasourceName; 
           String id="";
           String password="";
           con=DriverManager.getConnection(uri,id,password);
           DatabaseMetaData metadata=con.getMetaData();
           ResultSet rs1=metadata.getColumns(null,null,tableName,null);
           int 字段个数=0;
           while(rs1.next()) {
              字段个数++;
           }
           sql=con.prepareStatemen(SQL);
           rs=sql.executeQuery();
           while(rs.next()) {
             StringBuffer record=new StringBuffer();
             for(int k=1;k<=字段个数;k++) {
                 record.append(" "+rs.getString(k)+" ");
             }
             queryResult.add(record);
           }
           con.close();
       }
       catch(SQLException e) {
           System.out.println("请输入正确的表名"+e);
       }
       return queryResult;
   }    
}

import java.sql.*;
import java.util.*;
public class PrepareQuery {
   String datasourceName="";        //数据源名
   String tableName="";            //表名
   String SQL;                     //SQL语句
   ArrayList <StringBuffer> queryResult;  //查询结果
   public PrepareQuery() {
      try{  Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
      }
      catch(ClassNotFoundException e) {
         System.out.print(e);
      }
   }
   public void setDatasourceName(String s) {
      datasourceName=s.trim();
   }
   public void setTableName(String s) {
      tableName=s.trim();
   }
   public void setSQL(String SQL) {
      this.SQL=SQL;
   }
   public ArrayList <StringBuffer> getQueryResult() {
      queryResult=new ArrayList<StringBuffer>(); 
      Connection con;
      PreparedStatement sql; 
      ResultSet rs;
      try { 
           String uri="jdbc:odbc:"+datasourceName; 
           String id="";
           String password="";
           con=DriverManager.getConnection(uri,id,password);
           DatabaseMetaData metadata=con.getMetaData();
           ResultSet rs1=metadata.getColumns(null,null,tableName,null);
           int 字段个数=0;
           while(rs1.next()) {
              字段个数++;
           }
           sql=con.prepareStatement(SQL);
           rs=sql.executeQuery();
           while(rs.next()) {
             StringBuffer record=new StringBuffer();
             for(int k=1;k<=字段个数;k++) {
                 record.append(" "+rs.getString(k)+" ");
             }
             queryResult.add(record);
           }
           con.close();
       }
       catch(SQLException e) {
           System.out.println("请输入正确的表名"+e);
       }
       return queryResult;
   }    
}

import java.sql.*;
import java.util.*;
public class SequencePrepareQuery extends PrepareQuery {
   public ArrayList <StringBuffer> getQueryResult() {
       setSQL("SELECT * FROM "+tableName);
       return super.getQueryResult(); 
   }    
}




网友评论    (发表评论)


发表评论:

评论须知:

  • 1、评论每次加2分,每天上限为30;
  • 2、请文明用语,共同创建干净的技术交流环境;
  • 3、若被发现提交非法信息,评论将会被删除,并且给予扣分处理,严重者给予封号处理;
  • 4、请勿发布广告信息或其他无关评论,否则将会删除评论并扣分,严重者给予封号处理。


扫码下载

加载中,请稍后...

输入口令后可复制整站源码

加载中,请稍后...