JAVA连接Oracle的代码

2016-02-19 15:40 5 1 收藏

今天图老师小编给大家介绍下JAVA连接Oracle的代码,平时喜欢JAVA连接Oracle的代码的朋友赶紧收藏起来吧!记得点赞哦~

【 tulaoshi.com - 编程语言 】

最新做了一个连接Oracle的代码,要配好环境变量: ClassPath=.;E:j2sdk14libools.jar;E:oracleora90jdbclibclasses12.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));
  }
}

(本文来源于图老师网站,更多请访问http://www.tulaoshi.com/bianchengyuyan/)

来源:http://www.tulaoshi.com/n/20160219/1610158.html

延伸阅读
数据库操作现在是项目开发的根本,学习Java首先应该学会怎么样连接数据库,用Java连接数据库可不像用Delphi这类工具那样设几个属性就OK,说简单也简单,说复杂,其实也挺复杂的,而且很麻烦,如果是初学,根本不能保证第一次就连接成功,下面以SQL Server 2000为例,说说Java连接数据库的基本方法,也记录一下心得。 1、下载SQL Server 2000 ...
标签: ASP
  操作系统 Windows 2000 sp4 装有IIS5.0以及Oracle 8i 症状:用oracle自带的工具能连上数据库,一切操作正常。 使用ASP的ADODB以及MSADORA来接连oracle数据库,提示TNS错误或TNS适配器不能工作。 但是使用VB的程就能工作。 直到后来怀疑是IIS用户的权限不够,检查时发现用户名与机器名不匹配,原来这台机器改过名字。 于是改成与...
/* drop table varray_table; drop type num_varray; CREATE TYPE num_varray AS VARRAY(10) OF VARCHAR2(12) / CREATE TABLE varray_table (col1 num_varray); INSERT INTO varray_table VALUES (num_varray('你好', 'abc')); select * from varray_table; */ import Java.sql.*; import java.math.*; import Oracle.jdbc.dri...
自己实现了一遍: 代码如下: public class A implements Cloneable { public String str[]; A() { str = new String[2]; } public Object clone() { A o = null; try { o = (A) super.clone(); } catch (CloneNotSupportedException e) { e.printStackTrace(); } o.str = new String[2]; return o; } } void run() throws Exception { A ...
代码如下: package com.qhdstar.java.pdf; import java.awt.Color; import java.io.FileOutputStream; import com.lowagie.text.Chapter; import com.lowagie.text.Document; import com.lowagie.text.Font; import com.lowagie.text.FontFactory; import com.lowagie.text.PageSize; import com.lowagie.text.Paragraph; import com.lowag...

经验教程

401

收藏

57
微博分享 QQ分享 QQ空间 手机页面 收藏网站 回到头部