2013-11-01 29 views
0

我是使用MYSQL数据库在eclipse EE IDE中创建动态Web应用程序的新的java.I。我想将我的数据库连接到应用程序到目前为止,我已经创建了JSP页面以供查看。下面是我的JSP代码和Servlet连接。我无法连接到这个数据库。我的JSP页面正常工作。但我认为问题在于Servlet。另外,我还需要为Servlet创建两个Java文件,并从JSP页面获取数据。 在此先感谢。在eclipse EE IDE中使用MYSQL数据库的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

<%@ 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> 
+0

你怎么知道你不能连接?您遇到错误如果是这样,那将是实际发布错误所需的最少必需信息。您也扔掉了大量有价值的信息,至少使用e.printStackTrace()来打印异常,而不是仅打印消息。 – Gimby

+1

@Gimby:我是这个领域的新手,因此我只是在努力。 –

+0

这不能回答我的问题。你是否收到错误?如果是这样,请发布。 – Gimby

回答

1

,而无需进行详细的ErrorMessage我要说的是,你有一个编译错误的位置:

connection = DriverManager.getConnection(jdbc:mysql://localhost:3306/tool, root,); 

它应该是在l东:

connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/tool", root, psword); 

btw删除ResultSet。你不用它。

+0

我现在还没有设置任何密码。这就是我没有在那里填充任何东西的原因。 –

+0

如果该方法需要3个参数,则必须通过3个参数。你的IDE的名字是什么? –

+0

适用于Web开发人员的Eclipse Java EE IDE。 版本:开普勒版本 构建ID:20130614-0229 –

1

在本声明结尾处有一个逗号,之后没有任何值。我认为你的参数不完整。我还假设用户ID和密码需要在参数列表中的某处。

DriverManager.getConnection(jdbc:mysql:// localhost:3306/tool,root,);

哎呦 - 有人在我发布之前就回答了这个问题。猜猜我需要输入更快。

相关问题