Guest User

Untitled

a guest
May 21st, 2018
107
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.46 KB | None | 0 0
  1. package fh.at.ooe.mc10;
  2.  
  3. import java.sql.Connection;
  4. import java.sql.Date;
  5. import java.sql.DriverManager;
  6. import java.sql.ResultSet;
  7. import java.sql.SQLException;
  8. import java.sql.Statement;
  9.  
  10. import javax.jws.WebService;
  11. import javax.jws.soap.SOAPBinding;
  12.  
  13. @WebService
  14. @SOAPBinding (style = SOAPBinding.Style.RPC)
  15.  
  16. public class DBHandle {
  17.     private static Connection conn;
  18.     private static Statement stmt;
  19.     private static ResultSet rs;
  20.  
  21.     public static void main(String[] args) throws SQLException {
  22.         DBHandle dbh = new DBHandle();
  23.         dbh.printUserDetails();
  24.         dbh.getUsers();
  25.     }
  26.  
  27.     public DBHandle() {
  28.         try {
  29.             Class.forName("com.mysql.jdbc.Driver").newInstance();
  30.         } catch (Exception ex) {
  31.             ex.printStackTrace();
  32.         }
  33.         try {
  34.             conn = DriverManager.getConnection(
  35.                     "jdbc:mysql://localhost/citydetectivedatabase", "root", "");
  36.             stmt = conn.createStatement();
  37.         } catch (SQLException ex) {
  38.             printEx(ex);
  39.         }
  40.     }
  41.  
  42.     /**
  43.      *
  44.      * Prints the detailed information of an SQLException.
  45.      *
  46.      * @param ex
  47.      *            SQLException
  48.      */
  49.     public static void printEx(Exception ex) {
  50.         System.out.println("SQLException: " + ex.getMessage());
  51.         System.out.println("SQLState: " + ((SQLException) ex).getSQLState());
  52.         System.out
  53.                 .println("VendorError: " + ((SQLException) ex).getErrorCode());
  54.     }
  55.  
  56.     /**
  57.      * @param groupname
  58.      * @param password
  59.      * @return true if password is correct
  60.      */
  61.     public boolean joinGroup(String groupname, String password) {
  62.         try {
  63.             stmt = conn.createStatement();
  64.             rs = stmt
  65.                     .executeQuery("SELECT password FROM citydetectivegroupdata WHERE groupname='"
  66.                             + groupname + "'");
  67.             if (rs.getString(1) == password) {
  68.                 return true;
  69.             } else {
  70.                 return false;
  71.             }
  72.         } catch (SQLException ex) {
  73.             printEx(ex);
  74.             return false;
  75.         }
  76.     }
  77.  
  78.     /**
  79.      * Prints all user names, highscores and date to the console
  80.      */
  81.     public void printUserDetails() {
  82.         User[] users = getUsers();
  83.         System.out.println("Username \t Highscore \t Date \t\t Group");
  84.         for (int i = 0; i < users.length; i++) {
  85.             if (users[i].get_uname().length() < 8) {
  86.                 System.out.println(users[i].get_uname() + '\t' + '\t'
  87.                         + users[i].get_highscore() + '\t' + '\t'
  88.                         + users[i].get_date().toString() + '\t' + users[i].get_group());
  89.             } else {
  90.                 System.out.println(users[i].get_uname() + '\t'
  91.                         + users[i].get_highscore() + '\t' + '\t'
  92.                         + users[i].get_date().toString() + '\t' + users[i].get_group());
  93.             }
  94.         }
  95.     }
  96.  
  97.     /**
  98.      * @return is a User[] of all users from the database
  99.      */
  100.     public User[] getUsers() {
  101.         try {
  102.             rs = stmt.executeQuery("SELECT * FROM citydetectivedata");
  103.             int count = 0;
  104.             while (rs.next()) { // count entries
  105.                 count++;
  106.             }
  107.  
  108.             rs = stmt
  109.                     .executeQuery("SELECT * FROM citydetectivedata ORDER BY citydetectivedata.highscore DESC");
  110.  
  111.             User[] users = new User[count];
  112.             int i = 0;
  113.             while (rs.next()) {
  114.                 User u = new User();
  115.                 u.set_uname(rs.getString(1));
  116.                 u.set_highscore(rs.getInt(2));
  117.                 u.set_date(rs.getDate(3));
  118.                 u.set_group(rs.getString(4));
  119.                 users[i] = u;
  120.                 i++;
  121.             }
  122.             return users;
  123.         } catch (SQLException ex) {
  124.             printEx(ex);
  125.             return null;
  126.         }
  127.     }
  128.  
  129.     /**
  130.      * @param name
  131.      * @return false if user does not exist, true otherwise
  132.      */
  133.     public boolean existsUser(String name) {
  134.         try {
  135.             stmt = conn.createStatement();
  136.             rs = stmt
  137.                     .executeQuery("SELECT * FROM citydetectivedata WHERE username='"
  138.                             + name + "'");
  139.             if (rs == null) {
  140.                 return false;
  141.             }
  142.             return true;
  143.         } catch (SQLException ex) {
  144.             printEx(ex);
  145.             return false;
  146.         }
  147.     }
  148.  
  149.     /**
  150.      * @param groupname
  151.      * @return
  152.      */
  153.     public boolean existsGroup(String groupname) {
  154.         try {
  155.             stmt = conn.createStatement();
  156.             rs = stmt
  157.                     .executeQuery("SELECT * FROM citydetectivegroupdata WHERE groupname='"
  158.                             + groupname + "'");
  159.             if (rs == null) {
  160.                 return false;
  161.             }
  162.             return true;
  163.         } catch (SQLException ex) {
  164.             printEx(ex);
  165.             return false;
  166.         }
  167.     }
  168.  
  169.     /**
  170.      * @param name
  171.      * @param password
  172.      * @return true if succeeded, false otherwise
  173.      * @throws SQLException
  174.      */
  175.     public boolean createUser(String name) throws SQLException {
  176.         try {
  177.             Date d = new Date(System.currentTimeMillis());
  178.  
  179.             stmt = conn.createStatement();
  180.             stmt.execute("INSERT INTO `citydetectivedatabase`.`citydetectivedata` (`username` ,`highscore`, `date`, `group`) VALUES ('"
  181.                     + name
  182.                     + "', '"
  183.                     + 0
  184.                     + "', '"
  185.                     + d.toString()
  186.                     + "', '"
  187.                     + "null" + "');");
  188.             return true;
  189.         } catch (SQLException ex) {
  190.             printEx(ex);
  191.             return false;
  192.         }
  193.     }
  194.  
  195.     /**
  196.      * @param groupname
  197.      * @param password
  198.      * @return
  199.      * @throws SQLException
  200.      */
  201.     public boolean createGroup(String groupname, String password)
  202.             throws SQLException {
  203.         try {
  204.             stmt = conn.createStatement();
  205.             stmt.execute("INSERT INTO `citydetectivedatabase`.`citydetectivegroupdata` (`groupname` ,`password`) VALUES ('"
  206.                     + groupname + "', '" + password + "');");
  207.             return true;
  208.         } catch (SQLException ex) {
  209.             printEx(ex);
  210.             return false;
  211.         }
  212.     }
  213.  
  214.     /**
  215.      * @param name
  216.      * @param newHighscore
  217.      * @return
  218.      */
  219.     public boolean setNewUserHighscore(String name, int newHighscore) {
  220.         try {
  221.             stmt = conn.createStatement();
  222.             stmt.execute("UPDATE citydetectivedata SET highscore ='"
  223.                     + newHighscore + "' WHERE username='" + name + "'");
  224.             return true;
  225.         } catch (SQLException ex) {
  226.             printEx(ex);
  227.             return false;
  228.         }
  229.     }
  230. }
Add Comment
Please, Sign In to add comment