how to Create if not exists a new Java DB? - java

conn = DriverManager.getConnection("jdbc:derby:mydatabase;create=true",props);
will this line make my DB get overwritten everytime i execute it? if it will how do i create the DB once and just use it ever since?

What you are doing will work with Derby. It will create the database if it doesn't exist and do nothing if it already does exist.

Related

H2 Database Backup Restore Sequence Already Exists Exception

I have an application that uses an embedded H2 database. I want to implement a backup and restore feature. I can successfully backup my data, but I get a hurdle when I try to restore it. This is how i'm backing it up:
String DBQ = "SCRIPT TO ?";
PreparedStatement myStatement = connection.prepareStatement(DBQ);
myStatement.setString(1, backupFile.getAbsolutePath());
myStatement.executeQuery();
I get my backup file generated just fine with the data. I can't seem to restore it however. This is how I try to restore it:
String DBQ = "RUNSCRIPT FROM ?";
PreparedStatement preparedStatement = connection.prepareStatement(DBQ);
preparedStatement.setString(1,unencryptedFile.getAbsolutePath());
preparedStatement.executeUpdate();
I get a org.h2.jdbc.JdbcSQLSyntaxErrorException: Sequence already exists. I've looked around and pretty much the only thing I could find on it was from 2013, with no answer Backup and Restore h2 and I'm sure my database isn't corrupted. I can't seem to find any documentation to help me resolve this too. How can I resolve this?
Here is an whole example how to backup+restore:
https://gist.github.com/cwdesautels/11188409

Java Connection String to query from two database

I am having a problem. I have a query that checks one database table and updates another database table. I am using MySQL 5.1
UPDATE dldd.temp,test.temp
SET test.temp.name = dldd.temp.word
WHERE dldd.temp.id = test.temp.id
this is my SQL statement that is working fine. Now I want to execute this statement using Java PreparedStatement . The problem is I don't know how to write the Connection String to select two database i.e
"jdbc:mysql://localhost:3306/"+dbname+"?characterEncoding=UTF-8"
What should come in place of dbname. Can I select multiple db there.
Have a look at http://dev.mysql.com/doc/connector-j/en/connector-j-reference-configuration-properties.html.
If the database is not specified, the connection is made with no default database. In this case, either call the setCatalog() method on the Connection instance, or fully specify table names using the database name (that is, SELECT dbname.tablename.colname FROM dbname.tablename...) in your SQL. Opening a connection without specifying the database to use is generally only useful when building tools that work with multiple databases, such as GUI database managers.

Hibernate save SQL exception

Hi guys I am trying to save an object to a MySQL data base via Hibernate. if I execute following code
User user = new User();
user.setData_1("my data 5");
user.setFirstname("Freddy");
user.setLastname("Bob");
user.setId(5);
session.save(user);
session.getTransaction().commit();
I get a
'com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: Table 'xxx.my_table_1' doesn't exist'
exception. However, querying from the same table using the same config works just fine.
What could be the issue?
Check your connection string in the configuration something like hibernate.connection.url = jdbc:postgresql://localhost/mydatabase You may be missing the schema name in the url(mydatabase).
So, after some trial and error, I came to find out that the issue with the .get() (and apparently .save() too) was that I did not have a hibernate.default_schema set in config. Looks like it is used to create the 'dynamic' SQL for .save() and .get(), but if you use .createSQLQuery(), it just uses what ever String you pass as an argument for the SQL, and therefor works with out needing to have hibernate.default_schema set.

Updating an HSQLDB table from Eclipse

I'm currently using Java to access a .sql file (called patient.sql). Running queries and updating the table works well while the program is running, but the changes aren't made on disk.
So, for example, I have a 30 node database with some fields including caseID (primary key) and Hospital. I want to change the Hospital of the node with caseID = Case29. To do this, I use the following code:
// Prepare a statement to update a record
String sql = "UPDATE patient SET Hospital='CX' WHERE caseID = 'Case29'";
// Execute the insert statement
stmt.executeUpdate(sql);
I have checked this and seen that it works (using a quick System.out.println()). However, when I finish the program and open the patient.sql, my change has not been registered. How can I save this change made?
Cheers
EDIT: I'm using HSQLDB
If you are using HSQLDB changes are stored in a .log file until SHUTDOWN is called.
After a SHUTDOWN, all changes are moved to a .script file.
One description of HSQLDB files here:
http://hsqldb.org/doc/guide/ch01.html
In your case I suspect no SHUTDOWN has been called.

How to Create a MySql Database using JDBC

Can we create a new MySQL database( not a table, I want a new database) from java code ? I have searched the internet and I get code only for connecting with existing mysql databases. Can i create a new database at run time using java code.
please help
In your connection string omit a database name and then execute Create database command.
Connection String: jdbc:mysql://localhost
Create Database Syntax : stm.executeUpdate("CREATE DATABASE dbname")
Class.forName("org.gjt.mm.mysql.Driver").newInstance();
String url ="jdbc:mysql://localhost/myDB?user=soft&password=soft1234&useUnicode=true&characterEncoding=8859_1";
Connection conn = DriverManager.getConnection(url);

Categories