Guest User

Untitled

a guest
Oct 31st, 2018
190
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.16 KB | None | 0 0
  1. import java.sql.Connection;
  2. import java.sql.DriverManager;
  3. import java.sql.SQLException;
  4. import java.sql.Statement;
  5. import java.sql.ResultSet;
  6. import java.sql.DatabaseMetaData;
  7. import org.apache.commons.io.FileUtils;
  8. import java.io.*;
  9.  
  10. public class DBConnection{
  11.  
  12. public static void main(String[] args) {
  13. System.out.print('u000C');
  14. Connection conn = getDB("test2");
  15.  
  16. String[] cols = {"fName","lName","rowID"},
  17. type = {"varchar(20)","varchar(25)","real"};
  18. String name = "myTable",
  19. primaryKey = "rowID";
  20.  
  21. try{
  22. if (conn != null){
  23. conn.close();
  24. System.out.println("close connection from main");
  25. }
  26.  
  27. File file = new File("test2");
  28. System.out.println(file.exists());
  29. file.setWritable(true);
  30. System.out.println(file.canWrite());
  31. FileUtils.deleteDirectory(new File("test2"));
  32.  
  33. System.exit(0);
  34. }catch(IOException | SQLException e){
  35. System.out.println("FATAL ERROR: " + e);
  36. System.exit(0);
  37. }
  38. System.exit(0);
  39. }
  40.  
  41. public static Connection getDB(String database) {
  42. try{
  43. String URL;
  44.  
  45. /* jdbc:derby specifies the driver to use to connect to the derby database
  46. * database is the name of the database we want to connect to. A database can hold many tables
  47. * create=true is an option that creates and connects to the database if it does not exist, or just connect if it already exists.
  48. */
  49.  
  50. File db =new File(database);
  51.  
  52. if(db.exists()) URL = "jdbc:derby:" + database + ";create=false" ;
  53. else URL = "jdbc:derby:" + database + ";create=true";
  54.  
  55. System.out.println("db exists " + db.exists());
  56.  
  57. Connection conn = DriverManager.getConnection(URL);
  58. System.out.println("Succesfully connected to " + database);
  59.  
  60. return conn;
  61. }catch(SQLException e){
  62. System.out.println("FATAL ERROR: from getDB " + e);
  63.  
  64. return null;
  65. }
  66. }
Add Comment
Please, Sign In to add comment