DJBC获取数据

package text01;
import java.sql.*;
public class jdbc {
    public static void main(String[] args)  throws Exception{
        //0准备工作
//        String url="jdbc:mysql://localhost:3306/db_demo?useUnicode=true&characterEncoding=utf8";
//        String user="root";
//        String password="123";
        String sql="select * from emp";
        //1注册驱动,让虚拟机加载驱动类Driver(权限定路径,包.类)
        Class.forName("com.mysql.cj.jdbc.Driver");
        //2获取连接对象Connection
        Connection conn = DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/db_demo?useUnicode=true&characterEncoding=UTF-8&userSSL=false&serverTimezone=GMT%2B8","root","123");
        //3基于连接对象获取发送器statement
        Statement stmt = conn.createStatement();
        //4发送SQL语句,得到结果ResultSet
        ResultSet rs = stmt.executeQuery(sql);
        //5处理结果
        while (rs.next()){
            int empno=rs.getInt("empno");//列索引从1开始 写0错
            String enaem=rs.getString("ename");
            Date hiredate = rs.getDate("hiredate");
            double sal = rs.getDouble("sal");
            System.out.println(empno+":"+enaem+":"+hiredate+":"+sal);
            System.out.println("-----------------");

        }
        //6关闭资源 先开的后关闭
        rs.close();
        stmt.close();
        conn.close();
    }
}

你可能感兴趣的:(DJBC获取数据)