如何在 Java 中创建 PostgreSQL 数据库?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7945235/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How can I create a PostgreSQL database in Java?
提问by user869778
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. I'm working in Java.
如何创建本地和临时 PostgreSQL 数据库来测试我的查询。我在 Java 工作。
回答by bosvos
You CAN create and drop PostgreSQL db's via Java using the Statement object. Kanwarbrar's answer is incorrect and not to the point (but I cannot downvote yet).
您可以使用 Statement 对象通过 Java 创建和删除 PostgreSQL 数据库。Kanwarbrar 的回答是不正确的,不是重点(但我还不能投反对票)。
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
回答by Erwin Brandstetter
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.
回答by Albert
You can see her how to create database Hope this help
你可以看看她如何创建数据库 希望这有助于
- What you do is connect to localhost
- create an sql "create database your_db"
- execute
- 你要做的是连接到本地主机
- 创建一个 sql“创建数据库 your_db”
- 执行
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
回答by Kanwar_Singh
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