Home > Database > Mysql Tutorial > body text

Java语言测试连接Oracle数据库

WBOY
Release: 2016-06-07 15:21:47
Original
1373 people have browsed it

Java语言测试连接Oracle数据库 package com.linuxidc.jdbc; import java.sql.Connection;import java.sql.DriverManager;/** * 测

Java语言测试连接Oracle数据库

package com.linuxidc.jdbc;

import java.sql.Connection;
import java.sql.DriverManager;
/**
 * 测试连接oracle数据库
 * @author wpc
 *
 */

public class JdbcSuccess {
 //驱动,URL,用户名,密码
 private static final String driver = "oracle.jdbc.driver.OracleDriver";
 private static final String url = "jdbc:oracle:thin:@localhost:1521:orcl";
 private static final String username = "scott";
 private static final String password = "929118";

 public static Connection getConnection() {
  Connection connection = null;
  try {
   // 加 载驱动
   Class.forName(driver);
   // 通过驱动管理类获取数据库连接
   connection = DriverManager.getConnection(url, username, password);
   // 关闭连接
   connection.close();
  } catch (ClassNotFoundException e) {
   e.printStackTrace();
  } catch (Exception e) {
   e.printStackTrace();
  }
  return connection;
 }

 // 测试oracle连接是否成功
 public static void main(String[] args) {
  Connection connection = JdbcSuccess.getConnection();
  if (connection != null) {
   System.out.println("连接oracle数据库成功:" + connection);
  } else {
   System.out.println("连接oracle数据库失败!!!");
  }

 }
}

本文永久更新链接地址

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template