Five steps for using JDBC and query operations-database programming (2)

Source: Internet
Author: User

Five steps for using JDBC and query operations-database programming (2)
Jdbc usage steps

1. Load the jdbc driver.
2. Open the database connection.
3. Create a session and perform basic operations such as adding, deleting, modifying, and querying.
4. process the results
5. Clean up the environment, such as closing the session.

Query operations

First, use the forname method of the Class to instantiate a driver instance.
Then initialize three classes: Connection Statement ResultSet. These three classes are used to establish connections, execute operations, and process results respectively.
DriverManager uses the getConnection method to input three parameters: Database url and user name, and password to obtain the connection instance.
The createStatement method of connection to obtain the Statement instance.
Statement executeQuery method to obtain the ResultSet instance.
Finally, close the resultSet, Sratement, and Connection instances in ascending order.
The Code is as follows:

String sql = "SELECT * FROM tbl_user";        Connection conn = null;        Statement st = null;        ResultSet rs = null;        try {            Class.forName("com.mysql.jdbc.Driver");            conn = (Connection) DriverManager.getConnection("jdbc:mysql://localhost:3306/jsp_dbb", "root", "");            st = (Statement) conn.createStatement();            rs = st.executeQuery(sql);            while (rs.next()) {                System.out.print(rs.getInt("id")+"   ");                System.out.print(rs.getString("name")+"    ");                System.out.print(rs.getString("password")+"    ");                System.out.print(rs.getString("email")+"    ");                System.out.println("");            }        } catch (Exception e) {            e.printStackTrace();            }finally{                try {                    rs.close();                } catch (SQLException e) {                    // TODO Auto-generated catch block                    e.printStackTrace();                }                try {                    st.close();                } catch (SQLException e) {                    // TODO Auto-generated catch block                    e.printStackTrace();                }                try {                    conn.close();                } catch (SQLException e) {                    // TODO Auto-generated catch block                    e.printStackTrace();                }            }

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.