2016-07-28 9 views
-5

どのようにしてmysqlに接続されたjspを取得できますか?どこが間違っている?私のindex.jspページが正しく動作しています。しかし、私は疑問が約<%@ include file = "DB conn"%>何がフォームアクションで記述されるべきであり、私のDB conn.jspファイルが実際のエラーを持っています。JSPを使用してMysqlデータベースに接続するにはどうすればよいですか?私のコードでどこが間違っていますか?

jspでエラーを取り除くにはどうすればよいですか?エラーがありますか? あなたの提案は私にとって重要です、ありがとうございます。

デシベル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()) {} 
+0

スタックトレースを表示します。 – csmckelvey

+0

'se.printStackTrace();'私は下に移動するコードでそれを見ることができます –

+1

スタックトレースの**出力**を表示します。あなたは、それがあなたのコンソールに印刷されたテキストを知っています。 – csmckelvey

答えて

0

db conn.jspでtryとcatchブロックを含めてください。あなたの問題を解決するかもしれません。

関連する問題