package com.tg.untils;
import java.sql.DriverManager;
import java.sql.SQLException;
import com.sun.corba.se.pept.transport.Connection;
public class Util {
java.sql.Connection conn=null;
String url = "jdbc:oracle:thin:@192.168.10.229:1521:orcl"; /*数据库URL*/
String user = "aaa"; /*用户名*/
String password = "xxxxx"; /*密码*/
public void testOracle(){
try {
Class.forName("oracle.jdbc.driver.OracleDriver"); /*加载驱动*/
System.out.println("加载驱动成功...");
} catch (ClassNotFoundException e1) {
// TODO Auto-generated catch block
System.out.println("加载驱动失败...");
e1.printStackTrace();
}
try {
conn=DriverManager.getConnection(url,user,password); /*获取连接*/
System.out.println("开始连接数据库...");
System.out.println("获取连接成功...");
} catch (SQLException e) {
// TODO Auto-generated catch block
System.out.println("获取连接失败!!!");
e.printStackTrace();
}
}
/*关闭数据库连接*/
if(conn!=null){
try {
conn.close();
System.out.println("数据库连接已关闭");
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
System.out.println("数据库连接关闭失败...");
}
}
}
--------------------------------------------------------------------------------------------------------
运行代码 控制台输出截图如下:
/*一定不要忘了添加ojdbc这个 jar包*/
参考w3cschool手册 : http://www.w3cschool.cn/jdbc/jre31myf.html