oracle数据库连接

首先建一个动态网站工程,在webContent下建一个lib文件夹,放入Oracle数据库连接的jar包

再在src下建一个properties文件命名为oracle.properties

下面是建的数据库连接



import java.io.IOException;
import java.io.InputStream;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.Properties;


public final class DbConnection {
private static Properties ps=null;
private DbConnection(){}
private static void loadProperties() throws ClassNotFoundException{
if(ps==null){
ps=new Properties();
}
if(ps!=null&&ps.size()<4){
InputStream is=DbConnection.class.getResourceAsStream("oracle.properties");
try {
ps.load(is);
} catch (IOException e) {
// TODO Auto-generated catch block
System.out.println("资源加载失败");
}
}
Class.forName(ps.getProperty("classname"));
}
private static Connection getCon() throws ClassNotFoundException, SQLException{
loadProperties();
String url=ps.getProperty("url");
String  user=ps.getProperty("username");
String password=ps.getProperty("password");
return DriverManager.getConnection(url, user, password);
}
private static void closeCon(Connection con) throws SQLException{
if(con!=null&&!con.isClosed()){
try {
con.close();
}finally{}
}
}

public static void main(String[] args) throws ClassNotFoundException, SQLException {
// TODO Auto-generated method stub
System.out.println(DbConnection.getCon());
}


}


properties文件

classname=oracle.jdbc.driver.OracleDriver
url=jdbc:oracle:thin:@localhost:1521:orcl
username=admin根据数据库安装时设定的账号和密码
password=admin



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值