A java code

With the help of LiJun I got a piece of JAVA code. With this code, I can do below things like connect to oracle database with jdbc driver and run sql.

 

Below are the Java code. 

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
/**
*
* @author xx
*/

public class TestJDBC {
  static Connection conn= null;
  static String url="jdbc:oracle:thin:@pnd:1521:pnd";
  static String username="pnadm";
  static String password="password";

  public TestJDBC()
{
  try{
  String driverclass="oracle.jdbc.driver.OracleDriver";
  Class.forName(driverclass).newInstance();
  conn=DriverManager.getConnection(url,username,password);
   }catch(Exception e){
                        e.printStackTrace();
                        }
  }
  public static void getDate(){
  String sql="select object_name from all_objects where rownum<2";
  try{
        Statement stmt=conn.createStatement();
        ResultSet rs1=stmt.executeQuery(sql);

        while(rs1.next()){
                        System.out.print(rs1.getString("object_name")+" ");
                        }
                }catch(Exception e){
                        e.printStackTrace();
                        }

}

public static void main(String[] args) {
   TestJDBC t = new TestJDBC();
    for (int i=0;i <30 ; i++)
                t.getDate();

     try {
            Thread.sleep(30000);
        } catch (InterruptedException e) {
        }

    for (int i=0;i <280 ; i++)
                t.getDate();

     try {
            Thread.sleep(300000);
        } catch (InterruptedException e) {
        }


 }
}

 

 

There is something important I need to record down.

1. You need to specify classpath environment variable for example

export CLASS_PATH=/opt/oracle/product/10.2/jdbc/lib/ojdbc14.jar

2. You can get the jdbc driver from oracle directory

 

posted on 2014-05-12 15:07  kramer  阅读(182)  评论(0编辑  收藏  举报

导航