views:

20

answers:

1

I want to show my Oracle DB tables on my application. I create a new database connection DBConnection1. But I don't bind DBConnection1 in my class. How to do it?

A: 

OK. I solve my question.

            DriverManager.registerDriver(new oracle.jdbc.driver.OracleDriver());
            Connection connection = DriverManager.getConnection(connStr,"scott","tiger"); 

            Connector conn = new Connector(connStr);
            Statement stmt = conn.getConnection().createStatement();                       

            ResultSet rset = stmt.executeQuery(sql); 
            ResultSetMetaData metaData = rset.getMetaData();

            int rowCount = metaData.getColumnCount();                 

            for(i=1;i<=rowCount;i++)
                headers.add(metaData.getColumnLabel(i).toString()); 


            while(rset.next()){          
                Vector tmp = new Vector();
                for(i=1;i<=rowCount;i++) {
                    tmp.add(rset.getString(i));              
                }
                lists.add(tmp);
                index++;                              

Here is the my connector class.

package client;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;


public class Connector 
{
  private String connection_string;
  private Statement stmt;
  private Connection connection;

  public Connector(String conn)
  {
      //String connection_string = "jdbc:oracle:thin:@<host>:<port>:<db name>";
      try 
      {
        connection_string = conn;

        DriverManager.registerDriver(new oracle.jdbc.driver.OracleDriver());  

        connection = DriverManager.getConnection(conn,"scott","tiger");                 

      } catch(Exception f) 
      {
        f.printStackTrace();
      }

  }

  public ResultSet execute(String sql) 
  {
     try 
     {
       stmt = connection.createStatement();
       return stmt.executeQuery(sql);
     } catch (Exception f) 
     {
       f.printStackTrace();
     }    
     return null;
  }


  public void setConnection_string(String connection_string)
  {
    this.connection_string = connection_string;
  }


  public String getConnection_string()
  {
    return connection_string;
  }


  public void setStmt(Statement stmt)
  {
    this.stmt = stmt;
  }


  public Statement getStmt()
  {
    return stmt;
  }


  public void setConnection(Connection connection)
  {
    this.connection = connection;
  }


  public Connection getConnection()
  {
    return connection;
  }
}
Judas Imam