Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package bloggen.io;
- import java.sql.*;
- import java.util.ArrayList;
- import bloggen.domain.User;
- import dk.au.hum.imv.persistence.db.DatabasePersistent;
- import dk.au.hum.imv.persistence.db.JDBCConnectionFactory;
- public class UserDAO extends DatabasePersistent {
- public static User getUserById(long ID) throws Exception {
- User returnUser = null;
- Connection myConn = null;
- try { // myConn objekt
- myConn = JDBCConnectionFactory.getNewConnection();
- String sql = "SELECT * FROM User WHERE userID = ?";
- PreparedStatement prep = myConn.prepareStatement(sql);
- prep.setLong(1, ID);
- ResultSet res = prep.executeQuery();
- while (res.next()) {
- String fullName = res.getString("User.fullName");
- String userName = res.getString("User.userName");
- String password = res.getString("User.password");
- String email = res.getString("User.email");
- returnUser = new User(ID, fullName, userName, password, email);
- }
- } catch (SQLException e) {
- e.printStackTrace();
- } finally {
- JDBCConnectionFactory.closeConnection(myConn);
- }
- return returnUser;
- }
- // ny arraylist,
- // her vil vi opbevarer vore brugere, så vi kan udskrive dem og løbe igennem dem.
- public static ArrayList<User> getAllUsers() throws SQLException {
- // Arraylist --> indholder metoden getAllUsers
- ArrayList<User> users = new ArrayList<User>();
- Connection myConn = null;
- try {
- myConn = JDBCConnectionFactory.getNewConnection();
- String sql = "SELECT * FROM User";
- Statement statement = myConn.createStatement();
- ResultSet res = statement.executeQuery(sql);
- while (res.next()) {
- long userID = res.getLong("userID");
- String fullName = res.getString("fullName");
- String userName = res.getString("userName");
- String password = res.getString("Password");
- String email = res.getString("email");
- users.add(new User(userID, fullName, userName, password, email));
- }
- } catch (SQLException e) {
- e.printStackTrace();
- } finally {
- JDBCConnectionFactory.closeConnection(myConn);
- }
- return users;
- }
- public static void createUser(String userName, String password, String email, String fullName, long userID)
- throws SQLException {
- Connection myConn = null;
- try {
- myConn = JDBCConnectionFactory.getNewConnection();
- PreparedStatement statement = myConn.prepareStatement(
- "INSERT INTO User (userName, password, email, fullName, userID) VALUES(?, ?, ?, ?, ?)");
- statement.setString(1, userName);
- statement.setString(2, password);
- statement.setString(3, email);
- statement.setString(4, fullName);
- statement.setLong(5, userID);
- statement.executeUpdate();
- } catch (SQLException e) {
- e.printStackTrace();
- } finally {
- JDBCConnectionFactory.closeConnection(myConn);
- }
- }
- public static User createUserFromResultSet(ResultSet res) throws SQLException { //opretter et user objekt med det der kommer som forespørgelsel fra database, så der bliver lavet et nyt userobjekt hver gang
- String fullName = res.getString("user.fullName");
- String userName = res.getString("user.userName");
- String password = res.getString("password");
- long databaseId = res.getLong("user.userID");
- String email = res.getString("user.email");
- User result = new User(databaseId, fullName, userName, password, email);
- return result;
- }
- public static User getUserFromCredentials(String loginname, String password) throws SQLException {
- //metode til at hente bruger ud fra brugernavn og password. --> Den fungerer med login.jsp
- User result = null;
- Connection con = null;
- try{
- con = JDBCConnectionFactory.getNewConnection();
- String sql = "SELECT * FROM User WHERE User.userName = ? AND User.password = ?";
- PreparedStatement prep = con.prepareStatement(sql);
- prep.setString(1, loginname);
- prep.setString(2, password);
- ResultSet res = prep.executeQuery();
- while (res.next()) {
- result = createUserFromResultSet(res);
- }
- } catch(SQLException e) {
- e.printStackTrace();
- } finally {
- JDBCConnectionFactory.closeConnection(con);
- }
- return result;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement