Advertisement
Guest User

infoqueries

a guest
Aug 8th, 2012
12
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.54 KB | None | 0 0
  1. package model;
  2.  
  3. import java.sql.Connection;
  4. import java.sql.DriverManager;
  5. import java.sql.PreparedStatement;
  6. import java.sql.ResultSet;
  7. import java.sql.SQLException;
  8. import java.sql.Statement;
  9. import java.util.ArrayList;
  10. import java.util.List;
  11.  
  12. import controller.Info;
  13.  
  14. public class InfoQueries {
  15.  
  16.     private static final String URL = "jdbc:mysql://localhost/UserInfo_db";
  17.     private static final String USERNAME = "root";
  18.     private static final String PASSWORD = "";
  19.  
  20.     private Connection connection = null;
  21.     private PreparedStatement insertNewPerson = null;
  22.     private PreparedStatement selectPeopleByUsername = null;
  23.  
  24.     public InfoQueries() {
  25.         try {
  26.             connection = DriverManager.getConnection(URL, USERNAME, PASSWORD);
  27.             Statement stmt = (Statement) connection.createStatement();
  28.  
  29.             String query = "CREATE TABLE IF NOT EXISTS Usertable(ID int, Username VARCHAR(30), Password VARCHAR(20))";
  30.             stmt.executeUpdate(query);
  31.             insertNewPerson = connection
  32.                     .prepareStatement("INSERT INTO Usertable "
  33.                             + "(Username, Password)" + "VALUES (?, ?)");
  34.             selectPeopleByUsername = connection
  35.                     .prepareStatement("SELECT * FROM Usertable WHERE Username=?");
  36.  
  37.         } catch (SQLException aesql) {
  38.             aesql.printStackTrace();
  39.             System.exit(0);
  40.         }
  41.     }
  42.  
  43.     public Boolean getPeopleByUsername(String name, String password) {
  44.         List<Info> results = null;
  45.         ResultSet resultSet = null;
  46.         try {
  47.             selectPeopleByUsername.setString(1, name);
  48.             resultSet = selectPeopleByUsername.executeQuery();
  49.             results = new ArrayList<Info>();
  50.             while (resultSet.next()) {
  51.                 results.add(new Info(resultSet.getInt("ID"), resultSet
  52.                         .getString("Username"), resultSet
  53.                         .getString("Password")));
  54.                 if(resultSet.getString("Password").equals(password))
  55.                 {
  56.                     return true;                   
  57.                 }              
  58.             }
  59.             return false;
  60.         } catch (SQLException sqlException) {
  61.             sqlException.printStackTrace();
  62.         } finally {
  63.             try {
  64.                 resultSet.close();
  65.             } catch (SQLException sqlException) {
  66.                 sqlException.printStackTrace();
  67.                 close();
  68.             }
  69.         }
  70.    
  71.         return false;
  72.        
  73.     }
  74.  
  75.     public int addPerson(String name, String password) {
  76.         int result = 0;
  77.         try {
  78.             insertNewPerson.setString(1, name);
  79.             insertNewPerson.setString(2, password);
  80.  
  81.             result = insertNewPerson.executeUpdate();
  82.         } catch (SQLException sqlException) {
  83.             sqlException.printStackTrace();
  84.             close();
  85.         }
  86.         return result;
  87.     }
  88.  
  89.    
  90.  
  91.     public void close() {
  92.         try {
  93.             connection.close();
  94.         } catch (SQLException sqlException) {
  95.             sqlException.printStackTrace();
  96.         }
  97.     }
  98. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement