Advertisement
Guest User

Untitled

a guest
Dec 17th, 2016
103
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.28 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 = "etPccaCIk"; //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. "id VARCHAR(100) NOT NULL, "+
  37. "type VARCHAR(100) NOT NULL, "+
  38. "num VARCHAR(100) NOT NULL, "+
  39. "name VARCHAR(100) NOT NULL, "+
  40. "date DATE NOT NULL, "+
  41. "amount int NOT NULL)");
  42. System.out.println("Table created.");
  43.  
  44. System.out.println("Inserting data in table . . .");
  45.  
  46. stmt.executeUpdate("INSERT INTO user (id, type, num, name, date, amount) "+
  47. "VALUES(\"1234\", \"Savings\", \"Ritu1234\", \"Ritu\", '2013-12-12',920)");
  48. stmt.executeUpdate("INSERT INTO user (id, type, num, name, date, amount) "+
  49. "VALUES(\"1234\", \"CheckIn\", \"Ritu1234\", \"Ritu\", '2016-09-26',9920)");
  50. stmt.executeUpdate("INSERT INTO user (id, type, num, name, date, amount) "+
  51. "VALUES(\"1224\", \"CheckIn\", \"Kitu1224\", \"Kitu\", '2008-11-05',9200)");
  52. stmt.executeUpdate("INSERT INTO user (id, type, num, name, date, amount) "+
  53. "VALUES(\"1224\", \"Savings\", \"Kitu1224\", \"Kitu\", '2001-08-28',19200)");
  54. stmt.executeUpdate("INSERT INTO user (id, type, num, name, date, amount) "+
  55. "VALUES(\"5678\", \"Savings\", \"John5678\", \"John\", '2010-03-09',5679)");
  56. stmt.executeUpdate("INSERT INTO user (id, type, num, name, date, amount) "+
  57. "VALUES(\"5678\", \"CheckIn\", \"John5678\", \"John\", '2005-02-01',15679)");
  58. stmt.executeUpdate("INSERT INTO user (id, type, num, name, date, amount) "+
  59. "VALUES(\"6789\", \"CheckIn\", \"Max6789\", \"Max\", '2012-06-03',6780)");
  60. stmt.executeUpdate("INSERT INTO user (id, type, num, name, date, amount) "+
  61. "VALUES(\"4567\", \"Savings\", \"Ron4567\", \"Ron\", '2016-10-17',6792)");
  62. stmt.executeUpdate("INSERT INTO user (id, type, num, name, date, amount) "+
  63. "VALUES(\"4592\", \"Savings\", \"Mary4592\", \"Mary\", '2014-11-28',3456)");
  64. stmt.executeUpdate("INSERT INTO user (id, type, num, name, date, amount) "+
  65. "VALUES(\"5612\", \"CheckIn\", \"Joe5612\", \"Joe\", '2011-03-07',6788)");
  66. stmt.executeUpdate("INSERT INTO user (id, type, num, name, date, amount) "+
  67. "VALUES(\"7823\", \"CheckIn\", \"Smith7823\", \"Smith\", '2009-01-12',10000)");
  68. stmt.executeUpdate("INSERT INTO user (id, type, num, name, date, amount) "+
  69. "VALUES(\"9832\", \"Savings\", \"Alex9832\", \"Alex\", '2002-05-18',7832)");
  70. System.out.println("Inserted data.");
  71. }
  72. catch (SQLException E) {
  73. System.out.println("SQLException: " + E.getMessage());
  74. System.out.println("SQLState: " + E.getSQLState());
  75. System.out.println("VendorError: " + E.getErrorCode());
  76. }
  77. }
  78.  
  79. public static void main(String args[]) {
  80. jdbcMysql jbml = new jdbcMysql();
  81. jbml.createData();
  82. }
  83.  
  84. public void closeConn( ) throws SQLException {
  85. conn.close();
  86. }
  87. /*
  88. public TransactionObject dataOperations(String operation, TransactionObject tmpObj) throws SQLException {
  89. Statement stmt = conn.createStatement();
  90. switch (tmpObj.getType()) {
  91. case "WITHDRAW":
  92. int towithdraw = Integer.parseInt(tmpObj.getNum());
  93. ResultSet rs = stmt.executeQuery("SELECT balance from user where accountID="+tmpObj.getId());
  94. System.out.print(rs.getString("idnum"));
  95. rs.close();
  96. break;
  97. case "BALANCE":
  98. //ResultSet rs = stmt.executeQuery("SELECT balance from user where accountid="+tmpObj.getId());
  99. break;
  100. case "TRANSFER":
  101. //transfer checking saving
  102. break;
  103. }
  104. return tmpObj;
  105. }
  106. */
  107. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement