Created
June 29, 2020 04:12
-
-
Save flowsurplus/7daee58ad43c05c24ba0a0eacc9a7858 to your computer and use it in GitHub Desktop.
This is an exploration of SQLite using Java. There are separate classes for the insertion, retrieval, and deletion of entries in the database
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.sql.Connection; | |
import java.sql.DriverManager; | |
import java.sql.Statement; | |
public class Delete { | |
Connection c = null; | |
Statement stmt = null; | |
//This is the method which will be called upon | |
public void deletePerson() { | |
try { | |
Class.forName("org.sqlite.JDBC"); | |
c = DriverManager.getConnection("jdbc:sqlite:Person.db"); | |
c.setAutoCommit(false); | |
System.out.println("Opened database for Deletion"); | |
//This allows me to create a statement | |
stmt = c.createStatement(); | |
//I will delete you Professor | |
String sql = "DELETE from People where ID=5;"; | |
stmt.executeUpdate(sql); | |
c.commit(); | |
stmt.close(); | |
c.close(); | |
} | |
catch ( Exception e ) { | |
System.err.println( e.getClass().getName() + ": " + e.getMessage() ); | |
System.exit(0); | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.sql.Connection; | |
import java.sql.DriverManager; | |
import java.sql.Statement; | |
public class Insert { | |
Connection c = null; | |
Statement stmt = null; | |
//This is the method which will be called upon | |
public void insertPerson() { | |
try { | |
Class.forName("org.sqlite.JDBC"); | |
c = DriverManager.getConnection("jdbc:sqlite:Person.db"); | |
c.setAutoCommit(false); | |
System.out.println("Opened database for Insertion"); | |
//I will insert you Professor in the database | |
stmt = c.createStatement(); | |
String sql = "INSERT INTO People (ID,FirstNAME,LastNAME,Age,SSN, CCard) " + | |
"VALUES (5, 'Professor', 'Gossai', 25, 742090578, 825 );"; | |
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("Person Added Successfully successfully"); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.sql.Connection; | |
import java.sql.DriverManager; | |
import java.sql.ResultSet; | |
import java.sql.Statement; | |
public class Retrieval { | |
Connection c = null; | |
Statement stmt = null; | |
//This is the method which will be called upon | |
public void selectPerson() { | |
try { | |
Class.forName("org.sqlite.JDBC"); | |
c = DriverManager.getConnection("jdbc:sqlite:Person.db"); | |
c.setAutoCommit(false); | |
System.out.println("Opened database for Retrieval"); | |
System.out.println("Retrieved Person(s):"); | |
//This allows me to create a statement | |
stmt = c.createStatement(); | |
ResultSet rs = stmt.executeQuery( "SELECT * FROM People WHERE ID = 5;" ); | |
while ( rs.next() ) { | |
//all of the column names are called in lowercase apparently | |
int id = rs.getInt("id"); | |
String firstname = rs.getString("firstname"); | |
String lastname = rs.getString("lastname"); | |
int age = rs.getInt("age"); | |
float ssn = rs.getFloat("ssn"); | |
float ccard = rs.getFloat("ccard"); | |
System.out.println( "ID = " + id ); | |
System.out.println( "First Name = " + firstname ); | |
System.out.println( "Last Name = " + lastname ); | |
System.out.println( "AGE = " + age ); | |
System.out.println( "SSN = " + ssn); | |
System.out.println( "Credit Card Code = " + ccard ); | |
System.out.println(); | |
} | |
rs.close(); | |
stmt.close(); | |
c.commit(); | |
c.close(); | |
} | |
catch ( Exception e ) { | |
System.err.println( e.getClass().getName() + ": " + e.getMessage() ); | |
System.exit(0); | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.sql.*; | |
public class SQLMain { | |
public static void main( String args[] ) { | |
Connection c = null; | |
Statement stmt = null; | |
try { | |
Class.forName("org.sqlite.JDBC"); | |
//This is the connection for the Database | |
c = DriverManager.getConnection("jdbc:sqlite:Person.db"); | |
c.setAutoCommit(false); | |
System.out.println("Opened database successfully"); | |
//This is the Insert Method and Method call | |
Insert insert = new Insert(); | |
insert.insertPerson(); | |
//This is the retrieval method | |
//Retrieval select = new Retrieval(); | |
//select.selectPerson(); | |
//This is the deletion method | |
//Delete delete = new Delete(); | |
//delete.deletePerson(); | |
System.out.println("**Full Database**"); | |
stmt = c.createStatement(); | |
//This specific query (SELECT * FROM X) will allow me to select all | |
ResultSet rs = stmt.executeQuery( "SELECT * FROM People;" ); | |
while ( rs.next() ) { | |
//all of the column names are called in lowercase apparently | |
int id = rs.getInt("id"); | |
String firstname = rs.getString("firstname"); | |
String lastname = rs.getString("lastname"); | |
int age = rs.getInt("age"); | |
float ssn = rs.getFloat("ssn"); | |
float ccard = rs.getFloat("ccard"); | |
System.out.println( "ID = " + id ); | |
System.out.println( "First Name = " + firstname ); | |
System.out.println( "Last Name = " + lastname ); | |
System.out.println( "AGE = " + age ); | |
System.out.println( "SSN = " + ssn); | |
System.out.println( "Credit Card Code = " + ccard ); | |
System.out.println(); | |
} | |
rs.close(); | |
stmt.close(); | |
c.close(); | |
} catch ( Exception e ) { | |
System.err.println( e.getClass().getName() + ": " + e.getMessage() ); | |
System.exit(0); | |
} | |
System.out.println("Records created successfully"); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment