Java EclipseEEIDE中的Servlet和JSP连接与MYSQL数据库
我是java新手,我正在EclipseEEIDE中用MYSQL数据库创建动态web应用程序。我想将我的数据库连接到应用程序,到目前为止,我已经创建了用于查看的JSP页面。下面是我的JSP代码和用于连接的Servlet。我无法用此连接到数据库。我的JSP页面运行良好。但我认为问题在于Servlet。另外,我还建议我需要制作两个java文件,一个用于Servlet,另一个用于从JSP页面获取数据。 提前谢谢 ServletJava EclipseEEIDE中的Servlet和JSP连接与MYSQL数据库,java,eclipse,jsp,servlets,web-applications,Java,Eclipse,Jsp,Servlets,Web Applications,我是java新手,我正在EclipseEEIDE中用MYSQL数据库创建动态web应用程序。我想将我的数据库连接到应用程序,到目前为止,我已经创建了用于查看的JSP页面。下面是我的JSP代码和用于连接的Servlet。我无法用此连接到数据库。我的JSP页面运行良好。但我认为问题在于Servlet。另外,我还建议我需要制作两个java文件,一个用于Servlet,另一个用于从JSP页面获取数据。 提前谢谢 Servlet import java.io.IOException; import jav
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
<%@ 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>
$(文档).ready(函数(){
$(“#添加”)。单击(函数(){
$('#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();});
返回false;
});
});
价值1
价值2
如果没有详细的错误消息,我想说您在这里有一个编译错误:
connection = DriverManager.getConnection(jdbc:mysql://localhost:3306/tool, root, );
它至少应该是:
connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/tool", root, psword);
顺便说一句,删除
ResultSet
。您不使用它。此语句末尾有一个逗号,后面没有值。我认为你的论点不完整。我还假设userID和密码需要位于参数列表中的某个位置
DriverManager.getConnection(jdbc:mysql://localhost:3306/tool,根,)
哎哟-我还没来得及发帖,就有人回答了这个问题。我想我需要打字更快。你怎么知道你无法连接?如果是这样的话,您将得到一个错误,这将是实际发布您得到的错误所需的最小信息。您还丢弃了许多有价值的信息,至少使用e.printStackTrace()打印异常,而不是只打印消息。@Gimby:我是这个领域的新手,因此我只是在尝试。这并不能回答我的问题。你是否有错误?如果是,请发布。@Gimby:我无法连接,只是我认为我的服务中有错误。我现在没有设置任何密码。这就是我没有在那里填写任何内容的原因。如果该方法需要3个参数,则必须传递3个参数。您的IDE的名称是什么?面向Web开发人员的Eclipse Java EE IDE。版本:开普勒发布版本id:20130614-0229然后您应该在方法调用下面看到一条红线:-)第一个问题您在第一个参数中没有传递字符串值。第二个问题:您传递了2个参数,其中方法需要3个参数,最后但并非最不重要的是,您放置了一个“,”而没有后续参数。一行代码中有三个错误。