jdbc连接mysql数据库(5.7)

package cn.danawill;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Date;

public class TestMySql {
    public static void main(String[] args) {

        try {
            // 1.加载驱动类
            Class.forName("com.mysql.cj.jdbc.Driver").newInstance();
            // 获得的链接
            Connection connection = DriverManager
                    .getConnection(
                            "jdbc:mysql://localhost:3306/db?serverTimezone=UTC&useUnicode=true&characterEncoding=utf-8&useSSL=false",
                            "root", "123456");
            // 获得一个statement对象
            Statement st = connection.createStatement();
            String sql = "SELECT * FROM employeeinfo";
            ResultSet rs = st.executeQuery(sql);
            while (rs.next()) {
                int ID=rs.getByte(1);
                String name = rs.getString(2);
                Date date = rs.getDate(3);
                float salary = rs.getFloat(4);
                String job = rs.getString(5);
                int dept = rs.getInt(6);
                System.out.print(ID+"\t");
                System.out.print(name+"\t");
                System.out.print(date+"\t");
                System.out.print(salary+"\t");
                System.out.print(job+"\t");
                System.out.println(dept+"\t");
            }

        } catch (ClassNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (InstantiationException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

}

你可能感兴趣的:(jdbc连接mysql数据库(5.7))