| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 686 人关注过本帖
标题:Java连接MySQL的问题
只看楼主 加入收藏
游走之客
Rank: 1
等 级:新手上路
帖 子:1
专家分:0
注 册:2010-4-19
结帖率:0
收藏
已结贴  问题点数:20 回复次数:3 
Java连接MySQL的问题
首次使用Java连接MySQL做一个登录的功能 查询的user总是空 不知道为什么 求教
这里是BaseDao
package dao;
import java.sql.*;
public class BaseDao {
  private final static String DRIVER = "com.mysql.jdbc.Driver";
     private final static String URL="jdbc:mysql://localhost:3306/user";
     private final static String DBNAME = "root";
     private final static String DBPASS = "sa";
     /**
      * 得 到数据库的链接
      * @return
      * @throws ClassNotFoundException
      * @throws SQLException
      */
     public Connection getConn()throws ClassNotFoundException ,SQLException
     {
     Class.forName(DRIVER); // 注册驱动
     Connection conn = DriverManager.getConnection(URL, DBNAME, DBPASS) ; //
     return conn; // 返回连接
     }
     /**
      * 释放资源
      * @param conn
      * @param pstmt
      * @param rs
      */
     public void closeAll(Connection conn, PreparedStatement pstmt ,ResultSet rs)
     {
      if(rs!=null)
      {
       try{rs.close();}catch(SQLException e){}
      }
      if(pstmt!=null)
      {
       try{pstmt.close();}catch(SQLException e){}
      }
      if(conn!=null)
      {
       try{conn.close();}catch(SQLException e){}
      }
     }
}
这 里是查询数据库的方法
public class UserDaoImpl extends BaseDao implements UserDao {

    private Connection conn = null;
    private PreparedStatement pstmt = null;
    private ResultSet rs = null;
    private User user = null;
    public User login(String userName) {
        // TODO Auto-generated method stub
             String sql ="select * from User where Name=?";
             try {
                conn = this .getConn();
                pstmt = conn.prepareStatement(sql);
                pstmt.setString(1, userName);
                rs = pstmt.executeQuery();
                while(rs.next())
                {
                     user = new User();
                    user.setId(rs.getInt("id"));
                    user.setName(rs.getString("Name"));
                    user.setPassword(rs.getString("Password"));
                }
               
             } catch (ClassNotFoundException e) {
                 e.printStackTrace();
             } catch (SQLException e) {
                 e.printStackTrace();
             } finally{
                 this.closeAll(conn, pstmt, rs);
             }
        return user;
    }

在 JSP页面中
<%
request.setCharacterEncoding("GBK");
String name = request.getParameter("userName");
String pwd = request.getParameter("pwd");
UserBiz userBiz = new UserBizImpl();
User user= userBiz.login(name);
System.out.println(user);
int num = userBiz.login(user.getId());
System.out.println(num);
if(num!=0 && user!=null && user.getPassword().equals(pwd))
{
    response.sendRedirect("success.jsp");
}else
{
     response.sendRedirect("error.jsp");
}
%>

[ 本帖最后由 游走之客 于 2010-4-19 21:13 编辑 ]
搜索更多相关主题的帖子: MySQL Java 
2010-04-19 21:11
水心
Rank: 2
等 级:论坛游民
帖 子:3
专家分:10
注 册:2010-4-22
收藏
得分:10 
UserBiz userBiz = new UserBizImpl();   
2010-04-22 12:00
ghjsmzy
Rank: 7Rank: 7Rank: 7
等 级:黑侠
威 望:1
帖 子:187
专家分:573
注 册:2009-6-17
收藏
得分:10 
UserBiz userBiz = new UserBizImpl(); User user= userBiz.login(name);
是 UserDaoImpl userDI = new UserDaoImpl();  User user= userDI.login(name);
前面没有才会空的吧
2010-04-22 12:28
水心
Rank: 2
等 级:论坛游民
帖 子:3
专家分:10
注 册:2010-4-22
收藏
得分:0 
你试试
2010-04-22 13:04
快速回复:Java连接MySQL的问题
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.015810 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved