java - 如何从 JSP 页面获取登录信息?我怎样才能更正我的jsp代码。?

标签 java mysql sql-server jsp servlets

我这里错了我该如何更正

String Sql="select * from login where username='"+name+"' AND password='"+abc+"'";
c1.st.executeQuery(Sql);   
{ 
   out.println("aa");
}
catch(SQLException ex)
{

我只是错了,这是jsp中的登录页面。我只是错了,这是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;

  try {
  Class.forName("com.mysql.jdbc.Driver");
  c1 = (Connection) DriverManager.getConnection("jdbc:mysql://localhost:3306/teacher","root", "abcde");
       } 
   catch (Exception cnfe) {
   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.");
       }
    try {
    st = (Statement) c1.createStatement();
    System.out.println("Statement Created Successfully");
    } catch (SQLException se) {
    System.out.println("We got an exception while creating a statement:" + "that probably means we're no longer connected.");
    se.printStackTrace();
    }
    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+"'";

         c1.st.executeQuery(Sql);   
         { out.println("aa");
        }
        catch(SQLException ex)
         {
         out.println(ex);
         }}
       %>

错误:

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: Unable to compile class for JSP: 
      An error occurred at line: 60 in the jsp file: /db conn.jsp     
      c1.st cannot be resolved or is not a field
       57:             
       58:  String Sql="select * from login where username='"+name+"' AND password='"+abc+"'";
       59:                                   
       60:   c1.st.executeQuery(Sql);   
       61:   { 
       62:   out.println("aa");
       An error occurred at line: 63 in the jsp file: /db conn.jsp
       Syntax error, insert "}" to complete Block
       60:   c1.st.executeQuery(Sql);   
       61:    { 
       62:   out.println("aa");
       63:    }
       64:             
       65:   catch(SQLException ex)
       66:              `

最佳答案

你有语法错误。尝试使用以下语法。

<% 
 try{
    String name=request.getParameter("username");
    String abc=request.getParameter("password");

    String Sql="select * from login where username='"+name+"' AND password='"+abc+"'";

     c1.st.executeQuery(Sql);   
      out.println("aa");
    }
    catch(SQLException ex)
     {
     out.println(ex);
     }
   %>

关于java - 如何从 JSP 页面获取登录信息?我怎样才能更正我的jsp代码。?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38633634/

相关文章:

javax.persistence 不包含在 spring 数据 jpa 中?

java HashMap 'cannot be resolved'。可以执行方法但无法打印

mysql - unix 时间戳应该如何存储在 int 列中?

sql-server - SQL Server 2008 Windows 身份验证登录错误 : The login is from an untrusted domain

sql - 在SQL Server中如何从不在另一个表中的每个组中获取最大时间戳

java - java中的字节数组到文件而不覆盖,

java - normalize-space() 的目的是什么?

mysql - 在 (MM/DD/YY) 正式 mysql 中添加 1 天至今的字符串

mysql - 返回记录组的 max(field) - 不,不仅仅是 max()

c# - .NET Core 中的一对多关系数据播种