JDBC连接数据库的6种方法,5方法最代码最少,6方法配置文件properties文件

记得下载jdbc的插件jar,然后添加库

import com.mysql.cj.jdbc.Driver; import com.sun.xml.internal.ws.developer.UsesJAXBContext; import org.junit.Test; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.IOException; import java.sql.Connection; import java.sql.DriverManager; import java.sql.SQLException; import java.sql.Statement; import java.util.Properties; //连接mysql的6种方法

    @Test
    @SuppressWarnings({"all"})
 public void show1() throws SQLException { //灵活性低,依赖性高
        //前置工作,在项目下创建目录,把mysql.jar 拷贝改目录下,点击add to project (添加为库)
        //1.注册驱动
        Driver driver=new com.mysql.jdbc.Driver(); //创建Deiver对象 加载驱动
        //2.得到连接
        String url="jdbc:mysql://localhost:3306/db01";
        //将用户名和密码放入到Properties对象
        Properties properties=new Properties();
        properties.setProperty("user","root");
        properties.setProperty("password","sasa");
        Connection connection= driver.connect(url,properties); //连接mysql
    
    }

@Test
public void show2() throws SQLException, ClassNotFoundException, InstantiationException, IllegalAccessException {
    //使用反射加载Driver类 动态加载,更加的灵活,减少依赖性
     Class<?> aClass = Class.forName("com.mysql.jdbc.Driver");
     Driver driver = (Driver) aClass.newInstance();
     String url="jdbc:mysql://localhost:3306/db01";
     Properties properties=new Properties();
    properties.setProperty("user","root");
    properties.setProperty("passwrd","sasa");
    Connection connect= driver.connect(url,properties);
    System.out.println("方法2"+connect);
}

//方法3 使用DriverManager 替代 Properties
@Test
public void show3() throws ClassNotFoundException, InstantiationException, IllegalAccessException, SQLException {
    final Class<?> aClass = Class.forName("com.mysql.jdbc.Driver");
    Driver driver=(Driver) aClass.newInstance();
    String url="jdbc:mysql://localhost:3306/db01";
    DriverManager.registerDriver(driver);//注册Driver驱动z
    Connection connection=DriverManager.getConnection(url,"root","sasa");
    
}
​
//方法4 使用Class.forName 自动完成驱动,简化代码
@Test
public void show4() throws ClassNotFoundException, SQLException {
    Class.forName("com.mysql.cj.jdbc.Driver"); //底层逻辑 静态块会自动注册Driver 驱动com.mysql.cj.jdbc.Driver 源代码已经自动加载驱动程序类
    Connection connection=DriverManager.getConnection("jdbc:mysql://localhost:3306/db01","root","sasa");
    System.out.println("第4种方法:"+connection);
    connection.close();
}


@Test
@SuppressWarnings({"all"}) //对被批注的代码元素内部的某些警告保持静默。
public void show5() throws SQLException {
    //mysql驱动5.1.6就可以无需Class.forName()
    //从jdk1.5以后使用jdbc4,不在需要调用class.forName()注册驱动而是自动调用驱动
    // jar包下META-INF包下面的services的java.sql.Driver有
    Connection connection=DriverManager.getConnection("jdbc:mysql://localhost:3306/db01","root","sasa");
    System.out.println("第5种方法"+connection);
    connection.close();
}
​

@Test
public void show6() throws IOException, ClassNotFoundException, SQLException {
    //添加配置文件,让mysql更加灵活
    //通过Properties对象获取文件的信息
    Properties properties=new Properties();
    properties.load(new FileInputStream("src\\mysql.properites"));
    String user=properties.getProperty("user"); //通过key值获取Value值
    String password=properties.getProperty("password");
    String driver= properties.getProperty("driver");
    String url= properties.getProperty("url");
    Class.forName(driver);
    Connection connection=DriverManager.getConnection(url,user,password);
    System.out.println("配置文件"+connection);
}

}

配置文件,在src右键新建File名字为mysql.properites

根据自己的名字和密码进行修改 

user=root
password=sasa
url=jdbc:mysql://localhost:3306/db01
driver=com.mysql.cj.jdbc.Driver

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值