- 论坛徽章:
- 0
|
vista下servlet连sql server2000成功
package com.tsinghua;
import javax.servlet.http.*;
import javax.servlet.*;
import java.io.*;
import java.sql.*;
public class LoginCl extends HttpServlet
{
public static void main(String[] args)
{
// main fun
}
public void doGet(HttpServletRequest req,HttpServletResponse res)
{
this.doPost(req,res);
}
Connection ct = null;
Statement sm = null;
ResultSet rs = null;
public void doPost(HttpServletRequest req,HttpServletResponse res)
{
String u = req.getParameter("user");
String p = req.getParameter("passwd");
try
{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
ct = DriverManager.getConnection("jdbc:odbc:db","","");
sm = ct.createStatement();
rs =sm.executeQuery("select top 1 * from table1 where db_user = '" + u + "'");
if(rs.next())
{
res.sendRedirect("wel?user=" + u + "&passwd="+p);
HttpSession hs = req.getSession(true);
hs.setMaxInactiveInterval(20);
hs.setAttribute("pass","ok");
}
}
catch(Exception e)
{
e.printStackTrace();
}
finally
{
try
{
rs.close();
sm.close();
ct.close();
}
catch(Exception ee)
{
ee.printStackTrace();
}
}
}
}
本文来自ChinaUnix博客,如果查看原文请点:http://blog.chinaunix.net/u2/83308/showart_2071254.html |
|