java程序中连接数据库(基于Jdbc,非连接池)

此为传统方法连接数据库,没有用连接池。
连接数据库步骤:
注册驱动----------打开连接-------------------执行语句------------关闭

(2021.8.29) 跑通每一种方法,但是原理不太懂后面慢慢再体会。待学知识点:反射

数据库为test2,创建表actor,

在这里插入图片描述
在这里插入图片描述
结果
在这里插入图片描述

1
属于静态加载,灵活性差,依赖强

public class Jdbctest {

    @Test
    public void connection() throws SQLException {
       
        Driver driver=new com.mysql.cj.jdbc.Driver();

        String url="jdbc:mysql://localhost:3306/test2?serverTimezone=GMT";


        Properties properties=new Properties();
        properties.setProperty("user","root");
        properties.setProperty("password","ning");
        Connection connection=driver.connect(url,properties);

        //System.out.println(connection);
		//--------------------------------------------------------------
        String sql="insert into actor values(null,'刘大海','男')";

        Statement statement=connection.createStatement();
      
        int rows=statement.executeUpdate(sql);
        System.out.println(rows>0?"成功":"失败");
		
		statement.close();
        connection.close();




    }
}

使用反射加载Driver类,动态加载,更加的灵活,减少依赖性



@Test
    public void connection2() 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/test2?serverTimezone=GMT";


        Properties properties=new Properties();

        properties.setProperty("user","root");

        properties.setProperty("password","ning");

        Connection connection=driver.connect(url,properties);

        //System.out.println(connection);
		//----------------------------------------------------------------------
        String sql2="insert into actor values(null,'刘大海','男')";

        Statement statement=connection.createStatement();
        int rows=statement.executeUpdate(sql2);

        System.out.println(rows>0?"成功":"失败");


        statement.close();
        connection.close();




    }

推荐使用以下方法

利用Driver+DriverManager

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

        String url="jdbc:mysql://localhost:3306/test2?serverTimezone=GMT";
        String user="root";
        String password="ning";

        DriverManager.registerDriver(driver);//本句可以不要,因为使用Class.forName自动完成注册驱动


        Connection connection=DriverManager.getConnection(url,user,password );

        //------------------------------
        String sql3="insert into actor values(null,'刘大海','男')";

        Statement statement=connection.createStatement();
        int rows=statement.executeUpdate(sql3);

        System.out.println(rows>0?"成功":"失败");


        statement.close();
        connection.close();

    }

单独写一个配置文件Mysql.properties,从配置文件中加载数据

在这里插入图片描述

@Test
    public void connection4() throws IOException, SQLException {
        Properties properties=new Properties();
        properties.load(new FileInputStream("src\\Mysql.properties"));

        String user=properties.getProperty("user");
        String password=properties.getProperty("password");
        String driver=properties.getProperty("driver");
        String url=properties.getProperty("url");

        Connection connection=DriverManager.getConnection(url,user,password );

        String sql5="insert into actor values(null,'刘大海','男')";

        Statement statement=connection.createStatement();
        int rows=statement.executeUpdate(sql5);

        System.out.println(rows>0?"成功":"失败");


        statement.close();
        connection.close();

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Abner_iii

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值