Jsp和sql server数据库连接代码解释的问题
虽然觉得我太懒,但是由于小白明天下午要解释这段代码,请各位大神帮小妹解释一下,每句代码都什么意思,555,感激不尽。这应该是关于登录的一个代码。。。packagebean...
虽然觉得我太懒,但是由于小白明天下午要解释这段代码,请各位大神帮小妹解释一下,每句代码都什么意思,555,感激不尽。这应该是关于登录的一个代码。。。
package bean;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.*;
import util.*;
public class UserLoginBean {
Connection conn = null;
Statement st = null;
ResultSet rs = null;
ArrayList al = new ArrayList();
public int login(String name,String pwd){
int temp = 0;
conn = DBConn.getConn();
try {
st = conn.createStatement();
rs = st.executeQuery("select * from users where name='"+name+"'");
if(rs.next()){
String id = rs.getString("id");
String tname = rs.getString("name");
String tpwd = rs.getString("pwd");
if(pwd.equals(tpwd)){
temp = 1;
al.add(id);
al.add(tname);
al.add(tpwd);
}else{
temp = 2;
}
}else{
temp = 3;
}
} catch (SQLException e) {
e.printStackTrace();
} finally{
DBConn.close(conn,st,rs);
}
return temp;
}
public ArrayList getArrayLst(){
return al;
}
} 展开
package bean;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.*;
import util.*;
public class UserLoginBean {
Connection conn = null;
Statement st = null;
ResultSet rs = null;
ArrayList al = new ArrayList();
public int login(String name,String pwd){
int temp = 0;
conn = DBConn.getConn();
try {
st = conn.createStatement();
rs = st.executeQuery("select * from users where name='"+name+"'");
if(rs.next()){
String id = rs.getString("id");
String tname = rs.getString("name");
String tpwd = rs.getString("pwd");
if(pwd.equals(tpwd)){
temp = 1;
al.add(id);
al.add(tname);
al.add(tpwd);
}else{
temp = 2;
}
}else{
temp = 3;
}
} catch (SQLException e) {
e.printStackTrace();
} finally{
DBConn.close(conn,st,rs);
}
return temp;
}
public ArrayList getArrayLst(){
return al;
}
} 展开
1个回答
展开全部
package bean;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.*;
import util.*;
上面这些引入包。
public class UserLoginBean {
Connection conn = null;
Statement st = null;
ResultSet rs = null;
ArrayList al = new ArrayList();
定义变量及其类型.
public int login(String name,String pwd){
int temp = 0;
conn = DBConn.getConn(); //调用工具类的getConn()方法获得一个connect的链接
下面是try ……catch语句块:
try {
st = conn.createStatement(); 创建一个stament
rs = st.executeQuery("select * from users where name='"+name+"'"); 执行sql语句,获得一个result
if(rs.next()){ 用方法截取al,三个string类型的字符串
String id = rs.getString("id");
String tname = rs.getString("name");
String tpwd = rs.getString("pwd");
if(pwd.equals(tpwd)){ 对字符串pwd做判断是否相同
temp = 1; 相同执行下面的语句,并且temp的值为1 否则执行下面的两个else
al.add(id);
al.add(tname);
al.add(tpwd);
}else{
temp = 2;
}
}else{
temp = 3;
}
} catch (SQLException e) {
e.printStackTrace();
} finally{
DBConn.close(conn,st,rs);//关闭链接
}
return temp;
}
public ArrayList getArrayLst(){
return al;
}
}
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.*;
import util.*;
上面这些引入包。
public class UserLoginBean {
Connection conn = null;
Statement st = null;
ResultSet rs = null;
ArrayList al = new ArrayList();
定义变量及其类型.
public int login(String name,String pwd){
int temp = 0;
conn = DBConn.getConn(); //调用工具类的getConn()方法获得一个connect的链接
下面是try ……catch语句块:
try {
st = conn.createStatement(); 创建一个stament
rs = st.executeQuery("select * from users where name='"+name+"'"); 执行sql语句,获得一个result
if(rs.next()){ 用方法截取al,三个string类型的字符串
String id = rs.getString("id");
String tname = rs.getString("name");
String tpwd = rs.getString("pwd");
if(pwd.equals(tpwd)){ 对字符串pwd做判断是否相同
temp = 1; 相同执行下面的语句,并且temp的值为1 否则执行下面的两个else
al.add(id);
al.add(tname);
al.add(tpwd);
}else{
temp = 2;
}
}else{
temp = 3;
}
} catch (SQLException e) {
e.printStackTrace();
} finally{
DBConn.close(conn,st,rs);//关闭链接
}
return temp;
}
public ArrayList getArrayLst(){
return al;
}
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询