【问题标题】:How can I connected to Mysql database using JSP? where I'm wrong in my code?如何使用 JSP 连接到 Mysql 数据库?我的代码哪里错了?
【发布时间】:2016-12-03 11:15:56
【问题描述】:

如何将 jsp 连接到 mysql。我哪里错了?我的 index.jsp 页面工作正常。但是我怀疑 应该在表单操作中写入什么,并且我的 DB conn.jsp 文件有真正的错误。

如何使用 jsp 从该代码中删除错误。其中有什么错误? 您的建议对我很重要,谢谢。

db conn.jsp

 <%@page import="com.mysql.jdbc.Connection"%>
 <%@page import="com.mysql.jdbc.Statement"%>
 <%@page import="java.sql.ResultSet"%>
 <%@page import="java.sql.DriverManager"%>
 <%@page contentType="text/html" pageEncoding="UTF-8"%>
 <!DOCTYPE html>
  <html>
  <head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <title>JSP Page</title>
  </head>
   <body>
    <h1>Hello World!</h1>
   <%@ page import ="java.sql.*" %>
    <%
     Connection c1 = null;
     Statement st = null;
     ResultSet rs = null;

     Class.forName("com.mysql.jdbc.Driver");
     c1 = (Connection) DriverManager.getConnection("jdbc:mysql://localhost:3306/teacher","root", "abcde");
        {
         System.out.println("Couldn't find the driver!");
         System.out.println("Couldn't connect: print out a stack trace and exit.");
      System.out.println("We got an exception while creating a statement:" + "that probably means we're no longer connected.");

      st = (Statement) c1.createStatement();
          System.out.println("Statement Created Successfully");
             {
          System.out.println("We got an exception while creating a statement:" + "that probably means we're no longer connected.");  
           se.printStackTrace();}    //Stack Trace Added
           if (c1!= null) {
               System. out.println("Hooray! We connected to the database!");
            } else {
                System.out.println("We should never get here.");
            }}
            %>
              <%
       try{

        String name=request.getParameter("username");
        String abc=request.getParameter("password");       
        String Sql="select * from login where username='"+name+"' AND password='"+abc+"'";
        ResultSet rs=c1.st.executeQuery(Sql);   //error line
      while(rs.next())  {
      out.println("aa");}
       catch(SQLException ex)
         {
         out.println(ex);
         }
         %>

index.jsp

      <%@page contentType="text/html" pageEncoding="UTF-8"%>
      <!DOCTYPE html>
      <html>
      <%@include file="db conn.jsp" %>
      <head>
      <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
       <title>JSP Page</title>
      </head>
      <body>
      <form  method="GET " action="reg.jsp" autocomplete="on"> 
      <h1>Log in</h1> 
        <p> 
     <label for="username" class="uname" data-icon="u" >Username:</label>
     <input id="username" name="username" required="required" type="text" placeholder="Username"/>
     </p>
     <p> 
     <label for="password" class="youpasswd" data-icon="p">Password:</label>
     <input id="password" name="password" required="required" type="password" placeholder="Password" /> 
     </p>
     <p class="keeplogin"> 
     <input type="checkbox" name="loginkeeping" id="loginkeeping" value="loginkeeping" /> 
     <label for="loginkeeping">Keep me logged in</label>
     </p>
     <p class="login button"> 
     <input type="submit" value="Login" /> 
     <input type="button" onclick="location.href='reg.jsp';" value="Sign Up"/>
     </p>

    <input type="radio" name="gender" value="Teach" checked/> Teacher
    <input type="radio" name="gender" value="Student"/>Student
     </form> 
     </body>
 </html>

错误

    HTTP Status 500 -
    type Exception report

    message

    description The server encountered an internal error () that prevented it from fulfilling this request.

    exception

    org.apache.jasper.JasperException: Exception in JSP: /db conn.jsp:62

    62:             while(rs.next())  {}

【问题讨论】:

  • 显示堆栈跟踪。
  • se.printStackTrace(); 你可以在我下移的代码中看到它
  • 显示堆栈跟踪的输出。你知道,它打印到你的控制台的文本。
  • init: deps-module-jar: deps-ear-jar: deps-jar: library-inclusion-in-archive: library-inclusion-in-manifest: compile: compile-jsps: Incrementally deploying http://localhost:8081/project Completed incremental distribution of http://localhost:8081/project run-deploy: Browsing: http://localhost:8081/project run-display-browser: run: BUILD SUCCESSFUL (total time: 3 seconds)
  • 如果我不知道问题,则无法更正。没有看到错误日志我不知道问题所在。

标签: java mysql jsp tomcat netbeans


【解决方案1】:

能否在 db conn.jsp 中包含 try 和 catch 块?它可能会解决您的问题。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-07-24
    • 2011-12-21
    • 1970-01-01
    • 2013-11-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多