Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.sql.*;
- import java.util.ArrayList;
- import java.util.NoSuchElementException;
- import java.util.Scanner;
- public class SQLStuff {
- public static void main(String[] args) {
- CreateDatabase db = new CreateDatabase();
- Boolean continuation = true;
- Connection c = null;
- Statement stmt = null;
- //db.databaseCreate(c);
- //db.tableCreate(c, stmt);
- Scanner sc = new Scanner(System.in);
- while(continuation) {
- System.out.println("What operation do you want to execute?\nINSERT PERSON, PRINT PERSON, PRINT ALL, UPDATE PERSON, DELETE PERSON");
- String userIn = sc.nextLine();
- switch(userIn) {
- case "INSERT PERSON":
- db.insertPerson(c, stmt);
- break;
- case "PRINT PERSON":
- db.printPeople(db.selectPerson(c, stmt));
- break;
- case "PRINT ALL":
- db.printPeople(db.findAllPeople(c, stmt));
- break;
- case "UPDATE PERSON":
- db.updatePerson(c, stmt);
- break;
- case "DELETE PERSON":
- db.deletePerson(c, stmt);
- break;
- default:
- System.out.println("Unrecognized command.");
- }
- System.out.println("Continue? Y/N");
- String ct = sc.nextLine();
- if(ct.equals("Y")) {
- continuation = true;
- }
- else {
- continuation = false;
- }
- }
- System.out.println("Exited.");
- }
- }
- class CreateDatabase {
- public void databaseCreate(Connection c) {
- try {
- Class.forName("org.sqlite.JDBC");
- c = DriverManager.getConnection("jdbc:sqlite:SQLtest.db");
- }
- catch (Exception e) {
- System.err.println(e.getClass().getName() + ": " + e.getMessage());
- System.exit(0);
- }
- System.out.println("Opened database successfully.");
- }
- public void tableCreate(Connection c, Statement stmt) {
- try {
- Class.forName("org.sqlite.JDBC");
- c = DriverManager.getConnection("jdbc:sqlite:SQLtest.db");
- System.out.println("Database retrieved successfully.");
- stmt = c.createStatement();
- String sql = "CREATE TABLE PERSON " +
- "(ID INT PRIMARY KEY NOT NULL," +
- "FIRSTNAME TEXT NOT NULL," +
- "LASTNAME TEXT NOT NULL," +
- "AGE INT NOT NULL," +
- "SSN REAL NOT NULL," +
- "CC REAL NOT NULL)";
- stmt.executeUpdate(sql);
- stmt.close();
- c.close();
- }
- catch (Exception e) {
- System.err.println(e.getClass().getName() + ": " + e.getMessage());
- System.exit(0);
- }
- System.out.println("Table created successfully.");
- }
- public void infoCreate(Connection c, Statement stmt) {
- try {
- Person p = new Person(2,"Bob","Thompson",27,201234092,440019193);
- Class.forName("org.sqlite.JDBC");
- c = DriverManager.getConnection("jdbc:sqlite:SQLtest.db");
- c.setAutoCommit(false);
- System.out.println("Accessed database successfully.");
- stmt = c.createStatement();
- String sql = "INSERT INTO PERSON (ID,FIRSTNAME,LASTNAME,AGE,SSN,CC) "+
- p.buildSQL();
- stmt.executeUpdate(sql);
- stmt.close();
- c.commit();
- c.close();
- }
- catch(Exception e) {
- System.err.println(e.getClass().getName()+ ": "+e.getMessage());
- System.exit(0);
- }
- System.out.println("Records created successfully.");
- }
- public void insertPerson(Connection c, Statement stmt) {
- Scanner sc = new Scanner(System.in);
- System.out.println("Enter id start: ");
- int id = sc.nextInt();
- Boolean entry = true;
- while (entry) {
- Person p = new Person();
- p.buildPerson(id);
- try {
- Class.forName("org.sqlite.JDBC");
- c = DriverManager.getConnection("jdbc:sqlite:SQLtest.db");
- c.setAutoCommit(false);
- //System.out.println("Accessed database successfully.");
- stmt = c.createStatement();
- String sql = "INSERT INTO PERSON (ID,FIRSTNAME,LASTNAME,AGE,SSN,CC) "+
- p.buildSQL();
- stmt.executeUpdate(sql);
- stmt.close();
- c.commit();
- c.close();
- }
- catch (SQLException e) {
- System.out.println("Value already exists. Enter different ID.");
- }
- catch(Exception e) {
- System.err.println(e.getClass().getName()+ ": "+e.getMessage());
- System.exit(0);
- }
- System.out.println("Records created successfully.");
- id++;
- System.out.println("Add more records? 0/1 ");
- int val = sc.nextInt();
- if(val == 1) {
- entry = true;
- } else {
- entry = false;
- }
- }
- }
- public ArrayList<Person> findAllPeople(Connection c, Statement stmt) {
- ArrayList<Person> people = new ArrayList<Person>();
- try {
- Class.forName("org.sqlite.JDBC");
- c = DriverManager.getConnection("jdbc:sqlite:SQLtest.db");
- c.setAutoCommit(false);
- System.out.println("Opened database successfully.");
- stmt = c.createStatement();
- ResultSet rs = stmt.executeQuery("SELECT * FROM PERSON;");
- while(rs.next()) {
- int id = rs.getInt("ID");
- String firstName = rs.getString("FIRSTNAME");
- String lastName = rs.getString("LASTNAME");
- int age = rs.getInt("AGE");
- long ssn = rs.getLong("SSN");
- long cc = rs.getLong("CC");
- Person p = new Person(id,firstName,lastName,age,ssn,cc);
- people.add(p);
- }
- rs.close();
- stmt.close();
- c.close();
- }
- catch(Exception e) {
- System.err.println(e.getClass().getName()+ ": " + e.getMessage());
- System.exit(0);
- }
- System.out.println("Operation done successfully.");
- return people;
- }
- public void printPeople(ArrayList<Person> people) {
- for(Person person : people) {
- System.out.println("ID: "+ person.id);
- System.out.println("First Name: " + person.firstName);
- System.out.println("Last Name: "+person.lastName);
- System.out.println("Age: " + person.age);
- System.out.println("SSN: "+person.ssn);
- System.out.println("Credit Card: "+person.cc+"\n");
- }
- }
- public ArrayList<Person> selectPerson(Connection c, Statement stmt) {
- Scanner sc = new Scanner(System.in);
- System.out.println("Enter unique person ID: ");
- int userid=sc.nextInt();
- ArrayList<Person> people = new ArrayList<Person>();
- try {
- Class.forName("org.sqlite.JDBC");
- c = DriverManager.getConnection("jdbc:sqlite:SQLtest.db");
- c.setAutoCommit(false);
- System.out.println("Opened database successfully.");
- stmt = c.createStatement();
- ResultSet rs = stmt.executeQuery("SELECT * FROM PERSON WHERE ID="+userid+";");
- int id = rs.getInt("ID");
- String firstName = rs.getString("FIRSTNAME");
- String lastName = rs.getString("LASTNAME");
- int age = rs.getInt("AGE");
- long ssn = rs.getLong("SSN");
- long cc = rs.getLong("CC");
- Person p = new Person(id,firstName,lastName,age,ssn,cc);
- people.add(p);
- rs.close();
- stmt.close();
- c.close();
- }
- catch (SQLException e) {
- System.out.println("Out of bounds.");
- }
- catch(Exception e) {
- System.err.println(e.getClass().getName()+ ": " + e.getMessage());
- System.exit(0);
- }
- System.out.println("Operation done successfully.");
- return people;
- }
- public void updatePerson(Connection c, Statement stmt) {
- try {
- Class.forName("org.sqlite.JDBC");
- c = DriverManager.getConnection("jdbc:sqlite:SQLtest.db");
- c.setAutoCommit(false);
- System.out.println("Opened database successfully");
- stmt = c.createStatement();
- Scanner sc = new Scanner(System.in);
- System.out.println("Update which ID? ");
- int userid = sc.nextInt();
- System.out.println("Update which value? ");
- String userval = sc.next();
- System.out.println("Enter new value: ");
- String newVal = sc.next();
- String sql;
- switch(userval) {
- case "FIRSTNAME":
- sql = "UPDATE PERSON set "+userval+" = '"+newVal+"' where ID="+userid+";";
- break;
- case "LASTNAME":
- sql = "UPDATE PERSON set "+userval+" = '"+newVal+"' where ID="+userid+";";
- break;
- default:
- sql = "UPDATE PERSON set "+userval+" = "+newVal+" where ID="+userid+";";
- break;
- }
- stmt.executeUpdate(sql);
- c.commit();
- }
- catch(SQLException e) {
- System.out.println("Unrecognized command. Be sure to type it LIKETHIS.");
- }
- catch ( Exception e ) {
- System.err.println( e.getClass().getName() + ": " + e.getMessage() );
- System.exit(0);
- }
- System.out.println("Operation done successfully");
- }
- public void deletePerson(Connection c, Statement stmt) {
- Scanner sc = new Scanner(System.in);
- System.out.println("Enter user first name to delete: ");
- String userfirst = sc.nextLine();
- System.out.println("Enter user last name to delete: ");
- String userlast = sc.nextLine();
- try {
- Class.forName("org.sqlite.JDBC");
- c = DriverManager.getConnection("jdbc:sqlite:SQLtest.db");
- c.setAutoCommit(false);
- System.out.println("Opened database successfully");
- stmt = c.createStatement();
- String sql = "DELETE from PERSON where FIRSTNAME='"+userfirst+"' AND LASTNAME='"+userlast+"' where EXISTS;";
- stmt.executeUpdate(sql);
- c.commit();
- }
- catch (SQLException e) {
- System.out.println("No such person exists.");
- }
- catch ( Exception e ) {
- System.err.println( e.getClass().getName() + ": " + e.getMessage() );
- System.exit(0);
- }
- System.out.println("Operation done successfully");
- }
- }
- class Person {
- int id;
- String firstName;
- String lastName;
- int age;
- long ssn;
- long cc;
- Person(int id, String firstName, String lastName,int age,long ssn,long cc){
- this.id = id;
- this.firstName = firstName;
- this.lastName = lastName;
- this.age = age;
- this.ssn = ssn;
- this.cc = cc;
- }
- Person(){
- }
- public void buildPerson(int id) {
- this.id = id;
- Scanner sc = new Scanner(System.in);
- System.out.println("Enter first name: ");
- this.firstName = sc.nextLine();
- System.out.println("Enter last name: ");
- this.lastName = sc.nextLine();
- System.out.println("Enter age: ");
- this.age = sc.nextInt();
- System.out.println("Enter SSN: ");
- this.ssn = sc.nextLong();
- System.out.println("Enter credit card number: ");
- this.cc = sc.nextLong();
- System.out.println("Person info added.");
- }
- public String buildSQL() {
- StringBuilder str = new StringBuilder();
- str.append("VALUES (");
- str.append(this.id);
- str.append(",'");
- str.append(this.firstName);
- str.append("','");
- str.append(this.lastName);
- str.append("',");
- str.append(this.age);
- str.append(",");
- str.append(this.ssn);
- str.append(",");
- str.append(this.cc);
- str.append(");");
- return str.toString();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement