Java Eclipse EE IDE 中的 Servlet 和 JSP 连接与 MYSQL 数据库
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19728457/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Servlet and JSP connection in eclipse EE IDE with MYSQL database
提问by Mike Phils
I am new to java.I am creating dynamic web Application in eclipse EE IDE with MYSQL database. I want to connect my database to app so far I have created JSP page for view. Below is my JSP code and Servlet for connection. I am not able to connect to database with this. My JSP page work fine. But I think problem is with Servlet. And also advice to I need to made the two java file one for Servlet and other for getting data from JSP page. Thanks in advance.
我是 java 的新手。我正在使用 MYSQL 数据库在 Eclipse EE IDE 中创建动态 Web 应用程序。我想将我的数据库连接到应用程序,到目前为止我已经创建了 JSP 页面以供查看。下面是我的 JSP 代码和用于连接的 Servlet。我无法用这个连接到数据库。我的 JSP 页面工作正常。但我认为问题出在 Servlet 上。还建议我需要制作两个 java 文件,一个用于 Servlet,另一个用于从 JSP 页面获取数据。提前致谢。
Servlet
小服务程序
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class NewServlet extends HttpServlet {
protected void processRequest(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
//Get user_name and pass_word from the JSP page
String toolfirst=request.getParameter("tname1");
String toolsecond=request.getParameter("tname2");
String toolvalue=request.getParameter("tvalue");
//Print the above got values in console
System.out.println("The username is" +toolfirst);
//Setting connection Parameters
String connectionparams=”jdbc:mysql://localhost:3306/tool”;
//database name
String db=”tool”;
//Mysql user name and password whichever you have given during installation
String uname=”root”
String psword=””
//Declaring classes required for Database support
Connection connection=null;
ResultSet rs;
try {
// Loading the available driver for a Database communication
Class.forName("org.gjt.mm.mysql.Driver");
//Creating a connection to the required database
connection = DriverManager.getConnection(jdbc:mysql://localhost:3306/tool, root, );
//Add the data into the database
String sql = "insert into usertable values (?,?)";
PreparedStatement prep =
connection.prepareStatement(sql);
//Setting the values which we got from JSP form
prep.setString(1, tname1);
prep.setString(2, tname2);
prep.executeUpdate();
prep.close();
}catch(Exception E){
//Any Exceptions will be caught here
System.out.println(“The error is==”+E.getMessage());
}
finally{
//After the entire execution this block will execute and the connection with database gets closed
connection.close();
}
}
JSP
JSP
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<script type="text/javascript" src="http://code.jquery.com/jquery-latest.js"></script>
<script type="text/javascript">
$(document).ready(function() {
$("#add").click(function() {
$('#mytable tbody>tr:last').clone(true).insertAfter('#mytable tbody>tr:last');
$('#mytable tbody>tr:last #name').val('');
$("#mytable tbody>tr:last").each(function() {this.reset();});
return false;
});
});
</script>
</head>
<body>
<form method="post" action="NewServlet">
<a id="add" href="javascript:void(0)">Add another Credit card</a>
<table id="mytable" width="300" border="1" cellspacing="0" cellpadding="2">
<tbody>
<tr class="person">
<td><input type="text" name="tname1" id="name" /></td>
<td><input type="button" value="name" /></td>
<td><select name="tvalue">
<option>value1</option>
<option>value2</option></select>
<td><input type="text" name="tname2" id="name" /></td>
</tr>
</tbody>
</table>
<input type="submit" value="submit" >
</form>
<a href="logout.jsp">Logout</a>
</body>
</html>
采纳答案by Stefan Beike
without having a detailed errormessage I would say that you have a compile error here:
如果没有详细的错误消息,我会说你在这里有一个编译错误:
connection = DriverManager.getConnection(jdbc:mysql://localhost:3306/tool, root, );
it should be at least:
至少应该是:
connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/tool", root, psword);
btw remove the ResultSet
. You dont use it.
btw 删除ResultSet
. 你不使用它。
回答by user2810910
You have a comma at the end of this statement with no value after it. I think your number of arguments are incomplete. I also assume the userID and password needs to be somewhere in the argument list.
此语句末尾有一个逗号,后面没有任何值。我认为您的论点数量不完整。我还假设用户 ID 和密码需要在参数列表中的某个位置。
DriverManager.getConnection(jdbc:mysql://localhost:3306/tool, root, );
DriverManager.getConnection(jdbc:mysql://localhost:3306/tool, root,);
Whoops - someone answered the question before I could post this. Guess I need to type faster.
哎呀 - 在我发布这个问题之前有人回答了这个问题。猜猜我需要更快地打字。