使用jdbc原生态手写statement连接数据库并查询某一张表

2022-07-26,,,,

数据库:mysql,可视化工具SQLyog
编译器:idea
数据库名:test1
表名:t_user
那些连接数据库的用户名,密码,url全部封装在src下的一个叫jdbc的文件下,通过Properties类去加载并使用它。
最终的代码:

最终的代码:

    @org.junit.Test
    public void testConnectionFinal(){
        //这个String类型的参数是什么意思?答:是数据文件的路径,一般将它新建在src下
        try {
            //1、获取connection连接对象
            Class.forName("com.mysql.jdbc.Driver");
            InputStream is = Test.class.getClassLoader().getResourceAsStream("jdbc");
            Properties properties = new Properties();
            properties.load(is);
            String user = properties.getProperty("user");
            String password = properties.getProperty("password");
            String url = properties.getProperty("url");
            Connection connection = DriverManager.getConnection(url, user, password);
            //2、通过connection对象获取statement查询数据库
            Statement statement = connection.createStatement();
            ResultSet resultSet = statement.executeQuery("select * from t_user");
            while(resultSet.next()){
                System.out.println(resultSet.getInt(1)+"---"
                        +resultSet.getString(2)+"---"+resultSet.getString(3));
            }
        } catch (IOException e) {
            e.printStackTrace();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (SQLException e) {
            e.printStackTrace();
        }

    }

运行结果:

本文地址:https://blog.csdn.net/qq_41709577/article/details/110670456

《使用jdbc原生态手写statement连接数据库并查询某一张表.doc》

下载本文的Word格式文档,以方便收藏与打印。