当前位置:网站首页>JDBC reads MySQL data list

JDBC reads MySQL data list

2022-06-27 07:21:00 Qingyun ing

import com.mysql.jdbc.Driver;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;

public class JDBCDemo {
    public static void main(String[] args) throws Exception {

        //1 Get the connection
        String url = "jdbc:mysql://IP:Port/db_name";
        String username = "username ";
        String password = "password ";
        Connection conn = DriverManager.getConnection(url, username, password);

        //2 sql sentence
        String sql = "select * from account ";
        //3 Access to perform sql The object of
        Statement stmt = conn.createStatement();
        //4 perform sql
        ResultSet rs = stmt.executeQuery(sql);

        while (rs.next()){
            System.out.println("-----------------");
            System.out.println("ID:"+rs.getInt("id")+" Name:"+rs.getString("name")+" Age:"+rs.getInt("age"));
        }

        //6 Release resources
        stmt.close();
        conn.close();
    }
}

原网站

版权声明
本文为[Qingyun ing]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/178/202206270655101939.html