html文本框连接数据库失败,html表单未连接到mysql数据库
我正在做一個uni項目,試圖將用戶信息添加到我的數據庫中,或者檢查信息是否在數據庫中,以便用戶可以登錄,但是當我單擊“提交”按鈕時,將java文件添加到我的數據庫中我用來連接數據庫的代碼所在的便攜式計算機,而不是執行代碼。
這是我的html表單:
Username :
Password :
這是我的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("
No Such User in
Database
");
} 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("
Welcome, " + name + "
");} else {
pww.write("wrong username or password\n");
}
}
} catch (SQLException e) {
e.printStackTrace();
}
}
}
我希望它連接到我的mysql數據庫,但是它只是下載了login.java文件,我之前從未見過這種情況,因此,任何幫助都將非常有用,謝謝!
總結
以上是生活随笔為你收集整理的html文本框连接数据库失败,html表单未连接到mysql数据库的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: python的数据库应用,Python数
- 下一篇: js mysql json字符串转数组中