抽取数据库配置 到properties文件 配置代码解耦合
This commit is contained in:
parent
453e85d43e
commit
eab7564a30
@ -4,29 +4,55 @@ import com.alibaba.druid.pool.DruidDataSource;
|
||||
import com.alibaba.druid.pool.DruidPooledConnection;
|
||||
import com.mchange.v2.c3p0.ComboPooledDataSource;
|
||||
import com.sec.dao.UserDao;
|
||||
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
|
||||
import org.springframework.context.support.ClassPathXmlApplicationContext;
|
||||
import org.springframework.context.support.FileSystemXmlApplicationContext;
|
||||
|
||||
import java.beans.PropertyVetoException;
|
||||
import java.sql.*;
|
||||
import java.util.ResourceBundle;
|
||||
|
||||
//TIP To <b>Run</b> code, press <shortcut actionId="Run"/> or
|
||||
// click the <icon src="AllIcons.Actions.Execute"/> icon in the gutter.
|
||||
public class Main {
|
||||
static String driver = "com.mysql.jdbc.Driver";
|
||||
static String url = "jdbc:mysql://47.103.155.22:3306/learn_spring";
|
||||
static String user = "root";
|
||||
static String password = "mysql_wafeYX";
|
||||
public static void main(String[] args) {
|
||||
static String driver = "";
|
||||
static String url = "";
|
||||
static String user = "";
|
||||
static String password = "";
|
||||
|
||||
// ApplicationContext application=new ClassPathXmlApplicationContext("application.xml");
|
||||
public static void main(String[] args) {
|
||||
Main.getSpring();
|
||||
Main.getSetting();
|
||||
Main.getDruid();
|
||||
Main.getC3p0();
|
||||
}
|
||||
|
||||
//spring api
|
||||
//1.ClassPathXmlApplicationContext 读取根目录下的配置文件
|
||||
//2.FileSystemXmlApplicationContext 读取xml下的配置文件
|
||||
//3.AnnotationConfigApplicationContext 读取注解文件
|
||||
public static void getSpring(){
|
||||
// ApplicationContext application=new ClassPathXmlApplicationContext("application.xml");
|
||||
ClassPathXmlApplicationContext application=new ClassPathXmlApplicationContext("application.xml");
|
||||
UserDao userDao1 = (UserDao)application.getBean("UserDao");
|
||||
// UserDao userDao2 = (UserDao)application.getBean("UserDao");
|
||||
System.out.println(userDao1);
|
||||
application.close();
|
||||
// System.out.println(userDao2);
|
||||
|
||||
// userDao.getData();
|
||||
}
|
||||
|
||||
//读取properties文件 对数据库连接信息进行设置
|
||||
//目的是为了将配置信息与代码进行解耦
|
||||
public static void getSetting(){
|
||||
ResourceBundle rb = ResourceBundle.getBundle("jdbc");
|
||||
driver=rb.getString("jdbc.driver");
|
||||
url=rb.getString("jdbc.url");
|
||||
user=rb.getString("jdbc.user");
|
||||
password=rb.getString("jdbc.password");
|
||||
}
|
||||
|
||||
public static void getDruid(){
|
||||
DruidDataSource druidDataSource=new DruidDataSource();
|
||||
druidDataSource.setDriverClassName(driver);
|
||||
druidDataSource.setUrl(url);
|
||||
@ -39,7 +65,6 @@ public class Main {
|
||||
ResultSet resultSet = preparedStatement.executeQuery();
|
||||
while (resultSet.next()) {
|
||||
System.out.println(resultSet.getString("user_name")+" "+resultSet.getString("account_name"));
|
||||
// System.out.println(resultSet.getString("account_name"));
|
||||
}
|
||||
resultSet.close();
|
||||
preparedStatement.close();
|
||||
@ -47,6 +72,9 @@ public class Main {
|
||||
} catch (SQLException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
}
|
||||
|
||||
public static void getC3p0(){
|
||||
ComboPooledDataSource comboPooledDataSource=new ComboPooledDataSource();
|
||||
try {
|
||||
comboPooledDataSource.setDriverClass(driver);
|
||||
@ -65,8 +93,5 @@ public class Main {
|
||||
} catch (PropertyVetoException | SQLException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
|
||||
|
||||
|
||||
}
|
||||
}
|
4
springLearn202503/src/main/resources/jdbc.properties
Normal file
4
springLearn202503/src/main/resources/jdbc.properties
Normal file
@ -0,0 +1,4 @@
|
||||
jdbc.driver=com.mysql.jdbc.Driver
|
||||
jdbc.url=jdbc:mysql://47.103.155.22:3306/learn_spring
|
||||
jdbc.user=root
|
||||
jdbc.password=mysql_wafeYX
|
Binary file not shown.
Loading…
x
Reference in New Issue
Block a user