JAVA学习之路08——IDEA连接并读取MySQL数据库内容

新建一个资源文件:
在这里插入图片描述
注意资源文件存放的位置:
在这里插入图片描述
ConJDBC类用于加载资源文件,建立和数据库的连接。

import java.io.IOException;
import java.sql.*;
import java.util.Properties;

public class ConJDBC {

    private Properties pro;
    private Connection con;
    //构造方法
    public ConJDBC() {
        pro=new Properties();
        try {
            //加载资源文件
            pro.load(Thread.currentThread().getContextClassLoader().getResourceAsStream("res.properties"));
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    //建立和数据库的连接
    public Connection getCon() {
        try {
            //加载驱动类
            Class.forName(pro.getProperty("MySqlDriver"));
            //建立连接
            con = DriverManager.getConnection(pro.getProperty("url"),
                    pro.getProperty("user"), pro.getProperty("pwd"));
        } catch (ClassNotFoundException | SQLException e) {
            e.printStackTrace();
        }
        return con;
    }
    //关闭连接
    public void close(ResultSet rs, Statement stm,Connection con) {
        try {
            rs.close();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        try {
            stm.close();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        try {
            con.close();
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
}

定义一个井信息类,存放井的信息。

public class WInfo {
    String wellName;
    int dep;//深度
    int X;
    int Y;
    int Z;//大地坐标
    public WInfo(String name,int dep,int X,int Y,int Z){
        this.wellName=name;
        this.dep=dep;
        this.X=X;
        this.Y=Y;
        this.Z=Z;
    }
    public String getWellName() {
        return wellName;
    }

    public int getDep() {
        return dep;
    }

    public int getX() {
        return X;
    }

    public int getY() {
        return Y;
    }

    public int getZ() {
        return Z;
    }

    @Override
    public String toString() {
        return "{" +
                "wellName=" + wellName + '井' +
                ", 测深为=" + dep +
                ", X坐标=" + X +
                ", Y坐标=" + Y +
                ", Z坐标=" + Z +
                '}';
    }
}

测试函数,连接数据库并打印输出数据库中的全部数据:

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
public class Test {
    public static void main(String[] args) throws SQLException {
        ConJDBC cj=new ConJDBC();
        //获取连接
        Connection con=cj.getCon();
        String sql="select * from Well_information ";
        PreparedStatement pre=null;
        try {
            pre=con.prepareStatement(sql);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        ResultSet re=null;
        try {
            re=pre.executeQuery();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        //打印输出结果
        System.out.println(res(re).toString());
       //关闭连接
        cj.close(re, pre, con);
    }
    //将数据库中的结果转化成对象,存放到数组中。
   private static StringBuilder res(ResultSet re) throws SQLException {
        StringBuilder strB=new StringBuilder();
        ArrayList<WInfo> wells=new ArrayList<>();
      int i=0;
       while(re.next()){
       //将结果存放到数组中
           wells.add(new WInfo(re.getString(1),re.getInt(2),
             re.getInt(3),re.getInt(4),re.getInt(5)));
       //将数组中的结果转为字符串添加到StringBuilder中      
           strB.append(wells.get(i++).toString()+"\n");
       }
        return strB;
    }
}

数据库中的数据为(MySQL5.7+navicat):
在这里插入图片描述
输出的结果为:
在这里插入图片描述

  • 2
    点赞
  • 15
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值