您的位置:首页 > 博客中心 > 数据库 >

常用数据库的连接方式

时间:2022-03-15 18:25

--------------------------Mysql数据库----------------------------
url=jdbc:mysql://127.0.0.1:3306/数据库名称
user=root//用户名
password=123456 //密码                                                                                                                                      

---------------------------Oracle数据库---------------------------                                                                                                                                                                                                                                                                                    String URL = "jdbc:oracle:thin:@localhost:1521:ORCL";
String USERNAME = "system";//用户名
String PWD = "RZKruizhukai123";//密码

-------------------------QLServer2000数据库 --------------------

String dbUrl = "jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=数据库名字";
String user="sa"; //用户名
String password="";//密码
 
---------------------------------可以把上面的装在配置文件中  方便读取-------------------

public Connection getConnection() throws Exception{
//1.创建配置文件并得到对象输入流
InputStream is = this.getClass().getClassLoader().getResourceAsStream("db.properties");
//2.创建propetities
Properties pro= new Properties();
pro.load(is);
//3. 通过key-value 的方式得到相对应的值
String driver = pro.getProperty("driver");
String url = pro.getProperty("url");
String user = pro.getProperty("user");
String password = jdbc.getProperty("password");
//4.加载运行时类对象
Class.forName(driver);
//5通过DriverManager得到连接
Connection connection = DriverManager.getConnection(url,user,password);
return connection;
}

 
 
 

本类排行

今日推荐

热门手游