服务热线:13616026886

技术文档 欢迎使用技术文档,我们为你提供从新手到专业开发者的所有资源,你也可以通过它日益精进

位置:首页 > 技术文档 > JAVA > 新手入门 > 基础入门 > 查看文档

java连接oracle的代码

最新做了一个连接oracle的代码,要配好环境变量:

classpath=.;e:/j2sdk14/lib/tools.jar;e:/oracle/ora90/jdbc/lib/classes12.zip

测试的电脑已经装好oracle客户端,而且用sqlplus可以连接上。
/*
* this sample shows how to list all the names from the emp table
*
* it uses the jdbc thin driver.  see the same program in the
* oci8 samples directory to see how to use the other drivers.
*/

// you need to import the java.sql package to use jdbc
import java.sql.*;

class test
{
  public static void main (string args [])
       throws sqlexception
  {
    // load the oracle jdbc driver
    drivermanager.registerdriver(new oracle.jdbc.oracledriver());
/* try{
  class.forname("oracle.jdbc.driver.oracledriver");
}catch(exception e){
  system.out.println("no driver!");
}
*/
    // connect to the database
    // you must put a database name after the @ sign in the connection url.
    // you can use either the fully specified sql*net syntax or a short cut
    // syntax as <host>:<port>:<sid>.  the example uses the short cut syntax.
    string url = "jdbc:oracle:thin:@172.28.31.85:1521:yikatong";
    string username = "scott";
    string password = "tiger";

    if (args.length > 0) url = args[0];
    if (args.length > 1) username = args[1];
    if (args.length > 2) password = args[2];
system.out.println(url);
system.out.println(username);
system.out.println(password);
    connection conn =
      drivermanager.getconnection (url, username, password);

    // create a statement
    statement stmt = conn.createstatement ();

   // select the ename column from the emp table
    resultset rset = stmt.executequery ("select * from test");

    // iterate through the result and print the employee names
    while (rset.next ())
      system.out.println (rset.getstring (1));
  }
}

扫描关注微信公众号