Advertisement
Guest User

Untitled

a guest
Dec 17th, 2016
107
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.80 KB | None | 0 0
  1. import java.sql.*;
  2.  
  3. class jdbcMysql {
  4.  
  5.  
  6. String url = "sql2.njit.edu";
  7. String ucid = "ab822"; //your ucid
  8. String dbpassword = ""; //your MySQL password
  9. Connection conn;
  10.  
  11. public void createData() {
  12. try {
  13. Class.forName("org.gjt.mm.mysql.Driver").newInstance();
  14. }
  15. catch (Exception e) {
  16. System.err.println("Unable to load driver.");
  17. e.printStackTrace();
  18. }
  19. System.out.println("Driver loaded.");
  20. System.out.println("Establishing connection . . . ");
  21. try {
  22.  
  23.  
  24. conn = DriverManager.getConnection("jdbc:mysql://"+url+"/"+ucid+"?user="+ucid+"&password="+dbpassword);
  25.  
  26. System.out.println("Connection established.");
  27. System.out.println("Creating a Statement object . . . ");
  28.  
  29. Statement stmt = conn.createStatement();
  30. System.out.println("Statement object created.");
  31. stmt.executeUpdate("DROP TABLE IF EXISTS user");
  32. System.out.println("Old table dropped (if it existed).");
  33.  
  34. System.out.println("Creating a table . . .");
  35. stmt.executeUpdate("CREATE TABLE user("+
  36. "idnum INT NOT NULL AUTO_INCREMENT, "+
  37. "PRIMARY KEY(idnum), "+
  38. "pinno VARCHAR(100) UNIQUE NOT NULL, "+
  39. "accountid VARCHAR(100) UNIQUE NOT NULL, "+
  40. "fullname VARCHAR(100) NOT NULL, "+
  41. "email VARCHAR(100) NOT NULL, "+
  42. "amount int NOT NULL");
  43. System.out.println("Table created.");
  44.  
  45. System.out.println("Inserting data in table . . .");
  46.  
  47. stmt.executeUpdate("INSERT INTO user (userid, fullname, email, notes) "+
  48. "VALUES(\"1234\", \"AX1265\", \"john smith\", \"john@njit.edu\", 920)");
  49. stmt.executeUpdate("INSERT INTO user (userid, fullname, email, notes) "+
  50. "VALUES(\"2374\", \"AX1265\", \"john smith\", \"john@njit.edu\", 920)");
  51. stmt.executeUpdate("INSERT INTO user (userid, fullname, email, notes) "+
  52. "VALUES(\"9802\", \"AX1265\", \"john smith\", \"john@njit.edu\", 920)");
  53. stmt.executeUpdate("INSERT INTO user (userid, fullname, email, notes) "+
  54. "VALUES(\"8934\", \"AX1265\", \"john smith\", \"john@njit.edu\", 920)");
  55. stmt.executeUpdate("INSERT INTO user (userid, fullname, email, notes) "+
  56. "VALUES(\"8891\", \"AX1265\", \"john smith\", \"john@njit.edu\", 920)");
  57. stmt.executeUpdate("INSERT INTO user (userid, fullname, email, notes) "+
  58. "VALUES(\"9912\", \"AX1265\", \"john smith\", \"john@njit.edu\", 920)");
  59. stmt.executeUpdate("INSERT INTO user (userid, fullname, email, notes) "+
  60. "VALUES(\"0915\", \"AX1265\", \"john smith\", \"john@njit.edu\", 920)");
  61. stmt.executeUpdate("INSERT INTO user (userid, fullname, email, notes) "+
  62. "VALUES(\"0871\", \"AX1265\", \"john smith\", \"john@njit.edu\", 920)");
  63. stmt.executeUpdate("INSERT INTO user (userid, fullname, email, notes) "+
  64. "VALUES(\"0986\", \"AX1265\", \"john smith\", \"john@njit.edu\", 920)");
  65. System.out.println("Inserted data.");
  66. }
  67. catch (SQLException E) {
  68. System.out.println("SQLException: " + E.getMessage());
  69. System.out.println("SQLState: " + E.getSQLState());
  70. System.out.println("VendorError: " + E.getErrorCode());
  71. }
  72. }
  73.  
  74. public static void main(String args[]) {
  75. jdbcMysql jbml = new jdbcMysql();
  76. jbml.createData();
  77. }
  78.  
  79. public void closeConn( ) throws SQLException {
  80. conn.close();
  81. }
  82.  
  83. public TransactionObject dataOperations(String operation, TransactionObject tmpObj) throws SQLException {
  84. Statement stmt = conn.createStatement();
  85. switch (tmpObj.getType()) {
  86. case "WITHDRAW":
  87. int towithdraw = Integer.parseInt(tmpObj.getNum());
  88. ResultSet rs = stmt.executeQuery("SELECT balance from user where accountID="+tmpObj.getId());
  89. System.out.print(rs.getString("idnum"));
  90. rs.close();
  91. break;
  92. case "BALANCE":
  93. //ResultSet rs = stmt.executeQuery("SELECT balance from user where accountid="+tmpObj.getId());
  94. break;
  95. case "TRANSFER":
  96. //transfer checking saving
  97. break;
  98. }
  99. return tmpObj;
  100. }
  101. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement