欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

jdbc连接oracle数据库功能示例

程序员文章站 2024-03-08 23:46:10
本文实例讲述了jdbc链接oracle数据库功能。分享给大家供大家参考,具体如下: package com.sefvang.jdbc; import java....

本文实例讲述了jdbc链接oracle数据库功能。分享给大家供大家参考,具体如下:

package com.sefvang.jdbc;
import java.sql.connection;
import java.sql.drivermanager;
import java.sql.preparedstatement;
import java.sql.resultset;
import java.sql.sqlexception;
public class db_conn {
 private static final string url = "jdbc:oracle:thin:@localhost:1521:orcl";
 private static final string name = "system";
 private static final string passwd = "sefvang";
 private static final string db_driver = "oracle.jdbc.oracledriver";
 public connection getconnection() {
  connection conn = null;
  try {
   class.forname(db_driver);
   conn = drivermanager.getconnection(url, name, passwd);
   if (conn != null) {
    system.out.println("success!!!");
   }
  } catch (classnotfoundexception e) {
   // todo auto-generated catch block
   e.printstacktrace();
  } catch (sqlexception e) {
   // todo auto-generated catch block
   e.printstacktrace();
  }
  return conn;
 }
 public void close(connection conn) {
  try {
   if (conn != null) {
    if (!conn.isclosed()) {
     conn.close();
    }
   }
  } catch (sqlexception e) {
   // todo auto-generated catch block
   e.printstacktrace();
  }
 }
 public static void main(string[] args) {
  db_conn conn=new db_conn();
  connection con = conn.getconnection();
  try {
   preparedstatement papt = con.preparestatement("select tname from text where tid=1");
   resultset rs = papt.executequery();
   if(rs.next())
   system.out.println(rs.getstring(1));
  } catch (sqlexception e) {
   // todo auto-generated catch block
   e.printstacktrace();
  }
  conn.close(con);
 }
}

希望本文所述对大家java程序设计有所帮助。