jdbc获取数据库连接的五种方式

方式一:

 1     public void testConnection1() throws SQLException {
 2         //获取derive的实现类对象
 3         Driver driver = new com.mysql.jdbc.Driver();
 4 
 5         //jdbc:mysql:协议
 6         //localhost:ip地址
 7         //3306:默认mysql端口号
 8         //test:test数据库
 9         String url = "jdbc:mysql://localhost:3306/test";
10         //将用户名和密码封装在Properties中
11         Properties info = new Properties();
12         info.setProperty("user","root");
13         info.setProperty("password","crx666");
14 
15 
16         Connection conn = driver.connect(url,info);
17         System.out.println(conn);
18     }

方式二:对方式的迭代,在如下程序中不出现第三方的api,使得程序具有更好的可移植性

 1 public void testConnection2() throws Exception{
 2         //1、获取driver的实现类对象,使用反射
 3         Class clazz = Class.forName("com.mysql.jdbc.Driver");
 4         Driver driver = (Driver) clazz.newInstance();
 5 
 6         //2、提供要连接的数据库
 7         String url = "jdbc:mysql://localhost:3306/test";
 8 
 9         //3、提供连接需要的用户名和密码
10         Properties info = new Properties();
11         info.setProperty("user","root");
12         info.setProperty("password","crx666");
13 
14         //4、获取连接
15         Connection conn = driver.connect(url,info);
16         System.out.println(conn);
17     }

方式三:使用DriverManager替换Driver

 1   public void testConnection3() throws Exception {
 2         //1、获取Driver实现类对象
 3         Class clazz = Class.forName("com.mysql.jdbc.Driver");
 4         Driver driver = (Driver) clazz.newInstance();
 5 
 6         //2、提供另外三个连接的基本信息
 7         String url = "jdbc:mysql://localhost:3306/test";
 8         String user = "root";
 9         String password = "crx666";
10 
11         //注册驱动
12         DriverManager.registerDriver(driver);
13 
14         //获取连接
15         Connection conn = DriverManager.getConnection(url,user,password);
16         System.out.println(conn);
17     }

方式四:可以只是加载驱动,不用显示的注册驱动过了

 1    public void testConnection4() throws Exception {
 2 
 3         //1、提供另外三个连接的基本信息
 4         String url = "jdbc:mysql://localhost:3306/test";
 5         String user = "root";
 6         String password = "crx666";
 7 
 8         //2、加载Driver
 9         Class.forName("com.mysql.jdbc.Driver");
10         //相较于方式三,可以省略如下的操作
11 //        Driver driver = (Driver) clazz.newInstance();
12 //        //注册驱动
13 //        DriverManager.registerDriver(driver);
14         //为什么可以省略呢?
15         /**
16          * 在mysql的Driver实现类中,声明了如下操作:
17          *     static {
18          *         try {
19          *             java.sql.DriverManager.registerDriver(new Driver());
20          *                } catch (SQLException E) {
21          *             throw new RuntimeException("Can't register driver!");
22          *        }*     }
23          */
24 
25         //获取连接
26         Connection conn = DriverManager.getConnection(url,user,password);
27         System.out.println(conn);
28     }

方式五(final版):将数据库连接需要的四个基本信息声明在配置文件中,通过读取配置文件的方式获取连接

用此方法的好处:

1、实现类数据与代码的分离,实现了解耦;

2、如果需要修改配置文件信息,可以避免程序重新打包。

 1     public void getConnection5() throws Exception{
 2 
 3         //1.读取配置文件中的4个基本信息
 4         InputStream is = ConnectionTest.class.getClassLoader().getResourceAsStream("jdbc.properties");
 5 
 6         Properties pros = new Properties();
 7         pros.load(is);
 8 
 9         String user = pros.getProperty("user");
10         String password = pros.getProperty("password");
11         String url = pros.getProperty("url");
12         String driverClass = pros.getProperty("driverClass");
13 
14         //2.加载驱动
15         Class.forName(driverClass);
16 
17         //3.获取连接
18         Connection conn = DriverManager.getConnection(url, user, password);
19         System.out.println(conn);
20 
21 
22     }

 

配置文件:

user=root
password=crx666
url=jdbc:mysql://localhost:3306/test
driverClass=com.mysql.jdbc.Driver

 

posted @ 2021-11-29 20:46  一根大腿毛  阅读(407)  评论(0)    收藏  举报