代码mysqljsp数据库
string path = request.getcontextpath();
string basepath = request.getscheme()+://+request.getservername()+:+request.getserverport()+path+/;
%>
用户登陆 登陆 id:
登陆密码:
public static final string dbdriver = com.mysql.jdbc.driver ;
public static final string username = root; //登录的用户名
public static final string userpasswd = root; //登录mysql密码
public static final string dbname = test; //数据库名
public static final string tablename=form1; //表名
public static final string dburl = jdbc:mysql://localhost:3306/+dbname+?user=+username+&password=+userpasswd;
%>
connection conn = null ;
preparedstatement pstmt = null ;
resultset rs = null ;
boolean flag = false ; // 表示登陆成功或失败的标记
%>
string userid = request.getparameter(username) ; // 接收表单参数
string password = request.getparameter(password) ; // 接收表单参数
try{
class.forname(dbdriver) ;
conn = drivermanager.getconnection(dburl) ;
string sql = select userid,password from tuser where userid=? and password=? ;
pstmt = conn.preparestatement(sql) ;
pstmt.setstring(1,userid) ;
pstmt.setstring(2,password) ;
rs = pstmt.executequery() ;
while(rs.next()){
// 如果有内容,则此处执行,表示查询出来,合法用户
flag = true ;
}
}catch(exception e){
}finally{
try{
conn.close() ; // 连接一关闭,所有的操作都将关闭
}catch(exception e){}
}
%>
if(flag){ // 登陆成功,应该跳转到success.jsp
%>
}else{ // 登陆失败,跳转到failure.jsp
%>
}
%>