Java实现数据库连接的五种方法

数据库连接方式

方式一

直接使用com.mysql.jdbc.Driver(),属于静态加载,灵活性差,依赖性强
推出方式2

public void connect01() throws SQLException {
      //创建dirver对象
       Driver driver =new Driver();

       String  url = "jdbc:mysql://localhost:3306/studb";
       //用户名与密码放入properties对象
       Properties properties = new Properties();
       properties.setProperty("user","root");
       properties.setProperty("password","123456");
       //建立连接
       Connection connection = driver.connect(url,properties);
       System.out.println(connection);
   }

方式二

利用反射加载Driver类,动态加载,更加灵活

 public void connect02() throws ClassNotFoundException, InstantiationException, IllegalAccessException, SQLException {
        Class<?> aClass = Class.forName("com.mysql.cj.jdbc.Driver");
        Driver driver = (Driver) aClass.newInstance();

        String  url = "jdbc:mysql://localhost:3306/studb";
        //用户名与密码放入properties对象
        Properties properties = new Properties();
        properties.setProperty("user","root");
        properties.setProperty("password","123456");
        //建立连接
        Connection connection = driver.connect(url,properties);
        System.out.println(connection);

    }

方式三

用DriverManager替代Driver进行统一管理,具有更好的扩展性

 public void connect03() throws ClassNotFoundException, InstantiationException, IllegalAccessException, SQLException {
        //利用反射加载Driver
        Class<?> aClass = Class.forName("com.mysql.cj.jdbc.Driver");
        Driver driver = (Driver) aClass.newInstance();
        //创建url和user和password
        String  url = "jdbc:mysql://localhost:3306/studb";
        String user = "root";
        String password = "123456";
//      注册Driver驱动
        DriverManager.registerDriver(driver);
        Connection connection = DriverManager.getConnection(url, user, password);
        System.out.println(connection);
    }

方式四

使用Class.forName 自动完成注册驱动,简化代码
是使用最多的

public void connect04() throws ClassNotFoundException, SQLException {
        //利用反射加载Driver类
        //在加载Driver类时,完成注册
        /*源码
        1.静态代码块,在类加载时,会执行一次
        2.DriverManager.registerDriver(new Driver));
        static {
        try {
            DriverManager.registerDriver(new Driver());
        } catch (SQLException var1) {
            throw new RuntimeException("Can't register driver!");
        }
    }
}
         */
        Class.forName("com.mysql.cj.jdbc.Driver");

        //创建url,user,password
        String  url = "jdbc:mysql://localhost:3306/studb";
        String user = "root";
        String password = "123456";
        Connection connection = DriverManager.getConnection(url,user,password);

        System.out.println(connection);
    }

方式五

实际开发中比较好的,在方式4上改进,使用配置文件,连接数据库更加灵活

 public void connect05() throws IOException, ClassNotFoundException, SQLException {
        //通过properties对象获取文件信息
        Properties properties = new Properties();
        properties.load(new FileInputStream("src\\Jdbc\\myjdbc\\mysql.properities"));
        //获取相关的值
        String user = properties.getProperty("user");
        String url = properties.getProperty("url");
        String driver = properties.getProperty("driver");
        String password = properties.getProperty("password");

        Class.forName(driver);
        Connection connection = DriverManager.getConnection(url,user,password);
        System.out.println(connection);
    }

  • 3
    点赞
  • 41
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要连接Java数据库,需要使用Java中的JDBC(Java Database Connectivity)API。以下是连接Java数据库的基本步骤: 1. 导入JDBC驱动程序:要连接到数据库,需要导入适当的JDBC驱动程序。每个数据库都有自己的JDBC驱动程序,可以从相应的官方网站上下载。 2. 加载驱动程序:使用Class.forName()方法加载JDBC驱动程序。 3. 建立连接:使用DriverManager.getConnection()方法建立数据库连接。在getConnection()方法中,需要指定数据库的URL、用户名和密码。 4. 创建Statement对象:使用Connection对象的createStatement()方法创建Statement对象。Statement对象用于执行SQL查询或更新。 5. 执行查询或更新:使用Statement对象的executeQuery()方法执行SQL查询,使用executeUpdate()方法执行SQL更新。 6. 处理结果:如果执行的是SQL查询语句,可以使用ResultSet对象来处理查询结果。 7. 关闭连接:在完成数据库操作后,必须关闭数据库连接,以释放资源。 以下是连接MySQL数据库的示例代码: ``` import java.sql.*; public class JdbcExample { public static void main(String[] args) { Connection con = null; Statement stmt = null; ResultSet rs = null; try { // 加载MySQL驱动程序 Class.forName("com.mysql.jdbc.Driver"); // 建立数据库连接 con = DriverManager.getConnection("jdbc:mysql://localhost:3306/mydatabase", "username", "password"); // 创建Statement对象 stmt = con.createStatement(); // 执行SQL查询语句 rs = stmt.executeQuery("SELECT * FROM mytable"); // 处理查询结果 while (rs.next()) { System.out.println(rs.getString("column1")); } } catch (Exception e) { e.printStackTrace(); } finally { // 关闭数据库连接 try { if (rs != null) rs.close(); if (stmt != null) stmt.close(); if (con != null) con.close(); } catch (SQLException e) { e.printStackTrace(); } } } } ```

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值