package db;
import java.sql.*;
import action.LoginForm;
public class DBCon {
static String url="jdbc:sqlserver://localhost:1433; DatabaseName=L";
static String user="sa";
static String password="123";
public static Connection getConn(){
Connection conn=null;
try{
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
conn= DriverManager.getConnection(url,user,password);
}catch(Exception e){
e.printStackTrace();
}
return conn;
}
public static boolean login(LoginForm loginForm){
boolean flag=false;
String sql = "select * from Member where name=? and password=?";
System.out.println("sql"+loginForm.getUserName());
Connection conn=null;
conn=getConn();
if(conn==null){
System.out.println("conn is null------------------------------------");
}
PreparedStatement pstm=null;
ResultSet rs=null;
try {
pstm=conn.prepareStatement(sql);
pstm.setString(1, loginForm.getUserName());
pstm.setString(2, loginForm.getPassWord());
rs=pstm.executeQuery();
if(rs.next()){
flag=true;
}
} catch (SQLException e) {
e.printStackTrace();
}finally{
close(conn, null, pstm, rs);
}
return flag;
}
public static void close(Connection conn,Statement st,PreparedStatement ps,ResultSet rs){
try {
if(rs!=null){
rs.close();
rs=null;
}
if(ps!=null){
ps.close();
ps=null;
}
if(st!=null){
st.close();
st=null;
}
if(conn!=null){
conn.close();
conn=null;
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
DBCon
最新推荐文章于 2021-09-15 10:01:00 发布