Advertisement
Guest User

sql

a guest
Nov 1st, 2016
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.13 KB | None | 0 0
  1. package SQLiteHandler;
  2.  
  3. import java.sql.Connection;
  4. import java.sql.DriverManager;
  5. import java.sql.PreparedStatement;
  6. import java.sql.SQLException;
  7. import java.sql.Statement;
  8.  
  9. public class SqlHandler {
  10.     //connection
  11.     public static void connect(String db){
  12.         Connection conn = null;
  13.         try{
  14.         String url="jdbc:sqlite:"+db+".sqlite";
  15.         conn = DriverManager.getConnection(url);
  16.         System.out.println("connection established");
  17.         }catch(SQLException e){
  18.             System.out.println(e.getMessage());
  19.         }finally{
  20.             try{
  21.                 if(conn != null){
  22.                     conn.close();
  23.                 }
  24.             }catch(SQLException e1){
  25.                 System.out.println(e1.getMessage());
  26.             }
  27.         }
  28.        
  29.     }
  30.     public static void createNewTable(String db){
  31.         String url="jdbc:sqlite:"+db+".sqlite";
  32.         Connection conn = null;
  33.         String sql="CREATE  TABLE \"main\".\"Providers\" (\"Id\" INTEGER PRIMARY KEY  AUTOINCREMENT  NOT NULL  UNIQUE , "
  34.                 + "\"Name\" CHAR NOT NULL , "
  35.                         + "\"Date\" DATETIME NOT NULL  DEFAULT CURRENT_DATE)";
  36.     try{
  37.         conn = DriverManager.getConnection(url);
  38.         Statement stmt = conn.createStatement();
  39.         stmt.execute(sql);
  40.         System.out.println("table created succesfully");
  41.     }catch(SQLException e){
  42.         System.out.println(e.getMessage());
  43.     }finally{
  44.         try{
  45.             if(conn != null){
  46.                 conn.close();
  47.                 }
  48.         }catch(SQLException e1){
  49.             System.out.println(e1.getMessage());
  50.             }
  51.         }
  52.     }
  53.    
  54.     public static void insertData(String db,String tbl,String nm,String dt){
  55.         String url="jdbc:sqlite:"+db+".sqlite";
  56.         Connection conn = null;
  57.         String sql= "INSERT INTO \"main\"."+tbl+" (\"Name\",\"Date\") VALUES (?1,?2)";
  58.         try{
  59.             conn = DriverManager.getConnection(url);
  60.             PreparedStatement pstmt = conn.prepareStatement(sql);
  61.             pstmt.setString(1, nm);
  62.             pstmt.setString(2, dt);
  63.             pstmt.executeUpdate();
  64.             System.out.println("Inserted succesfully");
  65.         }catch(SQLException e){
  66.             System.out.println(e.getMessage());
  67.         }finally{
  68.             try{
  69.                 if(conn != null){
  70.                     conn.close();
  71.                     }
  72.             }catch(SQLException e1){
  73.                 System.out.println(e1.getMessage());
  74.                 }
  75.             }
  76.     }
  77.    
  78.     public static void deleteTable(String db){
  79.         String url="jdbc:sqlite:"+db+".sqlite";
  80.         Connection conn = null;
  81.         String sql="DROP TABLE \"CLIENTS\"";
  82.        
  83.     try{
  84.         conn = DriverManager.getConnection(url);
  85.         Statement stmt = conn.createStatement();
  86.         stmt.execute(sql);
  87.         System.out.println("table deleted succesfully");
  88.     }catch(SQLException e){
  89.         System.out.println(e.getMessage());
  90.     }finally{
  91.         try{
  92.             if(conn != null){
  93.                 conn.close();
  94.                 }
  95.         }catch(SQLException e1){
  96.             System.out.println(e1.getMessage());
  97.             }
  98.         }
  99.     }
  100.     public static void deleteColumn(String db){
  101.         String url="jdbc:sqlite:"+db+".sqlite";
  102.         Connection conn = null;
  103.         String sql="DELETE FROM PROVIDERS WHERE Name = Thanos ";
  104.     try{
  105.         conn = DriverManager.getConnection(url);
  106.         Statement stmt = conn.createStatement();
  107.         stmt.execute(sql);
  108.         System.out.println("column deleted succesfully");
  109.     }catch(SQLException e){
  110.         System.out.println(e.getMessage());
  111.     }finally{
  112.         try{
  113.             if(conn != null){
  114.                 conn.close();
  115.                 }
  116.         }catch(SQLException e1){
  117.             System.out.println(e1.getMessage());
  118.             }
  119.         }
  120.     }
  121.    
  122. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement