下面是我刚做的一个方法,我想把用户输入的帐号和密码是否存在于sql中来check该用户是否有登陆的权限.不懂的地方我用红色标记出来了,新手请高手门帮助 ...
String userName="";
String userP=""; public boolean userInfoLink(){
String strDBriver = "com.mysql.jdbc.Driver";
// 数据库ServiceID
String strConUrl="jdbc:mysql://127.0.0.1/user";
// 连接用户
String strUser="root";
// 连接密码
String strPass="";
Connection con = null;
Statement stmt = null;
ResultSet rs=null;
try{
Class.forName(strDBriver);
con = DriverManager.getConnection(strConUrl,strUser,strPass);
stmt = con.createStatement();
rs = stmt.executeQuery("select ? from USERINFO ");
String strSelectSql="SELECT NICKNAME FROM userinfo WHERE USERID=? AND PASSWORD=? ; ";
PreparedStatement pstmt=con.prepareStatement(strSelectSql);
pstmt.setString(1,userName);
pstmt.setString(2,userP );
//此处想用读进去的uservname和userp和sql进行连接来比较是否存在,不知道用什么方法来调用``
if(pstmt.)){
return true;
}else{
return false;
}
}catch(Exception e){
System.out.println(e);
}finally{
try{
if(rs!=null)
rs.close();
if(stmt!=null)
stmt.close();
if(con!=null)
con.close();
}catch(Exception e1){
System.out.println(e1);
}
}
}
谢谢拉.
解决方案 »
- Web项目如何实现类似在线客服聊天(一对多)
- [请教]对于人人网留言回复功能的疑问
- weblogic9.2部署JAVA web项目出错
- session.setAttribute()怎么取的是sessionid?
- 新手问struts标签设置问题,急啊!!!!!!!!!!!!!!!!!!
- 单独在tomcat环境下编译jsp的话.java文件应该放在tomcat哪个目录下呢?
- 请问,这个程序怎么只能运行一半呢?
- 有关应用框架技术,问个思路。多谢了!
- 怎么样保留输入时的格式?
- 小弟初学,各位帮我看看这段小程序错在哪了?
- JSP页面返回的问题?
- The value for the useBean class attribute com.pe.wei.note.vo.Person is invalid.
如果有记录就说明名字和密码。ID匹配了
如果rs没有找到相关记录就为false
用rs=pstmt.executeQuery()执行查询,判断结果集rs里有没有纪录就行吧
String strDBriver = "com.mysql.jdbc.Driver";
// 数据库ServiceID
String strConUrl = "jdbc:mysql://127.0.0.1/user";
// 连接用户
String strUser = "root";
// 连接密码
String strPass = "";
Connection con = null;
Statement stmt = null;
ResultSet rs = null;
try {
Class.forName(strDBriver);
con = DriverManager.getConnection(strConUrl, strUser, strPass);
String strSelectSql = "SELECT NICKNAME FROM userinfo WHERE USERID=? AND PASSWORD=? ; ";
PreparedStatement pstmt = con.prepareStatement(strSelectSql);
pstmt.setString(1, userName);
pstmt.setString(2, userP);
rs = pstmt.executeQuery(); // 查询结果
if (rs.next()) {
return true;
} else {
return false;
}
} catch (Exception e) {
System.out.println(e);
return false; // 增加了返回值
} finally {
try {
if (rs != null)
rs.close();
if (stmt != null)
stmt.close();
if (con != null)
con.close();
} catch (Exception e1) {
System.out.println(e1);
}
}
}
* 检查用户是否正确
*
* @param userName 用户名
* @param userP 密码
* @return 如果都正确,返回true,否则返回false
*/
public boolean userInfoLink(String userName, String userP) {
String strDBriver = "com.mysql.jdbc.Driver";
// 数据库ServiceID
String strConUrl = "jdbc:mysql://127.0.0.1/user";
// 连接用户
String strUser = "root";
// 连接密码
String strPass = "";
Connection con = null;
Statement stmt = null;
ResultSet rs = null;
try {
Class.forName(strDBriver);
con = DriverManager.getConnection(strConUrl, strUser, strPass);
String strSelectSql = "SELECT NICKNAME FROM userinfo WHERE USERID=? AND PASSWORD=?";
PreparedStatement pstmt = con.prepareStatement(strSelectSql);
pstmt.setString(1, userName);
pstmt.setString(2, userP);
rs = pstmt.executeQuery();
if (rs.next()) {
return true;
} else {
return false;
}
} catch (Exception e) {
System.out.println(e);
return false;
} finally {
try {
if (rs != null)
rs.close();
if (stmt != null)
stmt.close();
if (con != null)
con.close();
} catch (Exception e1) {
System.out.println(e1);
}
}
}