MySQL在JSP环境下的操作应用

80酷酷网    80kuku.com

  js|mysql

前提:

       将MySQL数据库的驱动放在工作目录的web-inf\lib目录下(这样才能在JSP中连结上)

       用JavaBean连接,将编译好得.class文件放在classes文件下,若文件包含package指令,则要放到

指定的目录下。

       此时,数据查询没问题,但是update,delete和insert都无效。(在SQL Server 中可行) 

       问题解决,察看JDK说明,找到Statement的方法段ResultSet executeQuery(String), int executeUpdate(String)

       修改JavaBean,添加executeUpdate方法,修改.jsp文件,将非select时指向executeUpdate,测试update,insert,

delete都成功实现

       executeQuery方法代码:

   public ResultSet executeQuery(String sqlString)
  {
   
   rs=null;
   try
   {
    
    conn=DriverManager.getConnection(connURL,userName,pwd);
    Statement stmt=conn.createStatement();
    rs=stmt.executeQuery(sqlString);
   }
   catch(SQLException ex)
   {
    System.err.println("aq.executeQuery:"+ex.getMessage());
   }
   
   return rs; 
  }

excuteUpdate方法代码:

public int executeUpdate(String sqlString)
  { 
   instructionCount=0;
   try
   {
    
    conn=DriverManager.getConnection(connURL,userName,pwd);
    Statement stmt=conn.createStatement();
    stmt.executeUpdate(sqlString);
    instructionCount=1;
   }
   catch(SQLException ex)
   {
    System.err.println("aq.executeQuery:"+ex.getMessage());
   }
   
   return instructionCount; 
  }

  新问题:在MySQL使用utf-8来支持全中文时,再次对支付串进行编解码会破坏中文的输入,

在插入和更新数据时,取消原来用GBK的new String 来编码

作者


分享到
  • 微信分享
  • 新浪微博
  • QQ好友
  • QQ空间
点击: