servlet执行SQL

/*=====SQLGatewayServlet.java=======*/
package  test;

import  java.io.*;
import  javax.servlet.*;
import  javax.servlet.http.*;
import  java.sql.*;

public  class  SQLGatewayServlet  extends  HttpServlet{

       private  Connection  connection;

       public  void  init()  throws  ServletException{
               try{
                       Class.forName("org.gjt.mm.mysql.Driver");
                       String  dbURL  =  "jdbc:mysql://localhost/murach?useUnicode=true&characterEncoding=gbk";
                       String  username  =  "root";
                       String  password  =  "";
                       connection  =  DriverManager.getConnection(dbURL,  username,  password);
               }
               catch(ClassNotFoundException  e){
                       System.out.println("找不到数据库驱动程序.");
               }
               catch(SQLException  e){
                       System.out.println("不能打开数据库连接:  "
                                                             +  e.getMessage());
               }
       }

       public  void  destroy()  {
               try{
                       connection.close();
               }
               catch(SQLException  e){
                       System.out.println("不能关闭数据库连接:  "  +  e.getMessage());
               }
       }

       public  void  doGet(HttpServletRequest  request,
                                           HttpServletResponse  response)
                                           throws  IOException,  ServletException{

               String  sqlStatement  =  request.getParameter("sqlStatement");
               String  message  =  "";

try  {
                     request.setCharacterEncoding("GB2312");
               }
               catch  (Exception  e)  {
                         e.printStackTrace();
               }

               try{
                       Statement  statement  =  connection.createStatement();
                       sqlStatement  =  sqlStatement.trim();
                       String  sqlType  =  sqlStatement.substring(0,  6);
                       if    (sqlType.equalsIgnoreCase("select")){
                               ResultSet  resultSet  =  statement.executeQuery(sqlStatement);
                               //  构造一个String,其中包含HTML表格形式的结果集数据
                               message  =  SQLUtil.getHtmlRows(resultSet);
                       }
                       else
                       {
                               int  i  =  statement.executeUpdate(sqlStatement);
                               if  (i  ==  0)  //  这是一个DDL命令
                                   message  =
                                       "<tr><td>"  +
                                           "命令执行成功."  +
                                       "</td></tr>";
                               else  //  这是一个INSERT、UPDATE或DELETE命令
                                       message  =
                                           "<tr><td>"  +
                                               "SQL命令执行成功。<br>"  +
                                               "已更改"  +  i  +  "  行。"  +
                                             "</td></tr>";
                       }
                       statement.close();
               }
               catch(SQLException  e){
                       message  =  "<tr><td>执行SQL命令时遇到错误:<br>"
                                       +  e.getMessage()  +  "</tr></td>";
               }
               message  =  new  String(message.getBytes("ISO-8859-1"),"GB2312");
               HttpSession  session  =  request.getSession();
               session.setAttribute("message",  message);
               session.setAttribute("sqlStatement",  sqlStatement);

               RequestDispatcher  dispatcher  =
                       getServletContext().getRequestDispatcher(
                               "/sql_gateway.jsp");
               dispatcher.forward(request,  response);

       }

       public  void  doPost(HttpServletRequest  request,
                                           HttpServletResponse  response)
                                           throws  IOException,  ServletException{
               doGet(request,  response);
       }
}

/*====test.jsp===========*/
<!doctype  html  public  "-//W3C//DTD  HTML  4.0  Transitional//EN">
<%  
     
     String  sqlStatement  =  (String)  session.getAttribute("sqlStatement");
     if  (sqlStatement  ==  null)  sqlStatement  =  "select  *  from  User";
     String  message  =  (String)  session.getAttribute("message");
     if  (message  ==  null)  message  =  "  ";
           message  =  new  String(message.getBytes("ISO-8859-1"),"GB2312");
%>
<html>
<head>
   <title>SQL网关</title>
</head>

<body>
<h1>SQL网关</h1>
<p>输入一个SQL命令,然后点击“执行”按钮。命令执行的结果将<br>显示在页面的下方。</p>
<form  action="SQLGatewayServlet.do"  method="post">
<b>SQL命令:</b><br>
<textarea  name="sqlStatement"  cols=60  rows=8><%=sqlStatement%></textarea><br>
<br>
<input  type="submit"  value="执行">
</form>

<p>
<b>SQL命令执行结果:</b><br>
<table  cellpadding="5"  border="1">
<%=message%>
</table>
</p>
</body>
</html>
  
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值