Home > Java > javaTutorial > How to query the database in java

How to query the database in java

coldplay.xixi
Release: 2020-08-17 13:38:54
Original
10996 people have browsed it

How to query the database in java: first create the user and teacher databases; then establish a one-to-many connection between the [user_id] column of the teacher table and the id column of the user table; then add data to the user data table, and Query the user database data according to the conditions; finally query the slave table data according to the main table.

How to query the database in java

[Related learning recommendations: java basic tutorial]

How to query the database in java:

1. Create database

Create user database

How to query the database in java

Create teacher database

How to query the database in java

The user_id column of the teacher table establishes a one-to-many connection with the id column of the user table, using user_id as a foreign key.

How to query the database in java

2. Java programming to query the database

Add data to the user data table

    /**
     * 添加数据
     */
    @Test
    public void addData() {
        Connection connection = null;
        PreparedStatement pstmt =null;
        try {
            connection = JDBCUtils_V3.getConnection();
            String sql = "insert into user values(null,?,?)";
            pstmt = connection.prepareStatement(sql);
            pstmt.setString(1, "wangxuan");
            pstmt.setString(2, "741852");
            int row = pstmt.executeUpdate();
            if (row>0) {
                System.out.println("数据添加成功!");
            }else {
                System.out.println("数据添加失败!");
            }
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }finally{
            JDBCUtils_V3.release(connection, pstmt, null);
        }
    }
Copy after login

Query according to conditions user database data

    /**
     * 按照条件查询数据
     */
    @Test
    public void selectTest() {
        Connection conn = null;
        PreparedStatement pstmt = null;
        ResultSet rs =null;
        try {
            conn = JDBCUtils_V3.getConnection();
            String sql = "select * from user where password = ?";
            pstmt = conn.prepareStatement(sql);
            pstmt.setString(1, "123456");
            rs = pstmt.executeQuery();
            while (rs.next()) {
                System.out.println(rs.getString(1)+"----"+rs.getString(2)+"---"+rs.getString(3));
            }
//            System.out.println(rs);
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }finally{
            JDBCUtils_V3.release(conn, pstmt, rs);
        }
    }
Copy after login

One-to-many query/Query the slave table teacher data based on the main table user

    /**
     * 一对多查询
     * 根据主表查询从表
     */
    @Test
    public void selectOnetoMore() {
        Connection conn = null;
        PreparedStatement pstmt = null;
        ResultSet rs = null;
        try {
            conn = JDBCUtils_V3.getConnection();
//            String sql = "select * from teacher where user_id = (select id from user where username =?) ";
            String sql = "select * from user,teacher where user.id = teacher.user_id ";
            pstmt = conn.prepareStatement(sql);
//            pstmt.setString(1, "wangxuan");
            rs = pstmt.executeQuery();
            while (rs.next()) {
//                System.out.println(rs.getString(1)+"----"+rs.getString(2)+"---"+rs.getString(3)+"---"+rs.getString(4));
                System.out.println(rs.getString(1)+"----"+rs.getString(2)+"---"+rs.getString(3)+"---"+rs.getString(4)+"----"+rs.getString(5)+"----"+rs.getString(6)+"----"+rs.getString(7));
            }
            System.out.println("查询完成");
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }finally{
            JDBCUtils_V3.release(conn, pstmt, rs);
        }
    }
Copy after login

One-to-many query/Query the main table based on the slave table

    /**
     * 一对多查询
     * 根据从表查询主表数据
     */
    @Test
    public void selectMoretoOne() {
        Connection connection = null;
        PreparedStatement pstmtPreparedStatement = null;
        ResultSet rSet =null;
        try {
            connection = JDBCUtils_V3.getConnection();
            String sql = "select * from user where id = (select user_id from teacher where teacher=?)";
            pstmtPreparedStatement = connection.prepareStatement(sql);
            pstmtPreparedStatement.setString(1, "钱田");
            rSet = pstmtPreparedStatement.executeQuery();
            while (rSet.next()) {
                System.out.println(rSet.getString(1)+"----"+rSet.getString(2)+"---"+rSet.getString(3));
            }
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }finally{
            JDBCUtils_V3.release(connection, pstmtPreparedStatement, rSet);
        }
    }
}
Copy after login

Related recommendations: Programming video course

The above is the detailed content of How to query the database in java. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template