static final String JDBC_DRIVER = “org.postgresql.Driver”;
static final String DB_URL = “jdbc:postgresql://你的虚拟机IP地址:你的数据库占用端口号/要连接的数据库”;
// 数据库的用户名与密码,需要根据自己的设置
static final String USER = “root”;
static final String PASS = “123456”;
public static void main(String[] args) {
Connection conn = null;
Statement stmt = null;
try{
// 注册 JDBC 驱动
Class.forName(JDBC_DRIVER);
// 打开链接
System.out.println("connecting...");
conn = DriverManager.getConnection(DB_URL,USER,PASS);
//实例化对象
stmt = conn.createStatement();
// 执行查询
String sql;
sql = "SELECT id, name, url FROM websites";
ResultSet rs = stmt.executeQuery(sql);
// 创建表
sql = "CREATE TABLE COMPANY1 " +
"(ID INT PRIMARY KEY NOT NULL," +
" NAME TEXT NOT NULL, " +
" AGE INT NOT NULL, " +
" ADDRESS CHAR(50), " +
" SALARY REAL)";
rs = stmt.executeQuery(sql);
//关闭脚本文件
stmt.close();
// 结束连接
c.close();
} catch ( Exception e ) {
System.err.println( e.getClass().getName()+": "+ e.getMessage() );
System.exit(0);
}
System.out.println("Table created successfully");
}
}