使用带有Netbeans的java servlet查询Oracle数据库中的数据

时间:2008-11-27 12:02:12

标签: java jsp netbeans servlets

来自index.jsp代码,

statement.executeQuery("select * from fus where tester_num like 'hf60' ") ;

示例我希望“hf60”是一个变量(userinput),其中USER必须输入/写入输入文本中的数据然后提交并获取数据,以便结果为

("select  * from fus where tester_num like 'userinput' ")

我应该在哪里插入该代码,它是在InsertServlet .java中还是在Index.jsp中?或制作另一个filename.java代码?请帮忙。感谢;)

的index.jsp

<%@ page import="java.sql.*" %>

<%  Class.forName("oracle.jdbc.driver.OracleDriver"); %>

<HTML>
<HEAD>
    <TITLE>SHIFT REPORT </TITLE>
</HEAD>

<BODY BGCOLOR=##342D7E>
    <CENTER>
    <H2><FONT COLOR="#ECD672" FACE="Verdana" >SHIFT REPORT</FONT></H2></CENTER>
<hr>
    <% 


 Connection connection=DriverManager.getConnection ("jdbc:oracle:thin:@oradev2.*****.com:1521:RPADB","shift_admin",  //
         "shift_admin"
            );

        Statement statement = connection.createStatement() ;
//**Should I input the codes here?**
        ResultSet resultset = 
            statement.executeQuery("select  * from fus where tester_num like 'hf60") ; 
    %>

    <TABLE BORDER="1" BGCOLOR="CCFFFF" width='200%' cellspacing='1' cellpadding='0'   bordercolor="black" border='1'>
        <TR>
            <TH bgcolor='#DAA520'> <font size='2'>RECORD NUMBER</TH>
            <TH bgcolor='#DAA520'><font size='2'>TESTER NUMBER</TH>
            <TH bgcolor='#DAA520'><font size='2'>DATE</TH>
            <TH bgcolor='#DAA520'><font size='2'>TIME</TH>
            <TH bgcolor='#DAA520'><font size='2'>SYSTEM TYPE</TH>
            <TH bgcolor='#DAA520'><font size='2'>PACKAGE</TH>
            <TH bgcolor='#DAA520'><font size='2'>SOCKETS</TH>
            <TH bgcolor='#DAA520'><font size='2'>VALIDATED BY</TH>
        </TR>

        <% while(resultset.next()){ %>
        <TR>
            <TD> <font size='2'><center><%= resultset.getLong(1) %></center></TD>
            <TD> <font size='2'><center><%= resultset.getString(2) %></center></TD>
            <TD> <font size='2'><center><%= resultset.getDate(3) %></center></TD>
            <TD> <font size='2'><center><%= resultset.getString(4) %></center></TD>
            <TD> <font size='2'><center><%= resultset.getString(5) %></center></TD>
            <TD> <font size='2'><center><%= resultset.getString(6) %></center></TD>
            <TD> <font size='2'><center><%= resultset.getString(7) %></center></TD>
            <TD> <font size='2'><center><%= resultset.getString(8) %></center></TD>
            </TR>

        <% } %>

    </TABLE>     
        </BODY>
</HTML>

InsertServlet.java

package fusion.shift.servlets.db;

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

public class InsertServlet extends HttpServlet {


public void init(ServletConfig config) throws ServletException { 
    super.init(config); 
} 

public void destroy() { 
} 
public boolean processRequest(HttpServletRequest request, HttpServletResponse    response) throws ServletException, IOException{


            String rec_num = request.getParameter("rec_num");
            String tester_num = request.getParameter("tester_num");
            String t_date = request.getParameter("t_date");
            String t_time = request.getParameter("t_time");
            String sys_type = request.getParameter("sys_type");
            String packages = request.getParameter("package");
            String sockets = request.getParameter("sockets");
            String sockets = request.getParameter("val");


    Connection con = null;
    Statement stmt = null;
    ResultSet rs = null;
    PreparedStatement ps = null;

    try {
                 Class.forName("oracle.jdbc.driver.OracleDriver");
    con=DriverManager.getConnection("jdbc:oracle:thin:@oradev2.*****.com:1521:RPADB","shift_admin",  //
         "shift_admin"
            );


        String sql;
                    sql = "INSERT INTO fusion_shiftrpt(RECORD_NUM, TESTER_NUM, T_DATE, T_TIME, SYSTEM_TYPE, PACKAGE, SOCKETS,VAL) VALUES (?,?,?,?,?,?,?,?)";

            ps = con.prepareStatement(sql);
        stmt = con.createStatement();

               ps.setString(1, rec_num);
             .0+  ps.setString(2, tester_num);
                             ps.setString(3, t_date);
                             ps.setString(4, t_time);
                             ps.setString(5, sys_type);
                             ps.setString(6, packages);
                             ps.setString(7, sockets);
                             ps.setString(8, val);
                             ps.executeUpdate();

        } catch (SQLException e) {
        throw new ServletException(e);
    } catch (ClassNotFoundException e) {
        throw new ServletException(e);
    } finally {
        try {
            if(rs != null)
                rs.close();
            if(stmt != null)
                stmt.close();
            if(ps != null)
                ps.close();
            if(con != null)
                con.close();
        } catch (SQLException e) {}
    }

 return(true);
 }


protected void doGet(HttpServletRequest request, HttpServletResponse response) 
    throws ServletException, IOException {
        processRequest(request,response);     

}
protected void doPost(HttpServletRequest request, HttpServletResponse response) 
    throws ServletException, IOException {
 processRequest(request,response);
 //String url = request.getRequestURI();
 //System.out.println(url);

}
 }

3 个答案:

答案 0 :(得分:4)

如果 您坚持使用此设计,我建议您使用JSTL。这提供了一组用于访问数据,控制逻辑和执行SQL访问的标记。

请参阅标准标记库和SQL tags上的Sun教程。这比将scriptlet嵌入JSP更好。也就是说,我建议这种方法(或scriplets)仅用于原型或作为非常临时的修复。

使用JSTL,您可以使用类似于:

的内容替换所有scriptlet
<sql:query var="rows" >
    select  * from fus where tester_num like ?
    <sql:param value="${param.user_input}" />
</sql:query>

<table>
   <c:forEach var="row" items="${rows}">
      <tr>
         <td>${row.column1name}</td>
         <td>${row.column2name}</td>
         <td>${row.column3name}</td>
      </tr>
   </c:forEach>
</table>

答案 1 :(得分:2)

您可以JSP访问该请求。因此,如果您要JSP这样访问:

test.jsp?q=userinput

你可以在JSP

中这样做
request.getParameter('userinput');

执行此操作时,您应将JSP代码转换为至少使用preparedStatement

PreparedStatement ps = connection.prepareStatement("select * from fus where tester_num like ?");
ps.setString(1, "%" + request.getParameter('userinput') + "%");
ResultSet resultSet = ps.executeQuery();

答案 2 :(得分:0)

正如tvanfosson所说,您应该从视图逻辑(JSP)中删除所有数据库访问代码。您应该只在JSP中显示信息,让Servlet完成所有处理。我还强烈建议您使用像Hibernate这样的OMR框架。