Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package SQLiteHandler;
- import java.sql.Connection;
- import java.sql.DriverManager;
- import java.sql.PreparedStatement;
- import java.sql.SQLException;
- import java.sql.Statement;
- public class SqlHandler {
- //connection
- public static void connect(String db){
- Connection conn = null;
- try{
- String url="jdbc:sqlite:"+db+".sqlite";
- conn = DriverManager.getConnection(url);
- System.out.println("connection established");
- }catch(SQLException e){
- System.out.println(e.getMessage());
- }finally{
- try{
- if(conn != null){
- conn.close();
- }
- }catch(SQLException e1){
- System.out.println(e1.getMessage());
- }
- }
- }
- public static void createNewTable(String db){
- String url="jdbc:sqlite:"+db+".sqlite";
- Connection conn = null;
- String sql="CREATE TABLE \"main\".\"Providers\" (\"Id\" INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL UNIQUE , "
- + "\"Name\" CHAR NOT NULL , "
- + "\"Date\" DATETIME NOT NULL DEFAULT CURRENT_DATE)";
- try{
- conn = DriverManager.getConnection(url);
- Statement stmt = conn.createStatement();
- stmt.execute(sql);
- System.out.println("table created succesfully");
- }catch(SQLException e){
- System.out.println(e.getMessage());
- }finally{
- try{
- if(conn != null){
- conn.close();
- }
- }catch(SQLException e1){
- System.out.println(e1.getMessage());
- }
- }
- }
- public static void insertData(String db,String tbl,String nm,String dt){
- String url="jdbc:sqlite:"+db+".sqlite";
- Connection conn = null;
- String sql= "INSERT INTO \"main\"."+tbl+" (\"Name\",\"Date\") VALUES (?1,?2)";
- try{
- conn = DriverManager.getConnection(url);
- PreparedStatement pstmt = conn.prepareStatement(sql);
- pstmt.setString(1, nm);
- pstmt.setString(2, dt);
- pstmt.executeUpdate();
- System.out.println("Inserted succesfully");
- }catch(SQLException e){
- System.out.println(e.getMessage());
- }finally{
- try{
- if(conn != null){
- conn.close();
- }
- }catch(SQLException e1){
- System.out.println(e1.getMessage());
- }
- }
- }
- public static void deleteTable(String db){
- String url="jdbc:sqlite:"+db+".sqlite";
- Connection conn = null;
- String sql="DROP TABLE \"CLIENTS\"";
- try{
- conn = DriverManager.getConnection(url);
- Statement stmt = conn.createStatement();
- stmt.execute(sql);
- System.out.println("table deleted succesfully");
- }catch(SQLException e){
- System.out.println(e.getMessage());
- }finally{
- try{
- if(conn != null){
- conn.close();
- }
- }catch(SQLException e1){
- System.out.println(e1.getMessage());
- }
- }
- }
- public static void deleteColumn(String db){
- String url="jdbc:sqlite:"+db+".sqlite";
- Connection conn = null;
- String sql="DELETE FROM PROVIDERS WHERE Name = Thanos ";
- try{
- conn = DriverManager.getConnection(url);
- Statement stmt = conn.createStatement();
- stmt.execute(sql);
- System.out.println("column deleted succesfully");
- }catch(SQLException e){
- System.out.println(e.getMessage());
- }finally{
- try{
- if(conn != null){
- conn.close();
- }
- }catch(SQLException e1){
- System.out.println(e1.getMessage());
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement