2 qq 24157075 qq_24157075 于 2014.12.15 11:16 提问

jsp链接Mysql数据库,代码问题、求指导

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">


        <h2>用户登陆</h2>

        登陆 ID:<input type="text" name="userid"><br>
        登陆密码:<input type="password" name="password"><br>
        <input type="submit" value = "登陆">
        <input type="reset" value = "重置">


<%!
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
%>

<%
}
%>

8个回答

devmiao
devmiao   Ds   Rxr 2014.12.15 14:47

<%@page contentType="text/html" pageEncoding="UTF-8" %>



登陆操作!








        </tr>
        <tr>
            <td>登陆 ID:</td>
            <td><input type="text" name="id">
            </td>
        </tr>

        <tr>
            <td>登陆密码:</td>
            <td><input type="password" name="password">
            </td>
        </tr>

        </tr>

        <tr>
            <td colspan="2"><input type="submit" value = "登陆!">
            <td colspan="2"><input type="reset" value = "重置!">
            </td>
        </tr>
    </table>

</form>


用户登陆
devmiao
devmiao   Ds   Rxr 2014.12.15 14:47

<%@page contentType="text/html" pageEncoding="UTF-8" %>



登陆操作!








        </tr>
        <tr>
            <td>登陆 ID:</td>
            <td><input type="text" name="id">
            </td>
        </tr>

        <tr>
            <td>登陆密码:</td>
            <td><input type="password" name="password">
            </td>
        </tr>

        </tr>

        <tr>
            <td colspan="2"><input type="submit" value = "登陆!">
            <td colspan="2"><input type="reset" value = "重置!">
            </td>
        </tr>
    </table>

</form>


用户登陆
devmiao
devmiao   Ds   Rxr 2014.12.15 14:48

<%@page contentType = "text/html" pageEncoding="GBK" %>



登陆操作


登陆成功


welcome!!!!<%= request.getParameter("uname") %>

</center>


devmiao
devmiao   Ds   Rxr 2014.12.15 14:48

<%@ page contentType="text/html" pageEncoding="GBK"%>



登陆失败!!!!!!

<h2><a href= "login.jsp">点击重新登陆!!!!!!!!</a></h2>



devmiao
devmiao   Ds   Rxr 2014.12.15 14:49
guwei4037
guwei4037   Ds   Rxr 2014.12.15 11:18
save4me
save4me   Ds   Rxr 2014.12.15 13:31

public static final String DBURL = "jdbc:mysql://localhost:3306/"+dbName+"?user="+userName+"&password="+userPasswd;
改成
public static final String DBURL = "jdbc:mysql://localhost:3306/"+dbName;

然后把
conn = DriverManager.getConnection(DBURL) ;
改成
conn = DriverManager.getConnection(DBURL, userName, userPasswd );

lzp_lrp
lzp_lrp   Ds   Rxr 2015.11.04 08:40

JSP连接MySql数据库方法,可参考这个

http://jingyan.baidu.com/article/0aa2237599b89888cc0d64c0.html

Csdn user default icon
上传中...
上传图片
插入图片
准确详细的回答,更有利于被提问者采纳,从而获得C币。复制、灌水、广告等回答会被删除,是时候展现真正的技术了!