[英]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.我创建了一组修改数据库的 SQL 查询,现在我想测试它们。
How can I create a local and temporary PostgreSQL database to test my queries.如何创建本地和临时 PostgreSQL 数据库来测试我的查询。 I'm working in Java.
我在 Java 工作。
You CAN create and drop PostgreSQL db's via Java using the Statement object.您可以使用 Statement 对象通过 Java 创建和删除 PostgreSQL 数据库。
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 http://www.jvmhost.com/articles/create-drop-databases-dynamically-java-jsp-code
Creating a database is simple enough once your database cluster is in place.一旦您的数据库集群就位,创建数据库就足够简单了。
Connect to the maintenance database postgres
(installed by default) and issue连接到维护数据库
postgres
(默认安装)并发出
CREATE DATABASE testdb;
The database will be created and you can connect to it now.将创建数据库,您现在可以连接到它。 Of course you need to have the necessary privileges to create a database .
当然,您需要具有创建数据库所需的权限。
You can see her how to create database Hope this help你可以看看她如何创建数据库 希望这有助于
http://www.tutorialspoint.com/jdbc/jdbc-create-database.htm http://www.tutorialspoint.com/jdbc/jdbc-create-database.htm
You must connect first to the localhost您必须先连接到本地主机
//STEP 1. Import required packages
import java.sql.*;
public class JDBCExample {
// JDBC driver name and database URL
static final String JDBC_DRIVER = "com.mysql.jdbc.Driver";
static final String DB_URL = "jdbc:mysql://localhost/";
// Database credentials
static final String USER = "username";
static final String PASS = "password";
public static void main(String[] args) {
Connection conn = null;
Statement stmt = null;
try{
//STEP 2: Register JDBC driver
Class.forName("com.mysql.jdbc.Driver");
//STEP 3: Open a connection
System.out.println("Connecting to database...");
conn = DriverManager.getConnection(DB_URL, USER, PASS);
//STEP 4: Execute a query
System.out.println("Creating database...");
stmt = conn.createStatement();
String sql = "CREATE DATABASE STUDENTS";
stmt.executeUpdate(sql);
System.out.println("Database created successfully...");
}catch(SQLException se){
//Handle errors for JDBC
se.printStackTrace();
}catch(Exception e){
//Handle errors for Class.forName
e.printStackTrace();
}finally{
//finally block used to close resources
try{
if(stmt!=null)
stmt.close();
}catch(SQLException se2){
}// nothing we can do
try{
if(conn!=null)
conn.close();
}catch(SQLException se){
se.printStackTrace();
}//end finally try
}//end try
System.out.println("Goodbye!");
}//end main
}//end JDBCExample
Your question is little bit wrong,with Java code you cannot create a database,you can just connect to a database.您的问题有点错误,使用 Java 代码无法创建数据库,您只能连接到数据库。
First of all you need to create a database in PgAdminIII.首先,您需要在 PgAdminIII 中创建一个数据库。
Here is the code which will help you to create table in postgresql database through JAVA这是帮助您通过 JAVA 在 postgresql 数据库中创建表的代码
package database;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.logging.Level;
import java.util.logging.Logger;
public class Database {
public static void main(String args[]) {
try {
Connection c = null;
Statement stmt = null;
try {
Class.forName("org.postgresql.Driver");
c = DriverManager.getConnection("jdbc:postgresql://localhost:5432/kanwar","postgres", "osm");
} catch (Exception e) {
e.printStackTrace();
System.err.println(e.getClass().getName()+": "+e.getMessage());
System.exit(0);
}
System.out.println("Opened database successfully");
try {
stmt = c.createStatement();
} catch (SQLException ex) {
Logger.getLogger(Database.class.getName()).log(Level.SEVERE, null, ex);
}
String sql = "CREATE TABLE MY_TABLE "+
"(ID INT NOT NULL,"
+ "NAME TEXT NOT NULL,"
+ "AGE INT NOT NULL)";
stmt.executeUpdate(sql);
stmt.close();
c.close();
} catch (SQLException ex) {
Logger.getLogger(Database.class.getName()).log(Level.SEVERE, null, ex);
}
catch(Exception e)
{
System.err.println( e.getClass().getName()+": "+ e.getMessage() );
System.exit(0);
}
System.out.println("Table Created Successfully");
}
}
For complete reference: http://kodingpoint.blogspot.in/2014/01/java-postgresql-connectivity-example.html完整参考: http : //kodingpoint.blogspot.in/2014/01/java-postgresql-connectivity-example.html
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.