public int executeSQL(String psql,String[] param) throws ClassNotFoundException, SQLException {
Connection con=null;
PreparedStatement pstmt=null;
int num=0;
//处理sql,执行sql语句

con=getConn();
pstmt=con.prepareStatement(psql);
if(param!=null){
for(int i=0;i<param.length;i++){
pstmt.setString(i+1, param[i]);
}
}
num=pstmt.executeUpdate();

this.closeAll(con, pstmt, null);

return num;
新手别见怪 还多希望老鸟帮忙一下
最好每行都解析一下! 
 这个是 一个BaseDao 类里面的!

解决方案 »

  1.   


    //这是一个类里的方法
    public int executeSQL(String psql,String[] param) throws ClassNotFoundException, SQLException {
            //初始化操作
            Connection con=null;
            PreparedStatement pstmt=null;
            int num=0;
            //处理sql,执行sql语句
                //获得连接,这个getConn可能是这个类的或者是这个类所继承的类的方法
                con=getConn();
                //对sql语句进行预处理
                pstmt=con.prepareStatement(psql);
                //如果接收到的参数数组不为空,用接收到的参数替换前面预处理的sql语句
                if(param!=null){
                    for(int i=0;i<param.length;i++){
                        pstmt.setString(i+1, param[i]);
                    }
                }
                //执行update操作,获得操作生效的行数
                num=pstmt.executeUpdate();
                //关闭前面打开的相关资源,也是这个类或者继承的父类里的方法
                this.closeAll(con, pstmt, null);
            //返回操作生效的行数,这样调用这个方法的时候如果得到0,就说明数据库更新操作没有成功
            return num;
    }
      

  2.   


    翻译的太好了JavaAPIexecuteUpdate() 
              在此 PreparedStatement 对象中执行 SQL 语句,该语句必须是一个 SQL INSERT、UPDATE 或 DELETE 语句;或者是一个什么都不返回的 SQL 语句,比如 DDL 语句。
      

  3.   

    public int executeSQL(String psql,String[] param) throws ClassNotFoundException, SQLException {//类没有找到异常,SQL异常
            Connection con=null;//连接为空
            PreparedStatement pstmt=null;//JDBC准备对象
             resultSet rs = null;//注意此处加结果集
            int num=0;//整型的num初始为0
            //处理sql,执行sql语句
        
                con=getConn();//获取连接
                pstmt=con.prepareStatement(psql);   
           if(param!=null){//错误param.setString(1, "XX"); 
                    for(int i=0;i<param.length;i++)//循环错误
                        pstmt.setString(i+1, param[i]) ;//error                }
                }
                num=pstmt.executeUpdate();
            
                this.closeAll(con, pstmt, null);
            
            return num;
      

  4.   

    你吧两种PreparedStatment和Statment两种方式混到一起用了。。、
    PreparedStatment方式
    try{ 
    Class.forName("com.mysql.jdbc.Driver"); 
    con=DriverManager.getConnection("jdbc:mysql://localhost:3306/caiwu", "root", "admin"); 
    PreparedStatement p=con.prepareStatement("select * from ?"); 
    p.setString(1, "hotleave"); 
    System.out.println(p.toString()); 
    }catch(Exception e){ 
    e.printStackTrace(); 
    } Statment方式
    Connection con = DriverManager.getConnection("jdbc:odbc:wombat","login",   "password");   Statement stmt = con.createStatement();   ResultSet rs = stmt.executeQuery("SELECT a, b, c FROM Table1");   while (rs.next()) {   int x = rs.getInt("a");   String s = rs.getString("b");   float f = rs.getFloat("c");   } 
      

  5.   

    就是preparedStatement设置参数,传入的是一个字符数组,这样看吧。
    循环
    原始的SQL为select * from table where param1=? and param2=?i = 1 select * from table where param1=param[1]
    i = 2 select * from table where param1=param[1] and param2=param[2]