2015-06-30 54 views
0

當我運行一個簡單的程序(沒有數據庫)它運行正常,但是當我運行我的servlet編程與數據庫它doesn't.I想運行我的登錄頁面,其中註冊用戶的數據將存儲在mySQl數據庫。我在做這個程序在netbeans IDE 8.0.2中用mySQL數據庫。代碼如下:在我的servlet使用database.it編程沒有給出任何輸出,但沒有JDBC或數據庫它正在正確運行,我可以修復它嗎?

<!DOCTYPE html> <html> 
<head><title>Registration form</title>  <meta charset="UTF-8"> 
    <meta name="viewport" content="width=device-width, initial-scale=1.0"> 
</head> 
     <body> 
    <form action="Register" method="get"> 
     <p>Name:<input type="text" name="username"/></p><br/> 
     <p>Password:<input type="password" name="password"/></p><br/> 
     <p>EmailId:<input type="text" name="emailid"/></p><br/> 
     <select name="usercountry"> 
      <option>India</option> 
      <option>pakistan</option> 
      <option>USA</option> 
      <option>australia</option> 
      <option>other</option> 
     </select><br/> <br/> 
     <input type="submit" name="login"/> 
     </form> 
</body> 

Register.java

import java.io.IOException; 
import java.io.PrintWriter;  
import java.sql.Connection;  
import java.sql.DriverManager;  
import java.sql.PreparedStatement;  
import javax.servlet.ServletException;  
import javax.servlet.annotation.WebServlet;  
import javax.servlet.http.HttpServlet;  
import javax.servlet.http.HttpServletRequest;  
import javax.servlet.http.HttpServletResponse; 

@WebServlet(name="Register",urlPatterns={"/Register"}) 
public class Register extends HttpServlet { 
     protected void doGet(HttpServletRequest request, HttpServletResponse response) 
     throws ServletException, IOException { 
      response.setContentType("text/html;charset=UTF-8"); 
      PrintWriter out = response.getWriter(); 
      String n=request.getParameter("username"); 
      String p=request.getParameter("password"); 
      String e=request.getParameter("emailid"); 
      String c=request.getParameter("usercountry"); 
      try{ 
       Class.forName("com.mysql.jdbc.Driver"); 
       Connection con=DriverManager.getConnection("jdbc:mysql://localhost:3306/registeruser","root","Mysql"); 
       PreparedStatement ps=con.prepareStatement("insert into registeruser values(?,?,?,?)"); 
       ps.setString(1, n); 
       ps.setString(2, p); 
       ps.setString(3, e); 
       ps.setString(4, c); 
       int i=ps.executeUpdate(); 
       if(i>0) 
        out.print("you are successfully registered....");} 
      catch(Exception e2){ 
       System.out.print(e2); 
      } 
      out.close(); 
    } 
} 
+0

通常您的應用程序服務器(Tomcat或類似的)會在服務器日誌中向您顯示更多信息。您是否已將數據庫的JDBC驅動程序添加到lib目錄中? –

回答

0

你的代碼是正確的,唯一的要求是添加MySQL-connector.jar文件在lib文件夾中。

相關問題