Guest User

Untitled

a guest
Feb 28th, 2018
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.81 KB | None | 0 0
  1. public static void createTable(){
  2.  
  3. //connect to database
  4. String url = percorsoDatabase;
  5.  
  6. //Statement create new table
  7. String sql = "CREATE TABLE IF NOT EXISTS Tabella (id PRIMARY KEY," +
  8. "video TEXT," +
  9. "game TEXT," +
  10. "firstAction TEXT," +
  11. "secondAction TEXT," +
  12. "thirdAction TEXT);";
  13.  
  14. try {
  15. Connection conn = DriverManager.getConnection(url);
  16. Statement stmt = conn.createStatement();
  17. stmt.execute(sql);
  18. } catch (SQLException e ){
  19. System.out.println(e.getMessage());
  20. }
  21.  
  22. }
  23.  
  24. public static void main(String[] args) {
  25.  
  26. createNewDatabase();
  27. connection();
  28. createTable();
  29. }
  30.  
  31. public static void createNewDatabase(){
  32.  
  33. String url = percorsoDatabase;
  34.  
  35. Connection conn;
  36. try {
  37.  
  38. conn = DriverManager.getConnection(url);
  39. if (conn != null){
  40. DatabaseMetaData meta = conn.getMetaData();
  41. System.out.println("The driver name is" + meta.getDriverName());
  42. System.out.println("A new database has been created.");
  43. }
  44. } catch (SQLException e){
  45. System.out.println(e.getMessage());
  46. }
  47.  
  48. }
  49.  
  50. public static void connection(){
  51.  
  52. Connection conn = null;
  53. try {
  54. //String url = "jdbc:sqlite://Volumes/Isma/Documenti/SUPSI/APA/Stage/"
  55. // + "Beans/esperimento/dati.db";
  56. conn = DriverManager.getConnection(percorsoDatabase);
  57. System.out.println("Connection to SQLite established.");
  58. } catch (SQLException e){
  59. System.out.println(e.getMessage());
  60. } finally {
  61. try {
  62. if (conn != null){
  63. conn.close();
  64. }
  65. } catch (SQLException e){
  66. System.out.println(e.getMessage());
  67. }
  68. }
  69.  
  70. }
  71.  
  72. url - a database url of the form jdbc:subprotocol:subname
Add Comment
Please, Sign In to add comment