Advertisement
Guest User

Untitled

a guest
Jul 19th, 2017
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.62 KB | None | 0 0
  1. package training;
  2.  
  3. import java.sql.*;
  4.  
  5. public class SqliteExample {
  6.     public final static String DATABASE = "jdbc:sqlite:myExample.db";
  7.  
  8.     public static void main(String[] args) {
  9.         if (!tableExists()) {
  10.             createTable();
  11.         }
  12.        
  13.         //addRow("Ford", "1999");
  14.         //delRow("Ford");
  15.        
  16.         StringBuffer results = getRows();
  17.        
  18.         System.out.println(results);
  19.     }
  20.    
  21.     private static void createTable() {
  22.         Connection conn = null;
  23.         Statement st = null;
  24.  
  25.         try {
  26.             Class.forName("org.sqlite.JDBC");
  27.             conn = DriverManager.getConnection(DATABASE);
  28.             st = conn.createStatement();
  29.             st.executeUpdate("CREATE TABLE `Vehicle` (" + "`id` INTEGER PRIMARY KEY,"
  30.                     + "`name` varchar(32) NOT NULL DEFAULT 'Name',"
  31.                     + "`year` varchar(4) NOT NULL DEFAULT '0000');");
  32.         } catch (SQLException e) {
  33.             System.out.println("Error: " + e);
  34.         } catch (ClassNotFoundException e) {
  35.             System.out.println("Error loading org.sqlite.JDBC: " + e);
  36.         } finally {
  37.             try {
  38.                 if (conn != null)
  39.                     conn.close();
  40.                 if (st != null)
  41.                     st.close();
  42.             } catch (SQLException e) {
  43.                 System.out.println("Could not create the table (on close): " + e);
  44.             }
  45.         }
  46.     }
  47.    
  48.     public static StringBuffer getRows() {
  49.         StringBuffer sb = new StringBuffer();
  50.         Connection conn = null;
  51.         Statement statement = null;
  52.         ResultSet set = null;
  53.         int iSize = 0;
  54.  
  55.         try {
  56.             Class.forName("org.sqlite.JDBC");
  57.             conn = DriverManager.getConnection(DATABASE);
  58.             statement = conn.createStatement();
  59.             set = statement.executeQuery("SELECT * FROM Vehicle");
  60.  
  61.             while (set.next()) {
  62.                 iSize++;
  63.                 int id = set.getInt("id");
  64.                 String name = set.getString("name");
  65.                 String year = set.getString("year");
  66.                
  67.                 sb.append("[id: " + id + ", name: " + name + ", year: " + year + "]\n");
  68.             }
  69.  
  70.             System.out.println(iSize + " rows retrieved from the " + DATABASE + " database");
  71.         } catch (SQLException e) {
  72.             System.out.println("Error: " + e);
  73.         } catch (ClassNotFoundException e) {
  74.             System.out.println("Error: " + e);
  75.         } finally {
  76.             try {
  77.                 if (statement != null)
  78.                     statement.close();
  79.                 if (set != null)
  80.                     set.close();
  81.                 if (conn != null)
  82.                     conn.close();
  83.             } catch (SQLException e) {
  84.                 System.out.println("Error: " + e);
  85.             }
  86.         }
  87.  
  88.         return sb;
  89.     }
  90.    
  91.     public static boolean addRow(String sName, String sYear) {
  92.         Connection conn = null;
  93.         PreparedStatement ps = null;
  94.         boolean bSuccess = false;
  95.  
  96.         try {
  97.             Class.forName("org.sqlite.JDBC");
  98.             conn = DriverManager.getConnection(DATABASE);
  99.  
  100.             ps = conn.prepareStatement("INSERT INTO Vehicle (id, name, year) VALUES (null,?,?)");
  101.             ps.setString(1, sName);
  102.             ps.setString(2, sYear);
  103.             ps.executeUpdate();
  104.             bSuccess = true;
  105.         } catch (SQLException e) {
  106.             System.out.println("Error: " + e);
  107.         } catch (ClassNotFoundException e) {
  108.             System.out.println("Error: " + e);
  109.         } finally {
  110.             try {
  111.                 if (ps != null) {
  112.                     ps.close();
  113.                 }
  114.                 if (conn != null)
  115.                     conn.close();
  116.             } catch (SQLException e) {
  117.                 System.out.println("Error: " + e);
  118.                 bSuccess = false;
  119.             }
  120.         }
  121.  
  122.         return bSuccess;
  123.     }
  124.  
  125.     public static boolean delRow(String sName) {
  126.         Connection conn = null;
  127.         PreparedStatement ps = null;
  128.         ResultSet set = null;
  129.         boolean bSuccess = false;
  130.  
  131.         try {
  132.             Class.forName("org.sqlite.JDBC");
  133.             conn = DriverManager.getConnection(DATABASE);
  134.             ps = conn.prepareStatement("DELETE FROM Vehicle WHERE name = ?");
  135.             ps.setString(1, sName);
  136.             ps.executeUpdate();
  137.             bSuccess = true;
  138.         } catch (SQLException e) {
  139.             System.out.println("Error: " + e);
  140.         } catch (ClassNotFoundException e) {
  141.             System.out.println("Error: " + e);
  142.         } finally {
  143.             try {
  144.                 if (ps != null) {
  145.                     ps.close();
  146.                 }
  147.                 if (set != null) {
  148.                     set.close();
  149.                 }
  150.                 if (conn != null)
  151.                     conn.close();
  152.             } catch (SQLException e) {
  153.                 System.out.println("Error: " + e);
  154.                 bSuccess = false;
  155.             }
  156.         }
  157.  
  158.         return bSuccess;
  159.     }
  160.    
  161.     private static boolean tableExists() {
  162.         Connection conn = null;
  163.         ResultSet rs = null;
  164.  
  165.         try {
  166.             Class.forName("org.sqlite.JDBC");
  167.             conn = DriverManager.getConnection(DATABASE);
  168.             DatabaseMetaData dbm = conn.getMetaData();
  169.             rs = dbm.getTables(null, null, "Vehicle", null);
  170.  
  171.             if (!rs.next())
  172.                 return false;
  173.  
  174.             return true;
  175.         } catch (SQLException e) {
  176.             System.out.println("Error: " + e);
  177.             return false;
  178.         } catch (ClassNotFoundException e) {
  179.             System.out.println("Error: " + e);
  180.             return false;
  181.         } finally {
  182.             try {
  183.                 if (rs != null)
  184.                     rs.close();
  185.  
  186.                 if (conn != null)
  187.                     conn.close();
  188.             } catch (SQLException e) {
  189.                 System.out.println("Error: " + e);
  190.             }
  191.         }
  192.     }
  193. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement