java连接mysql数据查询数据

package com.cn.peitest.connectDatabase;

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

public class Connect {

	public static void main(String[] args) {
        // 声明Connection对象
        Connection con;
        // 驱动程序名
        String driver = "com.mysql.jdbc.Driver";
        // URL指向要访问的数据库名 test
        String url = "jdbc:mysql://localhost:3306/peixian?serverTimezone=UTC";
        // MySQL配置时的用户名
        String user = "root";
        // MySQL配置时的密码
        String password = "123456";
        // 遍历查询结果集
        try {
            // 加载驱动程序
            Class.forName(driver);
            // 1.getConnection()方法,连接MySQL数据库!!
            con = DriverManager.getConnection(url, user, password);
            if (!con.isClosed())
                System.out.println("\n\t\t成功以 " + user + " 身份连接到数据库!!!");
 
            // 2.创建statement类对象,用来执行SQL语句!!
            Statement statement = con.createStatement();
            // 要执行的SQL语句
            String sql = "select * from pei_user_baseinf";
            // 3.ResultSet类,用来存放获取的结果集!!
            ResultSet rs = statement.executeQuery(sql);
            System.out.println("\n\t\t执行结果如下所示:");
            System.out.println("\t\t-----------------------------------------------------------------");
            System.out.println("\t\t|\t" + "phone_no" + "\t" + "姓名" + "\t" + "性别" + "\t" + "年龄" + "\t" + "手机号码" + "\t\t" + "地址\t|");
            System.out.println("\t\t-----------------------------------------------------------------");
 
            String phone_no = null;
            String Name = null;
            String Sex = null;
            //int Age = 0;
            String Phone = null;
            String Address = null;
 
            while (rs.next()) {
                // 获取 ID 这列数据
            	//phone_no = rs.getInt("phone_no");
            	phone_no = rs.getString("phone_no");
                // 获取 Name 这列数据
                Name = rs.getString("pass_word");
                // 获取 Sex 这列数据
                Sex = rs.getString("sex");
                // 获取 Age 这列数据
                //Age = rs.getInt("Age");
                // 获取 Phone 这列数据
                Phone = rs.getString("user_name");
                // 获取 Address 这列数据
                Address = rs.getString("company_name");
                // 输出结果
                System.out.println("\t\t|\t" + phone_no + "\t" + Name + "\t" + Sex + "\t" + Phone + "\t" + Address + "\t|\t\t");
            }
            System.out.println("\t\t-----------------------------------------------------------------");
            rs.close();
            con.close();
        }
        catch (ClassNotFoundException e) {
            // 数据库驱动类异常处理
            System.out.println("Sorry,can`t find the Driver!");
            e.printStackTrace();
        }
        catch (SQLException e) {
            // 数据库连接失败异常处理
            e.printStackTrace();
        }
        catch (Exception e) {
            // TODO: handle exception
            e.printStackTrace();
        }
        finally {
            System.out.println("\t\t\t\t\t\t\t\t获取数据库数据完毕!!!");
        }
    }
}

  

java连接mysql数据查询数据

上一篇:基于Flink SQL 构建实时数仓


下一篇:Error resolving template [index], template might not exist or might not be accessible by any of the configured Template Resolvers