Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I create a PostgreSQL database in Java?

I have created a set of SQL queries that modify a database, and now I want to test them.

How can I create a local and temporary PostgreSQL database to test my queries. I'm working in Java.

like image 357
user869778 Avatar asked Oct 30 '11 13:10

user869778


People also ask

Does PostgreSQL support Java?

PostgreSQL JDBC Driver allows Java programs to connect to a PostgreSQL database using standard, database independent Java code. Is an open source JDBC driver written in Pure Java (Type 4), and communicates in the PostgreSQL native network protocol.

Can you make a database in Java?

To create the database tables in Java DB, the database server included with Application Server, you need to create the database connection and execute the SQL commands in tut-install /examples/common/sql/javadb/tutorial.


1 Answers

You CAN create and drop PostgreSQL db's via Java using the Statement object.

Example:

Connection c = DriverManager.getConnection("jdbc:postgresql://localhost:5432/", "username", "password");
Statement statement = c.createStatement();
statement.executeUpdate("DROP DATABASE mydb");

See the following link for a good starting point:

http://www.jvmhost.com/articles/create-drop-databases-dynamically-java-jsp-code

like image 198
bosvos Avatar answered Oct 24 '22 11:10

bosvos