Java连接opengauss数据库实现增删改查

Java连接opengauss数据库实现增删改查,第1张

Java连接opengauss数据库实现增删改查 前期准备:

 


1.VMware workstation中配置了OpenEuler系统,并且在该系统上成功部署了OpenGauss2.0.0数据库

2.完成远程连接部署(通过Data Studio工具能成功连接到本地opengauss数据库)

3.配置java环境为jdk1.8版本,具体请参考jdk配置。

4.下载opengauss2.0.0-openEuler系统x86_64版本的JDBC驱动安装包,下载完成后解压,找到postgresql.jar文件,将该文件放置到java项目文件夹下。

https://opengauss.obs.cn-south-1.myhuaweicloud.com/2.0.1/x86_openEuler/openGauss-2.0.0-JDBC.tar.gz

连接过程
package HospitalDatabase;
 
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.Statement;
import java.sql.*;
 
public class PostgreSqlJdbcConnAddDatas {
	public static void main(String args[]) {
		Connection c = null;
		Statement stmt = null;
		try {
			Class.forName("org.postgresql.Driver");
			
			c = DriverManager.getConnection("jdbc:postgresql://192.168.159.129:26000/postgres","jack","gauss@111");
			c.setAutoCommit(false);
 
			System.out.println("连接数据库成功!");
			stmt = c.createStatement();
 
 
			
//			String sql = "INSERT INTO Course (Cno,Cname,Cpno,Ccredit) "
//					+ "VALUES (1,'数据库',5,4);";
//			stmt.executeUpdate(sql);
//			System.out.println("新增数据成功!");
			
			
			
//			ResultSet rs = stmt.executeQuery("select * from sc");
//			while(rs.next()){
//				int sno = rs.getInt("Sno");
//				int cno = rs.getInt("cno");
//				int grade = rs.getInt("Grade");
//				System.out.println(cno + "," + cno + "," + grade);
//			}
//			System.out.println("查询数据成功!");
// 
			
			
			String sql = "UPDATe sc set grade = 250 where cno=1 ";
			stmt.executeUpdate(sql);
			c.commit();
 
			ResultSet rs1 = stmt.executeQuery("select * from sc order by cno");
			while(rs1.next()){
				int sno = rs1.getInt("Sno");
				int cno = rs1.getInt("Cno");
				int grade = rs1.getInt("Grade");
				System.out.println(sno + "," + cno + "," + grade);
			}
			System.out.println("更新数据成功!");


			
//			String sql = "Delete from sc where Cno=2 ";
//			stmt.executeUpdate(sql);
//			c.commit();
// 
//			ResultSet rs1 = stmt.executeQuery("select * from sc order by cno");
//			while(rs1.next()){
//				int sno = rs1.getInt("Sno");
//				int cno = rs1.getInt("Cno");
//				int grade = rs1.getInt("Grade");
//				System.out.println(sno + "," + cno + "," + grade);
//			}
//			System.out.println("删除数据成功!");

 
			stmt.close();
			c.commit();
			c.close();
 
		} catch (Exception e) {
			e.printStackTrace();
			System.err.println(e.getClass().getName() + ": " + e.getMessage());
			System.exit(0);
		}
//		System.out.println("新增数据成功!");
	}
}
注意问题

1.这里可以 *** 作的表必须是能用命令d tablename查看到表结构的表。

 

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/zaji/5118189.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-11-17
下一篇 2022-11-17

发表评论

登录后才能评论

评论列表(0条)

保存