2014-03-06 34 views
0

我一直在使用XAMPP安裝了Tomcat 7.0.42。我試圖將從一個html頁面收集的數據添加到數據庫中。代碼正在編譯正確。但是我在執行中感到困惑。請給我一步一步的執行步驟。我創建了db和dsn。代碼如下。的Java Servlet與SQL 2008數據庫無法正常工作

import java.io.*; 

import javax.servlet.*; 
import javax.servlet.http.*; 
import java.sql.*; 

public class login extends HttpServlet 
{ 
    public void doPost(HttpServletRequest req, HttpServletResponse res)throws IOException, ServletException 
    { 
     String name , pass; 
     name = req.getParameter("txtuname"); 
     pass = req.getParameter("txtpass"); 

      res.setContentType("text/html"); 
      PrintWriter pw = res.getWriter(); 

     try 
     { 
      Class.forName("sun.jdbc.odbc.JdbcOdbcDriver"); 
      Connection con = DriverManager.getConnection("Jdbc:Odbc:shri_dsn"); 
      Statement stmt = con.createStatement(); 
      stmt.executeUpdate("insert into login_db(uname,password)values('"+name+"','"+pass+"')"); 
      pw.println(" Data Inserted !"); 
      con.close(); 
     } 

     catch(Exception e) 
     { 
      pw.println(" You're Lost !"); 
     } 
    } 
} 

的HTML代碼如下..

<html> 
<body> 
    <form name="login" method="post" action="../servlets/login" align="center"> 
     Username: <input type="text" name="txtuname" /> <br /> 
     Password: <input type="password" name="txtpass" /> <br /> 
      <input type="submit" name="submit" value="sumbit" /> 
    </form> 
</body> 
</html> 

我已經放置在兩個Java和HTML Tomcat內/ web應用/你好。請告訴它是否正確..以及如何執行它?

+0

這可能幫助http://www.javatpoint.com/steps-to-connect-to-the-database-in-java – ankit

+0

這是你正在連接的MySQL數據庫? –

+0

是sql server 2008 – user3386500

回答

0

我得到了解決最後。我已經設定的操作路徑是錯誤的:正確的是

<form name="login" method="post" action="http://localhost:8080/sample/servlets/login" align="center">