public class GetConn {
public Connection getConnection() {
Connection conn = null;
Statement stat = null;
Result result = null;
try {
Class.forName("oracle.jdbc.driver.OracleDriver"); //加载数据库驱动,MySQL的是com.mysql.jdbc.Driver
System.out.println("数据库驱动加载成功!"); //输出的信息
String url = "jdbc:oracle:thin:@localhost:1521:orcl"; //获取连接URL,mysql的是jdbc:mysql://localhsot:8080/数据库名
String user = "yzt"; //连接用户名
String password = "yzt"; //连接密码
Connection con = DriverManager.getConnection(url, user, password); //获取数据库连接,MySQL的一样
} catch (Exception e) {
e.printStackTrace();
} finally{
if(result!= null){
result.close();
}
if(stat!= null){
stat.close();
}
if(conn!= null){
conn.close();
}
}
return conn; //返回Connection实例
}
public static void main(String[] args) {
GetConn getConn = new GetConn();
getConn.getConnection();
}
}