Database 如何建立本地主机和我的数据库的连接
我正在使用NetBeans来创建一个web应用程序,并将pgadmin4用于我的数据库。问题是当我建立连接池时。Database 如何建立本地主机和我的数据库的连接,database,postgresql,netbeans,web-applications,Database,Postgresql,Netbeans,Web Applications,我正在使用NetBeans来创建一个web应用程序,并将pgadmin4用于我的数据库。问题是当我建立连接池时。 这是我的数据库,我想问题是因为我想要这样的东西,但我有这个。我尝试了很多解决方案,但都不起作用,我不知道我还需要做什么。我尝试过的一件事是这样做,但不幸的是没有成功。下面的代码片段展示了如何使用jdbc驱动程序为PostgreSQL建立连接。确保将jdbc驱动程序添加到库中 public static void main (String[] args) { try {
这是我的数据库,我想问题是因为我想要这样的东西,但我有这个。我尝试了很多解决方案,但都不起作用,我不知道我还需要做什么。我尝试过的一件事是这样做,但不幸的是没有成功。下面的代码片段展示了如何使用
jdbc
驱动程序为PostgreSQL
建立连接。确保将jdbc
驱动程序添加到库中
public static void main (String[] args) {
try {
Class.forName("com.mysql.jdbc.Driver");
String url = "jdbc:mysql://localhost:3306/yourdatabasename";
Connection conn = DriverManager.getConnection(url, user,password);
conn.close();
} catch (Exception e) {
System.err.println("Got an exception! ");
System.err.println(e.getMessage());
}
}
public Connection DBConnect() {
try {
String host = "localhost";//host
String port = "5432";//db port
String db = "exp";//database name
String user = "root";//database username
String pass = "1234";//password
//connection url
String url = "jdbc:postgresql://" + host + ":" + port + "/" + db;
//initialize jdbc driver for postger sql
Class.forName("org.postgresql.Driver");
Connection conn = DriverManager.getConnection(url, user, pass);
//return connection
return conn;
} catch (Exception e) {
System.out.println("Error : " + e.getMessage());
return null;
}
}
参考:这是用于mysql连接的