MySQL在JSP环境下的操作应用

博客讲述了在JSP中连接MySQL和SQL Server数据库的相关问题及解决办法。起初在JSP中连接MySQL数据库,update、delete和insert操作无效,通过修改JavaBean添加executeUpdate方法得以解决。还提到在MySQL使用utf - 8时,插入和更新数据取消用GBK的new String编码以避免破坏中文输入。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

前提:

       将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 来编码

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值