Eclipse使用JDBC方式连接SQLServer2008
时间:2021-07-01 10:21:17
帮助过:48人阅读
//登陆处理
public class LoginCL extends HttpServlet {
public void doGet(HttpServletRequest req,HttpServletResponse res){
Connection ct=null;
Statement sm = null;
ResultSet rs = null;
try{
//接收用户名和密码
String u=req.getParameter("username");
String p=req.getParameter("passwd");
//加载驱动,不同版本sqljdbc42.jar解压后文件夹顺序不同
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
//得到链接
ct=DriverManager.getConnection(
"jdbc:sqlserver://127.0.
0.1:
1433;databaseName
= madb","sa","
123");
sm = ct.createStatement();
rs = sm.executeQuery("
select top 1 * from users
where username
=‘"+u+"‘ and passwd
=‘"+p+"‘");
//验证
if(rs.
next()){
res.sendRedirect("wel");
}
else{
System.out.println("登陆失败");
}
}
catch(Exception ex){
ex.printStackTrace();
}
finally{
if(rs
!=null){
try {
rs.close();
} catch (SQLException e) {
// TODO Auto
-generated catch block
e.printStackTrace();
}
}
if(sm
!=null){
try {
sm.close();
} catch (SQLException e) {
// TODO Auto
-generated catch block
e.printStackTrace();
}
}
if(ct
!=null){
try {
ct.close();
} catch (SQLException e) {
// TODO Auto
-generated catch block
e.printStackTrace();
}
}
}
}
public void doPost(HttpServletRequest req,HttpServletResponse res){
doGet(req,res);
}
}
Eclipse使用JDBC方式连接SQLServer2008
标签:inf 管理 使用 rac 文件 send pack src sql