Click here to Skip to main content
15,886,578 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
package test;
import java.sql.Connection;
import java.sql.DriverManager;

class connection {
public static void main(String args[]) {
Connection c = null;
try {
Class.forName("org.postgresql.Driver");
c = DriverManager
.getConnection("jdbc:postgresql://localhost:5432/testdb",
"postgres", "");

} catch (Exception e) {

System.err.println(e.getClass().getName()+": "+e.getMessage());
System.exit(0);
}
System.out.println("Opened database successfully");
}
}


/*here is my code for connection now how to add a new database using this pgm here the testdb is already exissting database...i wish to add a new database what to do*/
Posted
Updated 14-Jul-14 23:15pm
v3
Comments
Member 10946695 15-Jul-14 4:45am    
i am not asking for how to create database in postgres how can i create database in postgres using java code that's my question ..help me..

You can add a new db like this:
C#
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;

public class Postgre {
	public static void main(String[] args) {
		try {
			Connection c = null;
			Statement statement = null;

			try {

				c = DriverManager.getConnection(
						"jdbc:postgresql://localhost:5432/", "postgres", "your_password");

			} catch (Exception e) {
				System.exit(0);
			}
			
			statement = c.createStatement();
			statement.executeUpdate("create database mydb");
			statement.close();
			c.close();
		} catch (SQLException ex) {

		}

	}
}
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900