html表单未连接到mysql数据库

时间:2019-01-07 16:57:16

标签: java html

我正在做一个uni项目,试图将用户信息添加到我的数据库中,或者检查信息是否在数据库中,以便用户可以登录,但是当我单击“提交”按钮时,将java文件添加到我的数据库中我用来连接数据库的代码所在的便携式计算机,而不是执行代码。

这是我的html表单:

<div class="panel-heading">
   <form action="login.java" method="post">
     Username : <input type="text" name="id"><br>
     Password : <input type="password" name="password"><br>
     <input type="submit"><br>
   </form>
 </div>

这是我的java类login.java-单击html网页上的Submit按钮时,将下载此文件,而不是执行代码:

    import java.io.IOException;
    import java.io.PrintWriter;
    import java.sql.Connection;
    import java.sql.DriverManager;
    import java.sql.SQLException;
    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 auth extends HttpServlet {
        private static final long serialVersionUID = 1L;

        public auth() {
            super();
        }
        protected void doGet(HttpServletRequest request,
                             HttpServletResponse response) throws 
  ServletException, IOException {

        }

        protected void doPost(HttpServletRequest request,
                              HttpServletResponse response) throws 
  ServletException, IOException {

            try {
                Class.forName("com.mysql.jdbc.Driver");
            } catch (ClassNotFoundException e) {
                e.printStackTrace();
            }

            String id = request.getParameter("id");
            String password = request.getParameter("password");

            String sql = "select * from reg where id='" + id + "'";
            Connection conn = null;

            try {
                conn = 
 DriverManager.getConnection("jdbc:mysql://localhost/gan",
                        "root", "password");
                Statement s = conn.createStatement();

                java.sql.ResultSet rs = s.executeQuery(sql);
                String un = null;
                String pw = null;
                String name = null;

                /* Need to put some condition in case the above 
   query does not return any row, else code will throw Null Pointer 
exception */

                PrintWriter prwr1 = response.getWriter();
                if(!rs.isBeforeFirst()){
                    prwr1.write("<h1> No Such User in 
Database<h1>");
                } else {

                    /* Conditions to be executed after at least one 
  row is returned by query execution */
                    while (rs.next()) {
                        un = rs.getString("id");
                        pw = rs.getString("password");
                    }

                    PrintWriter pww = response.getWriter();

                    if (un.equalsIgnoreCase(username) && 
pw.equals(pass)) {
                        // use this or create request dispatcher
                        response.setContentType("text/html");
                        pww.write("<h1>Welcome, " + name + "</h1>");
                    } else {
                        pww.write("wrong username or password\n");
                    }
                }

            } catch (SQLException e) {
                e.printStackTrace();
            }

        }

    }

我希望它连接到我的mysql数据库,但是它只是下载了login.java文件,我之前从未见过这种情况,因此,任何帮助都将非常有用,谢谢!

1 个答案:

答案 0 :(得分:0)

使用PHP脚本连接到mysql数据库要容易得多。在您的服务器上创建一个php文件,然后复制并粘贴以下代码。您需要在php脚本中更改用户名,密码和服务器名。

<?php
$servername = "localhost";
$username = "username";
$password = "password";

// Create connection
$conn = new mysqli($servername, $username, $password);

// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
} 
echo "Connected successfully";
?>