qq_33002885
如辇
采纳率50%
2016-10-05 10:47 阅读 1.5k

servlet连接mysql数据库问题

10
            本人初学web组件,很多基础不会,望各位大神不要鄙视~~~~

-----login.java----

package com.tsinghua;

import javax.servlet.http.*;

import java.io.*;

public class Login extends HttpServlet{

public void doGet(HttpServletRequest req,HttpServletResponse res){

    //业务逻辑

    try{

        //接受用户名和密码
        String u=req.getParameter("usrename");
        String p=req.getParameter("passwd");

        //中文乱码
        res.setContentType("text/html;charset=gbk");

        PrintWriter pw=res.getWriter();

        pw.println("<html>");
        pw.println("<body>");
        pw.println("<form action=logincl method=post>");
        pw.println("用户名:<input type=text name=username><br>");
        pw.println("密码:<input type=password name=passwd><br>");
        pw.println("<input type=submit value=Login><br>");
        pw.println("</form>");
        pw.println("</body>");
        pw.println("</html>");


        }
        catch(Exception ex){

            ex.printStackTrace();

            }
    }

public void doPost(HttpServletRequest req,HttpServletResponse res){

    this.doGet(req,res);
    }
}

----loginCl.java----------

package com.tsinghua;

import javax.servlet.http.*;
import java.io.*;
import java.sql.*;

public class LoginCl extends HttpServlet{

public void doGet(HttpServletRequest req,HttpServletResponse res){

    Connection ct=null;
    Statement sm=null;
    ResultSet rs=null;
    String host="127.0.0.1:3306";

    try{

        String u=req.getParameter("username");
        String p=req.getParameter("passwd");

        PrintWriter pw=res.getWriter();

        Class.forName("com.mysql.jdbc.Driver");
        ct=DriverManager.getConnection("jdbc:mysql://"+host+"/login","root","2535663");
        sm=ct.createStatement();
        rs=sm.executeQuery("select top 1 * from users where username='"+u+"'and passwd='"+p+"'");

        pw.println("连接成功!");

        if(rs.next()){
            //合法
            res.sendRedirect("welcome");

            }else{
                //不合法
                res.sendRedirect("login");
                }

        HttpSession hs=req.getSession(true);

        hs.setMaxInactiveInterval(30);

        hs.setAttribute("uname",u);

        }
        catch(SQLException e)
        {
            e.printStackTrace();
        }
        catch(ClassNotFoundException e)
        {
            e.printStackTrace();
        }
        catch(Exception ex){
            ex.printStackTrace();
            }
        finally{

            try {

                if(rs!=null){

                        rs.close();
                    }
                    if(sm!=null){

                        sm.close();
                    }
                    if(ct!=null){

                        ct.close();
                    }
            }catch (Exception ex) {

                ex.printStackTrace();
            }
    }
    }

public void doPost(HttpServletRequest req,HttpServletResponse res){

    this.doGet(req,res);
    }

}

------wel.java-------
package com.tsinghua;

import javax.servlet.http.*;
import java.io.*;
import java.sql.*;

public class wel extends HttpServlet{

public void doGet(HttpServletRequest req,HttpServletResponse res){

    Connection ct=null;
    Statement sm=null;
    ResultSet rs=null;
    String host="127.0.0.1:3306";


    try{
        String u=req.getParameter("username");
        String p=req.getParameter("passwd");




        //从session中得到用户名
        HttpSession hs=req.getSession(true);
        String myName=(String)hs.getAttribute("uname");
        String name="";
        String passwd="";
        if(myName==null){
            res.sendRedirect("login");
            return ;

            }


        //解决中文乱码
        res.setCharacterEncoding("gbk");

        PrintWriter pw=res.getWriter();
        pw.println("<body bgcolor=#CED3FF>");

        pw.println("<img src=imgs/1.GIF>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;欢迎您:"+myName+"<img src=imgs/me.gif><hr><center>");

        pw.println("<h1>管理用户</h1>");

        //做个超链接 
        pw.println("<br><a href=login>返回重新登录</a>");

        }catch(Exception ex){
            ex.printStackTrace();
            }

}
public void doPost(HttpServletRequest req,HttpServletResponse res){

    this.doGet(req,res);
    }

}


----------------------------sql文件------------------------------------


drop database if exists login;

create database login;

use login;

drop table if exists users;
create table users (
username varchar(20),
passwd varchar(20),
email varchar(30),
grade int);

insert into users values
('admin','admin','admin@sohu.com',1);

insert into users values
('shunping','shunping','shunping@sohu.com', 1);

insert into users values
('tester1','tester1','tester1@sohu.com',5);

insert into users values
('tester2','tester2','tester2@sohu.com',5);

insert into users values
('tester3','tester3','tester3@sohu.com',5);

insert into users values
('tester4','tester4','tester4@sohu.com',5);

insert into users values
('tester5','tester5','tester5@sohu.com',5);

insert into users values
('tester6','tester6','tester6@sohu.com',5);

insert into users values
('tester7','tester7','tester7@sohu.com',5);

insert into users values
('tester8','tester8','tester8@sohu.com',5);

insert into users values
('tester9','tester9','tester9@sohu.com',5);

insert into users values('tester10','tester10','tester10@sohu.com',5);

insert into users values('tester11','tester11','tester11@sohu.com',5);

insert into users values('tester12','tester12','tester12@sohu.com',5);

insert into users values('tester13','tester13','tester13@sohu.com',5);

insert into users values('tester14','tester14','tester14@sohu.com',5);

select * from users;

------------------------------web.xml--------------------------------

<?xml version="1.0" encoding="UTF-8"?>

xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee
http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
version="3.1"
metadata-complete="true">

Welcome to Tomcat

Welcome to Tomcat

<servlet>
    <servlet-name>HelloGen</servlet-name>
    <servlet-class>com.tsinghua.HelloGen</servlet-class>
</servlet>

<servlet-mapping>
    <servlet-name>HelloGen</servlet-name>
    <url-pattern>/sp</url-pattern>
</servlet-mapping>

<servlet>
    <servlet-name>helloHttp</servlet-name>
    <servlet-class>com.tsinghua.helloHttp</servlet-class>
</servlet>

<servlet-mapping>
    <servlet-name>helloHttp</servlet-name>
    <url-pattern>/sh</url-pattern>
</servlet-mapping>

<servlet>
    <servlet-name>Login</servlet-name>
    <servlet-class>com.tsinghua.Login</servlet-class>
</servlet>

<servlet-mapping>
    <servlet-name>Login</servlet-name>
    <url-pattern>/login</url-pattern>
</servlet-mapping>

<servlet>
    <servlet-name>LoginCl</servlet-name>
    <servlet-class>com.tsinghua.LoginCl</servlet-class>
</servlet>

<servlet-mapping>
    <servlet-name>LoginCl</servlet-name>
    <url-pattern>/logincl</url-pattern>
</servlet-mapping>

<servlet>
    <servlet-name>wel</servlet-name>
    <servlet-class>com.tsinghua.wel</servlet-class>
</servlet>

<servlet-mapping>
    <servlet-name>wel</servlet-name>
    <url-pattern>/welcome</url-pattern>
</servlet-mapping>

-----------------------------------文件位置-------------------------------------


mysql的jar包我也引了,然后这是sql文件位置。mysql运行没有问题。

然后文件我调试了,发现就是输入帐户名和密码后,离开登陆页面,进入登陆验证界面,进不去欢迎界面,调试后发现就是数据库连接不上。

各位大神帮帮忙,这个问题困扰很久了。。。

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享

6条回答 默认 最新

  • blueair_ren 努力的人儿 2016-10-05 10:53

    首先你看一下你的数据库名是否正确?

    点赞 评论 复制链接分享
  • morgerton 白萝卜blb 2016-10-05 11:00

    pw.println("连接成功!");

        if(rs.next()){
            //合法
            res.sendRedirect("welcome");
    
            }else{
                //不合法
                res.sendRedirect("login");
                }
    
                        从这一点开始调试吧,自己定义点自己能看到的 log看看是哪一点出问题了。
    
    点赞 评论 复制链接分享
  • waterhexuan 编天码地 2016-10-05 11:14

    你那 端口 不可能是 3306 啊 3306 一般是 mysql 数据库的服务端口 你是直接 连接sql 文件的 应该把3306 改成 你 sevlet 服务端口

    点赞 评论 复制链接分享
  • lyl18293847096 lyl18293847096 2016-10-07 03:15

    报什么错,数据库密码和用户名正确吗

    点赞 评论 复制链接分享
  • bashenmail bashenmail 2016-10-08 02:31

    top 1 * 是啥意思? 还不如select count(1) 呢 ,数字大于0 表示存在

    点赞 评论 复制链接分享
  • quasimodo_es 「已注销」 2016-10-08 09:57

    把异常贴出来,看一下就知道了

    点赞 评论 复制链接分享

相关推荐