I want to create a blank Oracle 12c database programmaticaltlly with Java. But if I use getConnection() method in JDBC driver, it does not allow me to run a CREATE DATABASE statement with since I have to mention a spwcific SID/service name for the connection.
Is there a way that I can connect to Oracle server without specifying a service name/SID in the connection string? and create a blank database from within a Java application?
Related
I use H2 database (file) in my Java app and due to some data appearance problem in IntelliJ, I use file option instead of memory.
Here is my url setting in application.properties:
spring.datasource.url=jdbc:h2:file:~/test-db;
DB_CLOSE_DELAY=-1;DATABASE_TO_UPPER=false;AUTO_SERVER=TRUE
I can connect and see table data via IntelliJ and H2 console by using the url parameter as shown below:
jdbc:h2:file:~/test-db
However, even I connect to the database successfully, the table data is not seen in DBeaver as shown below:
I think I should use tcp option to connect H2 daabase, but I cannot by using the following settings:
spring.datasource.url=jdbc:h2:tcp://localhost/~/test-db
or
jdbc:h2:tcp://localhost/C:/test-db
and get "Unable to open JDBC Connection for DDL execution" error (I use Windows). Any idea?
You're using a file-associated connection to that in-memory DB that can only be used by 1 tool at once.
So when IntelliJ is connected it generates a lock-file to protect it against multi-access.
I would suggest a TCP connection that allows multiple connections - see redundant question here:
H2 database error: Database may be already in use: "Locked by another process"
I'm trying to find a URL I could connect to in building a demo app for teaching purposes.
Anyone aware of one I could use with JDBC?
You could probably set up a database in a local file and connect to that via JDBC.
Two examples of database drivers that are freely available are:
SQLite Java, to connect to SQLite databases.
UCanAccess, to connect to MS Access databases.
Then you can connect to it via JDBC like this:
Connection conn = DriverManager.getConnection("jdbc:sqlite://<db file path>");
// or
Connection conn = DriverManager.getConnection("jdbc:ucanaccess://<mdb or accdb file path>");
The respective links contain more in-depth tutorials.
You can connect to a file and then set up the database via Java by executing SQL statements. The result is persisted in the file, which you could then use and redistribute for teaching.
I have an sql script which creates temp tables valid for only that session. Now after running the script, I am trying to read data from the table through spark and then process it. Below is the code I have code for spark read.
sparkSession.read().format("jdbc").option("url",
jdbcURL).option("dbtable", tableOrQuery).option("user",
userName).option("password", password)
.option("driver", driverName).load();
Now I need to pass the jdbc connection I created so that spark can read data in the same session. Is this possible ?
No, you cannot pass jdbc connection to spark. It will manage JDBC connection by itself.
JdbcRelationProvider Create Connection
JdbcUtils connect
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 5 years ago.
Improve this question
how would i saver the state of a java program so that if i had to give the copy from the dist folder that it would still work on another computer without for instance the other computer having MySQL installed etc
Instead of MySQL, you can use 100% Java relational databases such as Apache Derby, H2 Database engine or the HyperSQL. Starting at Java 6, the SDK included Derby as JavaDB. There is a Getting Started Guide in the Java DB 10 (JDK 8) documentation.
You can include the .jar files in your project and use the databases using a JDBC connection without installing any additional software. Application servers such as Glassfish and JBoss uses, by default, Derby and H2 for the example databases and JMS messages. Usually, administrators change that databases (using a different JDBC URL connection) in production.
Notes on using JavaDb or Derby
Derby (and JavaDB) can run as a network server or an embedded one. The network server allows connections from multiple users/programs. An embedded server only works with the application that starts the engine. Both server types are included in the .jar files and you can control them using JDBC and the library API.
For a network server, you must start the server and use a JDBC URL connection like jdbc:derby://localhost:1527/dbname;create=true where a port and a database are defined or a connection such as jdbc:derby://localhost:1527/c:\temp\myDatabase;create=true with the location of the database. You may check the Vogella's tutorial for Apache Derby
// use the Derby JDBC driver
Class.forName("org.apache.derby.jdbc.ClientDriver").newInstance();
// connect to the database
Connection connect = DriverManager
.getConnection("jdbc:derby://localhost/c:/temp/db/FAQ/db");
// execute a Query
PreparedStatement statement = connect
.prepareStatement("SELECT * from USERS");
ResultSet resultSet = statement.executeQuery();
:
For a embedded server, you must use a different JDBC driver and URL. The Connection URL is like jdbc:derby:MyDbTest;create=true, and does not include a port. There is an explanation in the Derby's documentation.
// use the Embedded Derby JDBC driver
Class.forName("org.apache.derby.jdbc.EmbeddedDriver").newInstance();
// connect to the database
Connection connect = DriverManager
.getConnection("jdbc:derby:MyDBTest;create=true");
// execute a Query
PreparedStatement statement = connect
.prepareStatement("SELECT * from USERS");
ResultSet resultSet = statement.executeQuery();
:
Method 1:
If there is no very complicated data like you would save it in a table of a database, you could use for example a simple config file.
Method 2:
Otherwise you could just use a sqllite database. That's like a database in a single file, that doesn't need a server to run.
Method 3:
You could use a remote database. You would have access to the data from anywhere. (Even without copying the Application every time)
Method 4:
Use a "Java" database, like for example H2.
Method ...: There are many other things you can do, to achieve this!
When I tried to establish a connection with the Oracle Database, I had to write
Connection CON = DriverManager.getConnection("jdbc:odbc:Dan", "system", "noodles");
Here, Dan is the data source name, isn't it? What if I created a table called cBC when the data source was Dan and what if I rename the data source and enter further rows into the table? What difference does it make?
Dan is the name of a ODBC connection configured on your machine. The name itself does not matter, as long as the database that it is configured to connect to is the same, it doesn't matter if you call it Dan, MyDatabase or foobar.
Note that this specific way to access a database that is configured externally is not a thing that JDBC does in general, it's a specific behaviour of the JDBC-ODBC bridge (which lets you access ODBC connections via JDBC).
Other JDBC drivers (such as MySQL) use a different syntax where the necessary configuration for accessing the database is encoded in the URL: jdbc:mysql://myDbServer/myDbName.
Note also that the JDBC-ODBC bridge was never intended for production-quality DB connections (it will even be removed in Java 8!). It is just a quick way to use an existing setup.
For Oracle DB connections you should instead use the appropriate Type 4 driver from Oracle. Those drivers use an URL in the form jdbc:oracle:thin:#//<host>:<port>/ServiceName (generally speaking, the part after jdbc: identifies the JDBC driver to be used).