我正在尝试使用以下JSP代码将数据插入到我的数据库中。我创建了名为music的数据库和名为tbl_user的表。在我输入register.jsp
中的所有相关字段后,控件转到Insertdata.java
,但数据不会进入数据库。
这是我的JSP页面:
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<!-- Bootstrap core CSS -->
<link href="assets/css/bootstrap.min.css" rel="stylesheet">
<!-- Custom styles for this template -->
<link href="assets/css/signin.css" rel="stylesheet">
<link href="assets/css/login.css" rel="stylesheet">
<script src="assets/js/bootstrap.js"></script>
<title>Sign UP</title>
</head>
<body>
<div class="container">
<div class="clearfix"></div>
<div class="col-md-6 col-md-offset-3">
<div class="panel panel-default login">
<div class="panel-heading">Register Here</div>
<div class="panel-body">
<form class="form-horizontal" role="form" method="post" action="Insertdata">
<div class="form-group">
<label for="firstname" class="col-sm-3 control-label"> First Name</label>
<div class="col-sm-8">
<input type="text" class="form-control" id="firstname" name="fname" placeholder="First Name">
</div>
</div>
<div class="form-group">
<label for="lastname" class="col-sm-3 control-label"> Last Name</label>
<div class="col-sm-8">
<input type="text" class="form-control" id="lastname" name="lname" placeholder="Last Name">
</div>
</div>
<div class="form-group">
<label for="adress" class="col-sm-3 control-label">Address</label>
<div class="col-sm-8">
<input type="text" class="form-control" id="address" name="address" placeholder="Address">
</div>
</div>
<div class="form-group">
<label for="inputEmail3" class="col-sm-3 control-label">Email</label>
<div class="col-sm-8">
<input type="email" class="form-control" id="inputEmail3" name="email" placeholder="Email">
</div>
</div>
<div class="form-group">
<label for="inputPassword3" class="col-sm-3 control-label">Password</label>
<div class="col-sm-8">
<input type="password" class="form-control" id="inputPassword3" name="password" placeholder="Password">
</div>
</div>
<div class="form-group">
<div class="col-sm-offset-3 col-sm-10">
<button type="submit" class="btn btn-default"">Register</button>
</div>
</div>
</form>
</div>
</div>
</div>
</div>
</body>
</html>
这是我的数据库连接servlet DatabaseConnection.java
package mypackage;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
public class DatabaseConnection {
Connection conn;
Statement stmt;
ResultSet res;
public DatabaseConnection(){
}
public Connection setConnection(){
try{
System.out.println("sdsadasd");
Class.forName("com.mysql.jdbc.Driver");
conn=DriverManager.getConnection("jdbc:mysql://localhost:3306/music","root","");
System.out.println("Connected to the database");
}catch(Exception e){
}
return conn;
}
public ResultSet getResult(String sql,Connection conn){
this.conn=conn;
try{
stmt=conn.createStatement();
res=stmt.executeQuery(sql);
}catch(Exception e){
}
return res;
}
}
这是我的servlet,用于插入数据 Insertdata.java
package mypackage;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.Statement;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class Insertdata extends HttpServlet {
String fname,lname,address,email,password;
String query;
Connection conn;
Statement stmt;
ResultSet res;
DatabaseConnection dbconn;
protected void processRequest(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html;charset=UTF-8");
PrintWriter out = response.getWriter();
try {
dbconn=new DatabaseConnection();
//Af_Scheme_Number=request.getParameter("Af_Scheme_Number");
fname=request.getParameter("fname");
lname=request.getParameter("lname");
address=request.getParameter("address");
email=request.getParameter("email");
password=request.getParameter("password");
conn=dbconn.setConnection();
stmt=conn.createStatement();
query= "insert into tbl_user(first_name,last_name,address,email,password)"+
" values('"+fname+"','"+lname+"','"+address+"',"+email+",'"+ password+"')";
int i=stmt.executeUpdate(query);
} catch(Exception e){
System.out.println("Error");
}finally {
out.close();
}
}
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
processRequest(request, response);
}
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
processRequest(request, response);
}
@Override
public String getServletInfo() {
return "Short description";
}
}
答案 0 :(得分:0)
只是一个词汇点:类DatabaseConnection
不是servlet。但是你应该阅读(或再读一遍)关于JDBC的好教程:
executeUpdate
您应该首先编写一个Junit测试来控制您可以在servlet上下文之外成功写入数据库。只有当该部分正常工作时,才将持久性代码集成到Web应用程序中。
答案 1 :(得分:0)
删除localhost并尝试127.0.0.1 ...它对我有用
conn=DriverManager.getConnection("jdbc:mysql://localhost:3306/music","root","");