Advertisement
Guest User

Untitled

a guest
Jun 2nd, 2017
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.75 KB | None | 0 0
  1. public static Vector getTableData() throws SQLException, ClassNotFoundException{
  2.         //connect to database
  3.         Vector data = new Vector();
  4.         Class.forName("org.sqlite.JDBC");
  5.         Connection conn = DriverManager.getConnection("jdbc:sqlite:prova.db");
  6.         Statement stmt = conn.createStatement();
  7.         //read data from db
  8.         //  Read data from a table
  9.         String sql = "SELECT * FROM people";
  10.         ResultSet rs = stmt.executeQuery( sql );
  11.         ResultSetMetaData md = rs.getMetaData();
  12.         int columns = md.getColumnCount();
  13.  //  Get column names
  14.            
  15.          //  Get row data
  16.            while (rs.next())
  17.            {
  18.               Vector row = new Vector(columns);
  19.               for (int i = 1; i <= columns; i++)
  20.               {
  21.                  row.addElement(rs.getObject(i));
  22.               }
  23.               data.addElement(row);
  24.            }
  25.            rs.close();
  26.            stmt.close();
  27.            return data;
  28.     }
  29.    
  30.     public static Vector getColumnNames() throws SQLException, ClassNotFoundException{
  31.          //connect to database
  32.         Vector columnNames = new Vector();
  33.         Class.forName("org.sqlite.JDBC");
  34.         Connection conn = DriverManager.getConnection("jdbc:sqlite:prova.db");
  35.         Statement stmt = conn.createStatement();
  36.         //read data from db
  37.         //  Read data from a table
  38.         String sql = "SELECT * FROM people";
  39.         ResultSet rs = stmt.executeQuery( sql );
  40.         ResultSetMetaData md = rs.getMetaData();
  41.         int columns = md.getColumnCount();
  42.  //  Get column names
  43.            for (int i = 1; i <= columns; i++)
  44.            {
  45.               columnNames.addElement( md.getColumnName(i) );
  46.            }
  47.         return columnNames;
  48.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement