请写出数据库查询操作的伪代码,程序不需要通过编译,只要思路正确,关键步骤不丢失就可以了。注意异常的捕获,IO流的关闭。可能用到的类或接口有(Connection,DriverManager, Statement, PreparedStatement, ResultSet, SQLException)。

解决方案 »

  1.   

    写法没什么太多的变化,看你个人的编程风格了,但是Connection, Statement, PreparedStatement, ResultSet  这些对象close的时候,要注意顺序。
      

  2.   

    public class TestJDBC
    {
    String DBDriver = "";
    String ConnStr = "";
    String user = "";
    String pass = "";
    Connection conn = null;
            ResultSet rs = null;
            Statement stm = null;
            //PreparedStatement pstm = null;
            String sqlquery = "";
            //String sqlupdate_P = "update tablename (colm1,colm2,colm3) values(?,?,?)";
            String sqlupdate = "";
    public static void main(String arg[])
    {
    try
    {
    Class.forName(DBDriver);
    }
    catch(ClassNotFoundException ce)
    {
    System.err.println(ce.getMessage());
    }
    try
    {
    conn = DriverManager.getConnection(ConnStr,user,pass);
    }
    catch(SQLException se)
    {
    System.err.println(se.getMessage());
    }
    try
    {
    stm = conn.CreateStatement();
    rs = stm.executQuery(sqlquery);
    while(rs.next())
    {
    //......
    }
    rs.close();
    int i = stm.executeUpdate(sqlupdate);
     
    }
    catch(SQLException se)
    {
    //do other
    }
    stm.close();
    conn.close();
    }
    }
    //慌乱中可能有错误
      

  3.   

    Public class DataBaseConnection 
    {
     public static Connection GetConnection()
    {
      Connection  con = null;
      String  classforname ="oracle.jdbc.driver.OracleDriver";
      String  url="jdbc:odbc:odbcsource";
      String  user = "user";
      String  pwd = " ";
    try{
       //转载驱动,每个数据库驱动程序都必须实现Driver接口;
        class.forName(classforname);
        //调用DriverManager的getConnection()连接到指定的数据库;
        con = DriverManager.getConnection(url,user,pwd);
       }
    catch(excepton e)
    {
      e.printStactTrance();
    }
    return con;
    }
    }  class ExeSql
    {  
       public ExeSql()
      {
          try
            {
            Connection con= null; 
            con = DataBaseConnection.GetConnection()
            Statement stmt = null;
            }
           catch(exception e)
            {
             e.printStackTrace();        }
       }   public  ResultSet ExecuteQuery()
     {
       try{
      stmt  = con.createStatement();
      ResultSet  rst = null;
      String  sql="select * from tablename  where a =b "
      rst  =  stmt.executeQuery("sql");
      reture rst;
      Close();
           }
       catch(exception e)
         {
          e.printStackTrace();
          }
     }
      
     public  int  ExecuteUpdate()
     {
      try
      {
      stmt  = con.createStatement();
      int rst =0;
      String  sql="update tablename  set a = b where a = c "
      rst  =  stmt.executeQuery("sql");
      return rst;
      Close();
     catch(exception e )
      {
        e.printStackTrace();
       }
     }  public  boolean  Execute()
     {
      try{
      stmt  = con.createStatement();
      boolean rst=false;
      String  sql="select * from tablename  where a =b "
      rst  =  stmt.execute("sql");
      return rst;
      Close();
        }
     catch(exception e)
       {
         e.printStackTrace();
       }
     }  public void Close()
       {
        if(con != null || stmt !=null)
        con.close;
        stmt.close;
       }
    }
    上面个有问题 呵呵,随手写的,三个月之前学的java 只后就做.net写这里参考下,有很多都想不起来,也没调试!
    难免有错 呵呵!