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

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

//这是我以前写的核对数据库实现登陆的方法,你只看jdbc部分就好,我还特地给你加了点注释\x0d\x0aString sql = "select username,password from account"\x0d\x0aString user = request.getParameter("user")\x0d\x0aString pass = request.getParameter("password")\x0d\x0aint j = 0\x0d\x0aConnection conn = null\x0d\x0aPreparedStatement ps = null\x0d\x0aResultSet rs = null\x0d\x0atry {\x0d\x0aconn = JDBCTools1.getConnection()\x0d\x0aps = conn.prepareStatement(sql)\x0d\x0ars = ps.executeQuery()\x0d\x0a//从表中查询获取所有账户的用户名&密码的ResultSet 对象\x0d\x0awhile(rs.next()){\x0d\x0aint i = 0\x0d\x0a\x0d\x0aString username[] = new String[10]//用户名数组\x0d\x0aString password[] = new String[10]//密码数组\x0d\x0ausername[i] = rs.getString(1)\x0d\x0apassword[i] = rs.getString(2)\x0d\x0aif(user.equals(username[i])&&pass.equals(password[i])){//比对\x0d\x0aresponse.getWriter().print("you are welcome!")\x0d\x0aj++\x0d\x0a}else if(user.equals(username[i])&&!pass.equals(password[i])){\x0d\x0aresponse.getWriter().println("the realy password is :"+ username[i] +","+password[i]+"\r\n")\x0d\x0aresponse.getWriter().println("and you password is :"+user +","+pass+" :so the username or password may not right")\x0d\x0aj++\x0d\x0a}else{\x0d\x0acontinue\x0d\x0a}\x0d\x0ai++\x0d\x0a}\x0d\x0aif(j == 0){\x0d\x0aresponse.getWriter().println("Your username may not be properly")\x0d\x0a}\x0d\x0a} catch (Exception e) {\x0d\x0ae.printStackTrace()\x0d\x0a}finally{\x0d\x0aJDBCTools1.release(rs, ps, conn)\x0d\x0a}\x0d\x0a//这是我JDBCTools的getConnection方法\x0d\x0agetConnection{\x0d\x0aString driverClass = oracle.jdbc.driver.OracleDriver\x0d\x0aString jdbcUrl = jdbc:oracle:thin:@localhost:1521:orcl\x0d\x0a//你的数据库的用户名密码\x0d\x0aString user = null\x0d\x0aString password = null\x0d\x0a// 通过反射创建Driver对象\x0d\x0aClass.forName(driverClass)\x0d\x0areturn DriverManager.getConnection(jdbcUrl, user, password)}\x0d\x0a//这是我JDBCTools的release方法\x0d\x0apublic static void release(ResultSet rs, Statement statement,\x0d\x0aConnection conn) {\x0d\x0aif (rs != null) {\x0d\x0atry {\x0d\x0ars.close()\x0d\x0a} catch (SQLException e) {\x0d\x0ae.printStackTrace()\x0d\x0a}\x0d\x0a}\x0d\x0a\x0d\x0aif (statement != null) {\x0d\x0atry {\x0d\x0astatement.close()\x0d\x0a} catch (Exception e2) {\x0d\x0ae2.printStackTrace()\x0d\x0a}\x0d\x0a}\x0d\x0a\x0d\x0aif (conn != null) {\x0d\x0atry {\x0d\x0aconn.close()\x0d\x0a} catch (Exception e2) {\x0d\x0ae2.printStackTrace()\x0d\x0a}\x0d\x0a}\x0d\x0a}

//这是我以前写的核对数据库实现登陆的方法,你只看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()

}

}

}

应该是编码问题,中英文编码转换的问题。

MySQL中默认字符集的设置有四级:服务器级,数据库级,表级 。最终是字段级 的字符集设置。注意前三种均为默认设置,并不代码你的字段最终会使用这个字符集设置。所以我们建议要用show create table table 或show full fields from tableName来检查当前表中字段的字符集设置。

MySQL中关于连接环境的字符集设置有 Client端,connection, results 通过这些参数,MySQL就知道你的客户端工具用的是什么字符集,结果集应该是什么字符集。这样MySQL就会做必要的翻译,一旦这些参数有误,自然会导致字符串在转输过程中的转换错误。基本上99%的乱码由些造成。

我在用hibernate做,编码也改了,可是还有问题。最后在hibernate.cfg.xml中配置<property name="hibernate.connection.url">jdbc:mysql://localhost:3306/online?characterEncoding=UTF-8</property>就解决了


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

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

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

发表评论

登录后才能评论

评论列表(0条)

保存