日期:2014-05-18  浏览次数:20785 次

Jsp中PreparedStatement与Statement不能同时应用么?
用的是mysql+tomcat.
下面是我用来访问数据库的java类,
public   class   DBAccess   {
  final   private   String   DBDRIVER   =   "com.mysql.jdbc.Driver ";
  final   private   String   DBURL   =   "jdbc:mysql://localhost:3306/mytest? ";
  public   Connection   conn;
  public   PreparedStatement   pstmt   =   null   ;
  public   Statement   pst   =   null;
  public   ResultSet   rs   =   null;
 
  public   void   OpenDB(String   strDBName,String   strDBPassword){
  //DB连接  
  try{
Class.forName(DBDRIVER);
conn=   java.sql.DriverManager.getConnection(
DBURL   +   "user= "   +   strDBName   +   "& "   +   "password= "+   strDBPassword);
  }catch(Exception   ex){
  System.out.println(ex);
  }
  }

  public   ResultSet   ExecuteSQL(String   strSql,ArrayList   arrPara){
  //SQL   执行
  try{
  int   i   =   0;
pstmt   =   conn.prepareStatement(strSql);
pst=conn.createStatement();
if(arrPara.size()!=0){
//有条件SQL
for(i=0;   i <arrPara.size();   i++){
pstmt.setString(i+1,arrPara.get(i).toString());
}
rs   =   pstmt.executeQuery();
}
else{
//无条件SQL(全件检索)
rs   =   pst.executeQuery(strSql);
}
  }catch(Exception   ex){
System.out.println(ex);
  }
  return   rs;
  }
public   void   DBClose(){
//DB   关闭
try{
rs.close();
pst.close();
pstmt.close();
conn.close();
}catch(Exception   ex){
System.out.println(ex);
}
}
}


<%@   page   language= "java "   import= "java.sql.*,DBAccess.*,java.util.* "   %>
<html>
<head>        
        <title> `test </title>
</head>
<%!
DBAccess   dbaccess   =   new   DBAccess();
%>

<%
//String   sql   =   "SELECT   *   FROM   mytable   where   name=? "   ;
String   sql   =   "SELECT   *   FROM   mytable "   ;
ResultSet   rs   =   null   ;
try{
ArrayList   arrPara   =   new   ArrayList();
dbaccess.OpenDB( "root ", "ck ");
//arrPara.add( "ck ");
rs   =   dbaccess.ExecuteSQL(sql,arrPara);
//out.println( "aa   bb ");  

while(rs.next()){
out.println(rs.getString( "birth "));
}
}
catch(Exception   ex){
System.out.println(ex);
}
%>
<BODY>
</BODY>
<%dbaccess.DBClose();%>
</html>


我用的是mysql+tomcat.
下面是我用来访问数据库的java类,
public   class   DBAccess   {
  final   private   String   DBDRIVER   =   "com.mysql.jdbc.Driver "; <