Java向PostgreSQL发送prepared statement 与 libpq 向PostgreSQL发送prepared statement之比较:
16lz
2021-01-22
Java 代码,在数据库端,并没有当成 prepared statetment 被处理。
C代码通过libpq 访问数据库端,被当成了 prepared statement 处理。也许是因PostgreSQL对JDBC的支持毕竟是后期出现的:
下面看代码和运行结果:
Java 代码:
import java.sql.*; public class Test01 { public static void main(String argsv[]){ try { Class.forName("org.postgresql.Driver").newInstance(); String url = "jdbc:postgresql://localhost:5432/postgres" ; Connection con = DriverManager.getConnection(url,"postgres","postgres" ); ///Phase 1:-------------Select data from table----------------------- System.out.println("Phase 1------------------------start"); String strsql = " select * from customers where cust_id = ?"; PreparedStatement pst=con.prepareStatement(strsql); pst.setInt(1,3); //find the customer with cust_id of 3. ResultSet rs = pst.executeQuery(); while (rs.next()) { System.out.print("cust_id:"+rs.getInt( "cust_id")); System.out.println("...cust_name:"+rs.getString( "cust_name" )); } System.out.println("Phase 1------------------------end\n"); ///Phase 2:-------------Use connection again,to select data from data dictionary----------------------- System.out.println("Phase 2------------------------start"); strsql = "select * from pg_prepared_statements"; pst=con.prepareStatement(strsql); rs = pst.executeQuery(); while (rs.next()) { System.out.println("statement:"+rs.getString( "statement")); } System.out.println("Phase 2------------------------end\n"); ///Phase 3:-------------Use connection again,to select data from table----------------------- System.out.println("Phase 3------------------------start"); strsql = "select * from customers"; pst=con.prepareStatement(strsql); rs = pst.executeQuery(); while (rs.next()) { System.out.print("cust_id:"+rs.getInt( "cust_id")); System.out.println("...cust_name:"+rs.getString( "cust_name" )); } System.out.println("Phase 3------------------------end\n"); rs.close(); pst.close(); con.close(); } catch (Exception ee) { System.out.print(ee.getMessage()); } } }
更多相关文章
- java 在数据库中添加新信息
- JAVA 关于图片上传的代码
- 线程“main”中的异常java.lang.RuntimeException:无法编译的源代
- JavaWeb-1-IOS或Android客户端上传图片到Java服务端存到数据库,再
- Java操作Sqlite数据库
- java 和 C 代码运行效率的比较(整理)
- Java:IntelliJ想法生成的代码错误地为所有Class名称添加了其包名
- Java连接Oracle数据库简单实例
- 在servlet中的init方法得到了对数据库操作的值,怎么传给前端