postgresql在Win10安装完全教程以及作为nosql开发的示例java程序入参用json含新增查询更新等
PostgreSQL安装教程:
-
下载PostgreSQL安装包:在PostgreSQL官网(https://www.postgresql.org/download/windows/)下载适合你的操作系统的安装包。
-
运行安装程序:双击下载的安装包,按照安装程序的指示进行安装。
-
设置数据库超级用户密码:在安装过程中,会提示你设置数据库超级用户(postgres)的密码。请记住这个密码,它将在后续的操作中用到。
-
完成安装:按照安装程序的指示完成安装过程。
-
启动PostgreSQL服务:在开始菜单中找到“PGAdmin 4”应用程序,双击运行。在PGAdmin 4管理界面中,点击左侧的“Servers”选项卡,然后右键点击“PostgreSQL”图标,选择“Connect Server”选项。在弹出的对话框中输入超级用户密码,点击“OK”按钮。成功连接到服务器后,在左侧的“Servers”选项卡下会显示一个“PostgreSQL 13”服务器。
-
创建数据库:在PGAdmin 4管理界面中,展开“PostgreSQL 13”服务器,右键点击“Databases”,选择“Create”->“Database”选项。在弹出的对话框中输入数据库名称(例如:mydb),点击“Save”按钮。
-
创建表格:在PGAdmin 4管理界面中,展开“PostgreSQL 13”服务器,展开“Databases”,展开你创建的数据库(例如:mydb),右键点击“Tables”,选择“Create”->“Table”选项。在弹出的对话框中输入表格名称(例如:users),定义表格的字段和数据类型,点击“Save”按钮。
示例Java程序:
以下是一个使用PostgreSQL作为NoSQL数据库的示例Java程序,程序使用JSON作为入参,并包含新增、查询和更新等操作:
import org.json.JSONObject;
import java.sql.*;
public class PostgreSQLExample {
public static void main(String[] args) {
String url = "jdbc:postgresql://localhost/mydb";
String user = "postgres";
String password = "your_password";
try {
// 连接到数据库
Connection connection = DriverManager.getConnection(url, user, password);
// 新增数据
JSONObject newUser = new JSONObject();
newUser.put("name", "John");
newUser.put("age", 25);
addUser(connection, newUser);
// 查询数据
int userId = 1;
JSONObject user = getUser(connection, userId);
System.out.println("User: " + user.toString());
// 更新数据
JSONObject updatedUser = new JSONObject();
updatedUser.put("id", 1);
updatedUser.put("age", 26);
updateUser(connection, updatedUser);
// 再次查询数据
user = getUser(connection, userId);
System.out.println("Updated User: " + user.toString());
// 关闭数据库连接
connection.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
public static void addUser(Connection connection, JSONObject user) throws SQLException {
String sql = "INSERT INTO users (data) VALUES (?::jsonb)";
PreparedStatement statement = connection.prepareStatement(sql);
statement.setString(1, user.toString());
statement.executeUpdate();
statement.close();
}
public static JSONObject getUser(Connection connection, int userId) throws SQLException {
String sql = "SELECT data FROM users WHERE id = ?";
PreparedStatement statement = connection.prepareStatement(sql);
statement.setInt(1, userId);
ResultSet resultSet = statement.executeQuery();
JSONObject user = null;
if (resultSet.next()) {
user = new JSONObject(resultSet.getString("data"));
}
resultSet.close();
statement.close();
return user;
}
public static void updateUser(Connection connection, JSONObject user) throws SQLException {
String sql = "UPDATE users SET data = ?::jsonb WHERE id = ?";
PreparedStatement statement = connection.prepareStatement(sql);
statement.setString(1, user.toString());
statement.setInt(2, user.getInt("id"));
statement.executeUpdate();
statement.close();
}
}
在上述示例程序中,需要将url、user和password变量的值替换为你的数据库连接信息。
请注意,示例程序中的表格结构和字段名称需要与你所创建的表格相匹配,否则需要进行调整。另外,示例程序中使用了jsonb数据类型来存储JSON数据,如果你的数据库中未安装jsonb扩展,请先安装该扩展
原文地址: https://www.cveoy.top/t/topic/hIEB 著作权归作者所有。请勿转载和采集!