Advertisement
Guest User

Untitled

a guest
Jun 19th, 2018
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.02 KB | None | 0 0
  1. try {
  2.         Class.forName("org.sqlite.JDBC");
  3.         }
  4.         catch (ClassNotFoundException e) {
  5.         e.printStackTrace();
  6.         }
  7.  
  8.         try(Connection con = DriverManager.getConnection("jdbc:sqlite:ice.db")){
  9.         executeStatement(con);
  10.         }
  11.  
  12.         catch (SQLException e){
  13.         e.printStackTrace();
  14.         }
  15.  
  16.  
  17. public static executeStatement(Connection con){
  18.         create(con);
  19.         delete(con,1);
  20.         insert (con, 1, "Songname","Playlist",long id,);
  21.         select(con);
  22.         }
  23.  
  24. private static void create(Connection con){
  25.         try(PreparedStatement pstmt = con.prepareStatement(
  26.         "CREATE TABLE IF NOT EXISTS ice (id long, name text, interpret text, album text); ")){
  27.         pstmt.executeUpdate();
  28.         } catch (SQLException e){
  29.         e.printStackTrace();
  30.         }
  31.         }
  32.  
  33. private static boolean insert(Connection con, long id, String name,String interpret, String album); {
  34.         try (PreparedStatement pstmt = con.prepareStatement(
  35.         "INSERT INTO ice (id, name, interpret, album) VALUES (?,?,?,?);")){
  36.         pstmt.setLong(1,id);
  37.         pstmt.setString(2,name);
  38.         pstmt.setString(3, interpret);
  39.         pstmt.setString(4,album);
  40.         pstmt.executeUpdate();
  41.         return true;
  42.         }catch(SQLException e){
  43.         e.printStackTrace();
  44.         }
  45.  
  46. private static void delete(Connection con, long id){
  47.         try(PreparedStatement pstmt = con.prepareStatement(
  48.         "DELETE FROM ice WHERE id = ?:")){
  49.         pstmt.setLong(1,id);
  50.         pstmt.executeUpdate();
  51.         }catch (SQLException e){
  52.         e.printStackTrace();
  53.         }
  54.         }
  55.  
  56. private static void select (Connection con){
  57.         try(PreparedStatement pstmt = con.prepareStatement(
  58.         "SELECT * FROM PIZZA;");
  59.         ResultSet rs = pstmt.executeQuery()){
  60.         while (rs.next()){
  61.         System.out.println(rs.getLong("id"));
  62.         }
  63.         }catch (SQLException e){
  64.         e.printStackTrace();
  65.         }
  66.         }
  67.  
  68.         }
  69.         }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement