Java: embedded database

I want to create a desctop application with an embedded database. Database - JavaDB (Derby). I connected the jar file of the derby.jar file to my project. The problem is that I do not understand how to register the driver to use this database. They say that I should use Class.forName ("org.apache.derby.jdbc.EmbeddedDriver") But what if it was a different database and its driver was not in the standard java package? As you can see, I am confused by this. I want to know how to use the derby.jar I plugged in, how to work with its jdbc driver, and how to create tables in the specified directory.

Please give a detailed answer as you can. (I am a mannequin in this =)))

+3
source share
4 answers

:

Class.forName("org.apache.derby.jdbc.EmbeddedDriver");

JDBC Derby, JDBC java.sql.DriverManager , . , . , , - , .

, database.properties:

jdbc.driver=org.apache.derby.jdbc.EmbeddedDriver
jdbc.url=jdbc:derby:derbyDB;create=true
jdbc.username=dbusername
jdbc.password=dbpassword

:

InputStream in = new FileInputStream("database.properties");
Properties props = new Properties();
props.load(in);
in.close();

String driver = props.getProperty("jdbc.driver");
Class.forName(driver);

String url = props.getProperty("jdbc.url");
String username = props.getProperty("jdbc.username");
String password = props.getProperty("jdbc.password");

Connection conn = DriverManager.getConnection(url, username, password);

Apache Derby Sun JDBC.

+5

Derby, URL- JDBC-, :

Connection conn = DriverManager.getConnection("jdbc:derby:c:/otherDirectory/myDB");

. Apache Derby; Derby.

+2

Derby Java, Sun Windows, , , OS X.

, jar Class.forName("") .

+1

Driver registration is not required at all if you are using Java 6, which comes with JDBC 4. JDBC 4 requests the driver required from the connection URL. The driver, of course, should be somewhere on your way to classes ... Otherwise, I would answer Jesper's second question.

+1
source

Source: https://habr.com/ru/post/1747147/


All Articles