使用jsp和servlet以及mysql的登录示例

时间:2013-09-05 05:06:49

标签: mysql jsp servlets

我有一个登录示例,它已经有一个sql表。这是我的用户表,它获取有关用户的一般信息。现在我想在我的数据库中插入另一个表 - 详细信息,然后将这两个表连接在一起。 我已经为我的详细信息表创建了一个不同的POJO类,以及我已经为用户表创建的那个。

package com.shreya.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;
import com.shreya.model.User;
import com.shreya.util.DbUtil;

public class UserDao {

private Connection connection;

public UserDao() {
    connection = DbUtil.getConnection();
}

public void addUser(User user) {
    try {
        PreparedStatement preparedStatement = connection
                .prepareStatement("insert into     users(firstname,lastname,dob,email)       values (?, ?, ?, ? )");
        // Parameters start with 1
        preparedStatement.setString(1, user.getFirstName());
        preparedStatement.setString(2, user.getLastName());
        preparedStatement.setDate(3, new java.sql.Date(user.getDob().getTime()));
        preparedStatement.setString(4, user.getEmail());
        preparedStatement.executeUpdate();

    } catch (SQLException e) {
        e.printStackTrace();
    }
}

public void deleteUser(int userId) {
    try {
        PreparedStatement preparedStatement = connection
                .prepareStatement("delete from users where userid=?");
        // Parameters start with 1
        preparedStatement.setInt(1, userId);
        preparedStatement.executeUpdate();

    } catch (SQLException e) {
        e.printStackTrace();
    }
}

public void updateUser(User user) {
    try {
        PreparedStatement preparedStatement = connection
                .prepareStatement("update users set firstname=?, lastname=?, dob=?, email=?" +
                        "where userid=?");
        // Parameters start with 1
        preparedStatement.setString(1, user.getFirstName());
        preparedStatement.setString(2, user.getLastName());
        preparedStatement.setDate(3, new java.sql.Date(user.getDob().getTime()));
        preparedStatement.setString(4, user.getEmail());
        preparedStatement.setInt(5, user.getUserid());
        preparedStatement.executeUpdate();

    } catch (SQLException e) {
        e.printStackTrace();
    }
}

public List<User> getAllUsers() {
    List<User> users = new ArrayList<User>();
    try {
        Statement statement = connection.createStatement();
        ResultSet rs = statement.executeQuery("select * from users");
        while (rs.next()) {
            User user = new User();
            user.setUserid(rs.getInt("userid"));
            user.setFirstName(rs.getString("firstname"));
            user.setLastName(rs.getString("lastname"));
            user.setDob(rs.getDate("dob"));
            user.setEmail(rs.getString("email"));
            users.add(user);
        }
    } catch (SQLException e) {
        e.printStackTrace();
    }

    return users;
}

public User getUserById(int userId) {
    User user = new User();
    try {
        PreparedStatement preparedStatement = connection.
                prepareStatement("select * from users where userid=?");
        preparedStatement.setInt(1, userId);
        ResultSet rs = preparedStatement.executeQuery();

        if (rs.next()) {
            user.setUserid(rs.getInt("userid"));
            user.setFirstName(rs.getString("firstname"));
            user.setLastName(rs.getString("lastname"));
            user.setDob(rs.getDate("dob"));
            user.setEmail(rs.getString("email"));
        }
    } catch (SQLException e) {
        e.printStackTrace();
    }

    return user;
}

}

这是我为连接写的那个。 我尝试为细节表添加相同的东西。但它只是不起作用。 帮助将不胜感激。

0 个答案:

没有答案