Advertisement
Guest User

Untitled

a guest
Mar 3rd, 2019
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.44 KB | None | 0 0
  1. package com.RS09HD.model;
  2.  
  3. import java.sql.*;
  4. import java.util.Map;
  5.  
  6. import com.RS09HD.MySQL.SqlManager;
  7.  
  8. /**
  9.  * Represents a object, primarily manages it's examine option.
  10.  */
  11.  
  12. public class ObjectDefinition {
  13.  
  14.     // JDBC driver name and database URL
  15.     static final String JDBC_DRIVER = "com.mysql.jdbc.Driver";
  16.     static final String DB_URL = "jdbc:mysql://localhost:3306/rs09hd";
  17.  
  18.     //  Database credentials
  19.     static final String USER = "server";
  20.     static final String PASS = "Runescape09";
  21.  
  22.     private static String examine;
  23.  
  24.     public static String query(int id) throws SQLException {
  25.         System.out.println("ID:" + id);
  26.         Connection conn = null;
  27.         PreparedStatement stmt = null;
  28.         try{
  29.             // Register JDBC driver
  30.             Class.forName("com.mysql.jdbc.Driver");
  31.  
  32.             // Open a connection
  33.             System.out.println("Connecting to database...");
  34.             conn = DriverManager.getConnection(DB_URL,USER,PASS);
  35.         }catch(SQLException se){
  36.             //Handle errors for JDBC
  37.             se.printStackTrace();
  38.         }catch(Exception e){
  39.             //Handle errors for Class.forName
  40.             e.printStackTrace();
  41.         }
  42.         // Execute a query
  43.         System.out.println("Creating statement...");
  44.         PreparedStatement ps = conn.prepareStatement( "SELECT * FROM object_examines WHERE id = ? " ) ;
  45.         //Bind values into the parameter
  46.         ps.setInt( 1, id) ;
  47.  
  48.         System.out.println(ps);
  49.         ResultSet rs = ps.executeQuery() ;
  50.         System.out.println(rs);
  51.         while(rs.next()){
  52.             //Retrieve by column name
  53.             String examine = rs.getString("examine");
  54.             //Display values
  55.             System.out.println("Examine: " + examine);
  56.         }
  57.         return examine;
  58.     }
  59.  
  60.     public static ObjectDefinition forId(int id) {
  61.         try {
  62.             query(id);
  63.         } catch (SQLException e) {
  64.             e.printStackTrace();
  65.         }
  66.         return null;
  67.     }
  68.  
  69.     private int id;
  70.  
  71.     public int getId() {
  72.         return id;
  73.     }
  74.  
  75.     public String getExamine() {
  76.         return examine;
  77.     }
  78.  
  79.     public static ObjectDefinition produceDefinition(int id) {
  80.         ObjectDefinition def = new ObjectDefinition();
  81.         def.id = id;
  82.         String idString;
  83.         idString = new String( "Object ID: " + def.id);
  84.         def.examine = idString;
  85.         return def;
  86.  
  87.     }
  88.  
  89. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement