JDBC连接数据库工具类

package until;


import java.sql.*;
public class Dbutil {

private static final String driver="com.microsoft.sqlserver.jdbc.SQLServerDriver";
private static final String url="jdbc:sqlserver://localhost:1433;DataBaseName=userifno";
private static final String user="sa";
private static final String pwd="123.com";
private static Connection con = null;
//建立与数据库的连接
public static Connection getCon(){
try
{
Class.forName(driver);   
con = DriverManager.getConnection(url,user,pwd);

}catch(Exception e)
{
e.printStackTrace();
}

return con;
}
//关闭Connection
public static void closeCon(Connection con){
try
{
if(con!=null)
con.close();
}catch(Exception e)
{
e.printStackTrace();
}
}

//关闭ResultSet
public static void closeResultSet(ResultSet rst){
try
{
if(rst!=null){
rst.close();
rst = null;
}
}catch(Exception e)
{
e.printStackTrace();
}

}

//关闭Statement
public static void closeStatement(Statement pst){
try
{
if(pst!=null){
pst.close();
pst = null;
}
}catch(Exception e)
{
e.printStackTrace();
}

}

}



package dao;


import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;


import until.Dbutil;


import entity.Users;


public class UserInfoDao {
public boolean Login(Users u){
Connection con=null;
PreparedStatement pst=null;
boolean flag = false;
ResultSet rst = null;
con=Dbutil.getCon();
try {
pst = con.prepareStatement("select * from users where username=? and password=?");
pst.setString(1, u.getUsername());
pst.setString(2, u.getPassword());
rst = pst.executeQuery();
if (rst.next()) {
int result = rst.getInt(2);
flag = result >=1 ? true : false;
} else {
flag = false;
}


} catch (Exception e) {
e.printStackTrace();
} finally {
Dbutil.closeResultSet(rst);
Dbutil.closeStatement(pst);


}
return flag;
}
}


  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值