学习JDBC中的一些笔记,分享给大家,一块学习,一块进步。 使用JDBC连接数据库的五种方式
public void test1() { try { // 1.提供Driver接口实现类对象 Driver driver = new com.mysql.jdbc.Driver(); // 2.提供url,指明具体操作的数据 String url = "jdbc:mysql://localhost:3306/test"; // 3.提供 Properties对象,指明用户名和密码 Properties info = new Properties(); info.setProperty("user", "root"); info.setProperty("password", "0000"); // 4.调用driver的connect,获取连接 Connection connect = driver.connect(url, info); System.out.println(connect); } catch (SQLException e) { e.printStackTrace(); } }方式二
public void test2() { // 对连接数据方式进行迭代,目的是不在程序中出现第三方api,使程序具有更好的移植性 try { // 1.利用反射来提供Driver类实现对象 String className = "com.mysql.jdbc.Driver"; Class clazz = Class.forName(className); Driver driver = (Driver) clazz.newInstance(); // 2.提供url,指明具体操作的数据 String url = "jdbc:mysql://localhost:3306/test"; // 3.提供properties,指明用户名和吗,密码 Properties info = new Properties(); info.setProperty("user","root"); info.setProperty("password","0000"); // 4.调用Driver的connect方法,连接数据库 Connection connect = driver.connect(url, info); System.out.println(connect); } catch (ClassNotFoundException e) { e.printStackTrace(); } catch (InstantiationException e) { e.printStackTrace(); } catch (IllegalAccessException e) { e.printStackTrace(); } catch (SQLException e) { e.printStackTrace(); } }方式三:使用DriverManager来代替Driver
public void test3() { try { // 1 利用反射来获取Driver类对象 String ClassName = "com.mysql.jdbc.Driver"; Class clazz = Class.forName(ClassName); Driver driver = (Driver) clazz.newInstance(); // 2 提供三个连接所需要的基本信息 String url = "jdbc:mysql://localhost:3306/test"; String user = "root"; String password = "0000"; // 3 注册驱动 DriverManager.registerDriver(driver); // 4 连接数据库 Connection connection = DriverManager.getConnection(url, user, password); System.out.println(connection); } catch (ClassNotFoundException e) { e.printStackTrace(); } catch (InstantiationException e) { e.printStackTrace(); } catch (IllegalAccessException e) { e.printStackTrace(); } catch (SQLException e) { e.printStackTrace(); } }方式四:优化DriverMananger连接数据库
public void test4() { try { // 1 获取连接数据库的三个基本信息 String url = "jdbc:mysql://localhost:3306/test"; String user = "root"; String password = "0000"; // 2 加载Driver Class.forName("com.mysql.jdbc.Driver"); /*方式三: Class clazz = Class.forName("com.mysql.jdbc.Driver"); Driver driver = (Driver) clazz.newInstance(); DriverManager.registerDriver(driver); 可以进行优化 但是在mysql的Driver实现类中,声明了如下的操作:加载Driver类时,会自动创建Driver类对象,并且注册驱动 static { try { DriverManager.registerDriver(new Driver()); } catch (SQLException var1) { throw new RuntimeException("Can't register driver!"); } } */ // 3 连接数据库 Connection connect = DriverManager.getConnection(url, user, password); System.out.println(connect); } catch (ClassNotFoundException e) { e.printStackTrace(); } catch (SQLException e) { e.printStackTrace(); } }方式五:将数据库连接需要的4个基本信息声明在配置文件中,通过读取配置文件的方式,获取连接
实际开发中一般使用这种方式
public void test5() { try { // 1.创建properties配置文件,在文件中写入user,password,url,driver信息 // 2. 创建输入流,读取配置文件中的四个基本信息 InputStream is = ConnectionTest.class.getClassLoader().getResourceAsStream("jdbc.properties"); Properties pros = new Properties(); pros.load(is); String user = pros.getProperty("user"); String password = pros.getProperty("password"); String url = pros.getProperty("url"); String driver = pros.getProperty("driver"); // 2. 加载驱动 Class.forName(driver); // 3.连接数据库 Connection connection = DriverManager.getConnection(url, user, password); System.out.println(connection); } catch (IOException e) { e.printStackTrace(); } catch (ClassNotFoundException e) { e.printStackTrace(); } catch (SQLException e) { e.printStackTrace(); } }