Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.sql.*;
- class jdbcMysql {
- String url = "sql2.njit.edu";
- String ucid = "ab822"; //your ucid
- String dbpassword = "etPccaCIk"; //your MySQL password
- Connection conn;
- public void createData() {
- try {
- Class.forName("org.gjt.mm.mysql.Driver").newInstance();
- }
- catch (Exception e) {
- System.err.println("Unable to load driver.");
- e.printStackTrace();
- }
- System.out.println("Driver loaded.");
- System.out.println("Establishing connection . . . ");
- try {
- conn = DriverManager.getConnection("jdbc:mysql://"+url+"/"+ucid+"?user="+ucid+"&password="+dbpassword);
- System.out.println("Connection established.");
- System.out.println("Creating a Statement object . . . ");
- Statement stmt = conn.createStatement();
- System.out.println("Statement object created.");
- stmt.executeUpdate("DROP TABLE IF EXISTS user");
- System.out.println("Old table dropped (if it existed).");
- System.out.println("Creating a table . . .");
- stmt.executeUpdate("CREATE TABLE user("+
- "id VARCHAR(100) NOT NULL, "+
- "type VARCHAR(100) NOT NULL, "+
- "num VARCHAR(100) NOT NULL, "+
- "name VARCHAR(100) NOT NULL, "+
- "date DATE NOT NULL, "+
- "amount int NOT NULL)");
- System.out.println("Table created.");
- System.out.println("Inserting data in table . . .");
- stmt.executeUpdate("INSERT INTO user (id, type, num, name, date, amount) "+
- "VALUES(\"1234\", \"Savings\", \"Ritu1234\", \"Ritu\", '2013-12-12',920)");
- stmt.executeUpdate("INSERT INTO user (id, type, num, name, date, amount) "+
- "VALUES(\"1234\", \"CheckIn\", \"Ritu1234\", \"Ritu\", '2016-09-26',9920)");
- stmt.executeUpdate("INSERT INTO user (id, type, num, name, date, amount) "+
- "VALUES(\"1224\", \"CheckIn\", \"Kitu1224\", \"Kitu\", '2008-11-05',9200)");
- stmt.executeUpdate("INSERT INTO user (id, type, num, name, date, amount) "+
- "VALUES(\"1224\", \"Savings\", \"Kitu1224\", \"Kitu\", '2001-08-28',19200)");
- stmt.executeUpdate("INSERT INTO user (id, type, num, name, date, amount) "+
- "VALUES(\"5678\", \"Savings\", \"John5678\", \"John\", '2010-03-09',5679)");
- stmt.executeUpdate("INSERT INTO user (id, type, num, name, date, amount) "+
- "VALUES(\"5678\", \"CheckIn\", \"John5678\", \"John\", '2005-02-01',15679)");
- stmt.executeUpdate("INSERT INTO user (id, type, num, name, date, amount) "+
- "VALUES(\"6789\", \"CheckIn\", \"Max6789\", \"Max\", '2012-06-03',6780)");
- stmt.executeUpdate("INSERT INTO user (id, type, num, name, date, amount) "+
- "VALUES(\"4567\", \"Savings\", \"Ron4567\", \"Ron\", '2016-10-17',6792)");
- stmt.executeUpdate("INSERT INTO user (id, type, num, name, date, amount) "+
- "VALUES(\"4592\", \"Savings\", \"Mary4592\", \"Mary\", '2014-11-28',3456)");
- stmt.executeUpdate("INSERT INTO user (id, type, num, name, date, amount) "+
- "VALUES(\"5612\", \"CheckIn\", \"Joe5612\", \"Joe\", '2011-03-07',6788)");
- stmt.executeUpdate("INSERT INTO user (id, type, num, name, date, amount) "+
- "VALUES(\"7823\", \"CheckIn\", \"Smith7823\", \"Smith\", '2009-01-12',10000)");
- stmt.executeUpdate("INSERT INTO user (id, type, num, name, date, amount) "+
- "VALUES(\"9832\", \"Savings\", \"Alex9832\", \"Alex\", '2002-05-18',7832)");
- System.out.println("Inserted data.");
- }
- catch (SQLException E) {
- System.out.println("SQLException: " + E.getMessage());
- System.out.println("SQLState: " + E.getSQLState());
- System.out.println("VendorError: " + E.getErrorCode());
- }
- }
- public static void main(String args[]) {
- jdbcMysql jbml = new jdbcMysql();
- jbml.createData();
- }
- public void closeConn( ) throws SQLException {
- conn.close();
- }
- /*
- public TransactionObject dataOperations(String operation, TransactionObject tmpObj) throws SQLException {
- Statement stmt = conn.createStatement();
- switch (tmpObj.getType()) {
- case "WITHDRAW":
- int towithdraw = Integer.parseInt(tmpObj.getNum());
- ResultSet rs = stmt.executeQuery("SELECT balance from user where accountID="+tmpObj.getId());
- System.out.print(rs.getString("idnum"));
- rs.close();
- break;
- case "BALANCE":
- //ResultSet rs = stmt.executeQuery("SELECT balance from user where accountid="+tmpObj.getId());
- break;
- case "TRANSFER":
- //transfer checking saving
- break;
- }
- return tmpObj;
- }
- */
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement