package com.xxxx.lunwen.test;
import java.sql.*;
public class DBUtil {
static
{
try
{
// 加载Oracle驱动程序
Class.forName("oracle.jdbc.driver.OracleDriver").newInstance();
System.out.println("oracle驱动程序加载中!");
}
catch(InstantiationException e1)
{
System.out.println("实例异常");
}
catch(IllegalAccessException e2)
{
System.out.println("访问异常");
}
catch(ClassNotFoundException e3)
{
System.out.println("MySQL驱动类找不到");
}
}
/***
* 返回一个数据库连接
*/
public static Connection getConnection()
{
Connection connection = null;// 创建一个数据库连接
try
{
System.out.println("开始尝试连接数据库!");
String url = "jdbc:oracle:thin:@127.0.0.1:1521:xkpt";//Oracle的默认数据库名
String user = "xkpt";// 系统默认的用户名
String password = "123456";// 安装时设置的密码
connection = DriverManager.getConnection(url, user, password);// 获取连接
System.out.println(url);
System.out.println("用户名:"+user+" "+"密码:******");
System.out.println("数据库连接成功!");
return connection;
}
catch (Exception e)
{
e.printStackTrace();
return null;
}
}
}