当前位置:编程学习 > JAVA >>

求教:servlet无法将表单获取的数据插入数据库

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.Statement;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import we.Opp;
public class Ar extends HttpServlet
{  static Connection conn;
  public void service(HttpServletRequest rq, HttpServletResponse rp)throws ServletException, IOException
  {
    String userName = "system";
    String userPwd = "123";
   // String driverName = "oracle.jdbc.driver.OracleDriver";
    //String dburl = "jdbc:oracle:thin:@localhost:1521:WENHUA";
    rp.setContentType("text/html;charset=gbk");
    PrintWriter out = rp.getWriter();
    String no = new String(rq.getParameter("wzbh").getBytes("iso-8859-1"));
    String nm = new String(rq.getParameter("wzmc").getBytes("iso-8859-1"));
    String sx = new String(rq.getParameter("wzxh").getBytes("iso-8859-1"));
    String ag = new String(rq.getParameter("wzlx").getBytes("iso-8859-1"));
    String dp = new String(rq.getParameter("wzdw").getBytes("iso-8859-1"));
    System. out.println("Thank u ");
    try {
        if (Ab.flag == 0){
          new Ab(userName, userPwd);
          out.println("flag==0");
          }
        out.println("Connection Susful!");
       Statement stmt = Ab.conn.createStatement();
        String s1 = "insert into WUZIXX values('";
        s1 = s1 + no + "','" + nm + "','" + sx + "','" + ag + "','" + dp + "')";
        out.println(s1+"cao!");
        stmt.executeUpdate(s1);
        out.println("Thank you ca");
      } catch (java.lang.Exception stmt) {
      }
      out.println("Thank you ");
      out.println("Thank you !!");
      
  }
}



Ab类:
import java.io.PrintStream;
import java.sql.Connection;
import java.sql.DriverManager;
public class Ab {
   String driverName = "oracle.jdbc.driver.OracleDriver";
   String dburl = "jdbc:oracle:thin:@localhost:1521:WENHUA";
   static int flag = 0;
  // String username = "system";
 //   String password = "123";
  static Connection conn;
   
  public Ab(String userName, String userPwd)
  {
    try
    {
      Class.forName(this.driverName);
      conn = DriverManager.getConnection(this.dburl, userName, userPwd);
      flag = 1;
      System.out.println("Connec Successful!");
    } catch (Exception e) {
      System.out.println("connec error!");
      e.printStackTrace();
    }
  }
} servlet oracle 表单 --------------------编程问答-------------------- HttpServlet  不是有俩默认方法呢 哪去了 --------------------编程问答--------------------
引用 1 楼 songbgi 的回复:
HttpServlet  不是有俩默认方法呢 哪去了

 

这是我刚改的
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.Statement;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


public class Ar extends HttpServlet
{  static Connection conn;
  public void service(HttpServletRequest rq, HttpServletResponse rp)throws ServletException, IOException
  {
    String userName = "system";
    String userPwd = "123";
    String driverName = "oracle.jdbc.driver.OracleDriver";
    String dburl = "jdbc:oracle:thin:@localhost:1521:WENHUA";
    rp.setContentType("text/html;charset=gbk");
    PrintWriter out = rp.getWriter();
    String no = new String(rq.getParameter("wzbh").getBytes("iso-8859-1"));
    String nm = new String(rq.getParameter("wzmc").getBytes("iso-8859-1"));
    String sx = new String(rq.getParameter("wzxh").getBytes("iso-8859-1"));
    String ag = new String(rq.getParameter("wzlx").getBytes("iso-8859-1"));
    String dp = new String(rq.getParameter("wzdw").getBytes("iso-8859-1"));
    out.println(no+sx);
    try
    {
      Class.forName(driverName);
      conn = DriverManager.getConnection(dburl, userName, userPwd);
      Statement stmt = conn.createStatement();
      String s1 = "insert into WUZIXX values('";
      s1 = s1 + no + "','" + nm + "','" + sx + "','" + ag + "','" + dp + "')";
      out.println(s1+"cao!");
      stmt.executeUpdate(s1);
      out.println("Thank you ca");
     out.println("Connec是真的 Successful!");
    } catch (Exception e) {out.println("connec草啊 error!");
      //e.printStackTrace();
    }
      out.println("Thank you ");
      out.println("Thank you !!");
      
  }
}

这是浏览器返回的
1232 connec草啊 error! Thank you Thank you !!
--------------------编程问答--------------------

那 web.xml里你配置了么  你咋访问的 --------------------编程问答-------------------- 都抛异常了,还怎么持久化你的数据进入数据库?
你的异常信息捕捉一下,贴一下看看
补充:Java ,  Web 开发
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,