本人初学jsp,以下是有问题的代码:
本人初学jsp,以下是有问题的代码:
过渡final String DBDRIVER = "com.mysql.jdbc.Driver" ;
final String DBURL = "jdbc:mysql://localhost:3306/text2"; //text2数据库名
PreparedStatement pstmt = null ;
ResultSet rs = null ;
%>
request.setCharacterEncoding("gb2312");
String username = request.getParameter("username");
String password = request.getParameter("password");
String userName="root";
String userpasswd="123456789";
String dbName="text";
String tableName="text";
String
url="jdbc:mysql://localhost:3306/"+dbName+"?user="+userName+"&password="+userpasswd+"&useUnicode=true&characterEncoding=GB2312";
Class.forName("com.mysql.jdbc.Driver").newInstance();
Connection conn =DriverManager.getConnection(url) ;
String sql = "SELECT name FROM admin WHERE name=? AND password=?" ;
boolean flag = false ;
try
{
Class.forName(DBDRIVER) ;
conn = DriverManager.getConnection(DBURL,"root","123456789") ;
pstmt = conn.prepareStatement(sql) ;
pstmt.setString(1,username) ;
pstmt.setString(2,password) ;
rs = pstmt.executeQuery() ;
if(rs.next())
{
username = rs.getString(1) ;
flag = true ;
}
rs.close() ;
pstmt.close() ;
conn.close() ;
}
catch(Exception e)
{
System.out.println(e) ;
}
%>
if(flag)
{
String name_s=null;
session.putValue("name_s",username);
response.sendRedirect("success.jsp");
//成功,传递session,转向
}
else{
out.println("");
}
%>
此代码问题是在运行网页时,输入用户名和密码,与数据库中的数据完全正确,但无法登陆界面,显示“请输入正确的用户名或密码”,请问问题出在哪里啊?求高手解决!!!
展开