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());
       }
        }
 
}

更多相关文章

  1. java 在数据库中添加新信息
  2. JAVA 关于图片上传的代码
  3. 线程“main”中的异常java.lang.RuntimeException:无法编译的源代
  4. JavaWeb-1-IOS或Android客户端上传图片到Java服务端存到数据库,再
  5. Java操作Sqlite数据库
  6. java 和 C 代码运行效率的比较(整理)
  7. Java:IntelliJ想法生成的代码错误地为所有Class名称添加了其包名
  8. Java连接Oracle数据库简单实例
  9. 在servlet中的init方法得到了对数据库操作的值,怎么传给前端

随机推荐

  1. php简单图形面积计算器的实现
  2. 直击PHP的异常和错误处理
  3. php如何实现图片上传的封装
  4. PHP中如何形成static::与new static()的
  5. 【设计模式】PHP单例模式的应用场景和实
  6. PHP中$是什么意思?
  7. PHP 10 个常见安全问题(实例讲解)
  8. 一个简单QQ群聊案例代码解析(PHP实现)
  9. php面向对象简单概括
  10. 一个简单的部门案例详解(PHP代码实例)