Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.sql.Connection;
- import java.sql.DriverManager;
- import java.sql.SQLException;
- import java.sql.Statement;
- import java.sql.ResultSet;
- import java.sql.DatabaseMetaData;
- import org.apache.commons.io.FileUtils;
- import java.io.*;
- public class DBConnection{
- public static void main(String[] args) {
- System.out.print('u000C');
- Connection conn = getDB("test2");
- String[] cols = {"fName","lName","rowID"},
- type = {"varchar(20)","varchar(25)","real"};
- String name = "myTable",
- primaryKey = "rowID";
- try{
- if (conn != null){
- conn.close();
- System.out.println("close connection from main");
- }
- File file = new File("test2");
- System.out.println(file.exists());
- file.setWritable(true);
- System.out.println(file.canWrite());
- FileUtils.deleteDirectory(new File("test2"));
- System.exit(0);
- }catch(IOException | SQLException e){
- System.out.println("FATAL ERROR: " + e);
- System.exit(0);
- }
- System.exit(0);
- }
- public static Connection getDB(String database) {
- try{
- String URL;
- /* jdbc:derby specifies the driver to use to connect to the derby database
- * database is the name of the database we want to connect to. A database can hold many tables
- * create=true is an option that creates and connects to the database if it does not exist, or just connect if it already exists.
- */
- File db =new File(database);
- if(db.exists()) URL = "jdbc:derby:" + database + ";create=false" ;
- else URL = "jdbc:derby:" + database + ";create=true";
- System.out.println("db exists " + db.exists());
- Connection conn = DriverManager.getConnection(URL);
- System.out.println("Succesfully connected to " + database);
- return conn;
- }catch(SQLException e){
- System.out.println("FATAL ERROR: from getDB " + e);
- return null;
- }
- }
Add Comment
Please, Sign In to add comment