java链接mysql数据库实现登陆验证

java链接mysql数据库实现登陆验证,第1张

//这是我以前写的核对数据库实现登陆的方法,你只看jdbc部分就好,我还特地给你加了点注释

String sql = "select username,password from account"

String user = request.getParameter("user")

String pass = request.getParameter("password")

int j = 0

Connection conn = null

PreparedStatement ps = null

ResultSet rs = null

try {

conn = JDBCTools1.getConnection()

ps = conn.prepareStatement(sql)

rs = ps.executeQuery()

//从表中查询获取所有账户的用户名&密码的ResultSet 对象

while(rs.next()){

int i = 0

String username[] = new String[10]//用户名数组

String password[] = new String[10]//密码数组

username[i] = rs.getString(1)

password[i] = rs.getString(2)

if(user.equals(username[i])&&pass.equals(password[i])){//比对

response.getWriter().print("you are welcome!")

j++

}else if(user.equals(username[i])&&!pass.equals(password[i])){

response.getWriter().println("the realy password is :"+ username[i] +","+password[i]+"\r\n")

response.getWriter().println("and you password is :"+user +","+pass+" :so the username or password may not right")

j++

}else{

continue

}

i++

}

if(j == 0){

response.getWriter().println("Your username may not be properly")

}

} catch (Exception e) {

e.printStackTrace()

}finally{

JDBCTools1.release(rs, ps, conn)

}

//这是我JDBCTools的getConnection方法

getConnection{

String driverClass = oracle.jdbc.driver.OracleDriver

String jdbcUrl = jdbc:oracle:thin:@localhost:1521:orcl

//你的数据库的用户名密码

String user = null

String password = null

// 通过反射创建Driver对象

Class.forName(driverClass)

return DriverManager.getConnection(jdbcUrl, user, password)}

//这是我JDBCTools的release方法

public static void release(ResultSet rs, Statement statement,

Connection conn) {

if (rs != null) {

try {

rs.close()

} catch (SQLException e) {

e.printStackTrace()

}

}

if (statement != null) {

try {

statement.close()

} catch (Exception e2) {

e2.printStackTrace()

}

}

if (conn != null) {

try {

conn.close()

} catch (Exception e2) {

e2.printStackTrace()

}

}

}

//验证用户和密码

@Override

public int queryUser(UserVo vo) {

// TODO Auto-generated method stub

Connection conn = null

PreparedStatement stmt = null

ResultSet rs = null

String sql = "select count(*) from user_info where username=? and password=?"

int count = 0

try{

//获取连接对像

conn = ConnTools.getConnection()

//获取stmt

stmt = conn.prepareStatement( sql )

//设置动态参数

stmt.setString( 1, vo.getUsername() )

stmt.setString( 2, vo.getPassword() )

//执行查询

rs = stmt.executeQuery()

if( rs.next() ){

count = rs.getInt( 1 )

}

}catch( Exception e ){

e.printStackTrace()

}finally{

//释放资源

ConnTools.close(conn, stmt, rs)

}

return count

}


欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/sjk/6817949.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-03-28
下一篇 2023-03-28

发表评论

登录后才能评论

评论列表(0条)

保存