java怎么从mysql查询出来的结果转化为二维数组,举个例子

2025-03-28 05:42:25
推荐回答(3个)
回答1:

package db;

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

/**
 * 
 * 数据库连接类,通用
 * 
 * @author lsy
 * 
 */
public class DBConnection {
public static final String driver = "com.mysql.jdbc.Driver";// 驱动
public static final String url = "jdbc:mysql://localhost:3306/mydb";// mysql固定的URL:jdbc:mysql://localhost:3306/数据库名(我这里是mydb)
public static final String user = "root";// 我的数据库的用户名
public static final String pwd = "123";// 我的数据库密码

public static Connection dBConnection() {
Connection con = null;
try {
// 加载mysql驱动器
Class.forName(driver);
// 建立数据库连接
con = DriverManager.getConnection(url, user, pwd);
} catch (ClassNotFoundException e) {
System.out.println("加载驱动器失败");
e.printStackTrace();
} catch (SQLException e) {
System.out.println("注册驱动器失败");
e.printStackTrace();
}
return con;
}
}
package dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import db.DBConnection;

/**
 * 
 * 取出数据,存到二维数组里
 * 
 * @return flowers
 * @author lsy
 */
public class SelectData {
public static final String SELECT = "select* from iris_PCA";
public static final int k = 4;// 4个属性

public double[][] getFlowers() throws SQLException {
Connection con = DBConnection.dBConnection();
ResultSet rs;// 创建结果集
PreparedStatement pstmt = con.prepareStatement(SELECT);// 创建一个PreparedStatement对象
rs = pstmt.executeQuery();

// 为初始化数组的大小提供方便
int sample = 0;
while (rs.next()) {
sample++;
}

double[][] flower = new double[sample][k];
rs = pstmt.executeQuery();// 特别重要,否则取到的全是0。因为执行上面的while(rs.next())后,ResultSet对象的下标已指到0。
// API:当生成ResultSet对象的Statement对象关闭、重新执行或用来从多个结果的序列获取下一个结果时,ResultSet对象将自动关闭。
for (int i = 0; rs.next(); i++) {
for (int j = 0; j < k; j++) {
flower[i][j] = rs.getDouble(j + 2);
}
}

// 输出二维数组
System.out.println("花花: ");
for (int i = 0; i < flower.length; i++) {
for (int j = 0; j < flower[0].length; j++) {
System.out.print(flower[i][j] + "\t");
}
System.out.println();
}
pstmt.close();
rs.close();
con.close();
return flower;
}

public static void main(String[] args) {
try {
SelectData selectData = new SelectData();
selectData.getFlowers();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}

回答2:

数组是定长的,建议转化为list列表;

有一个stu实体类,属性你应该能看懂吧,那看不懂继续追问
 public ArrayList queryAll() {
       
        String sql = "select * from stu_manger_sys";
        selectAll =true;
        this.setApp(sql);
        return list;
    }
     public void setApp(String sql){
         Statement stmt = null;
        ResultSet rs = null;
        StuNum =0;
         list =  new ArrayList<>();
        try {
            stmt = conn.createStatement();
            rs = stmt.executeQuery(sql);
            while(rs.next()){
                stu = new Student();
                stu.setId(rs.getInt(1));
                stu.setName(rs.getString(2));
                stu.setAge(rs.getInt(3));
                stu.setDate(rs.getDate(4));
                stu.setHome(rs.getString(5));
                stu.setPhoneNumber(rs.getString(6));
                 StuNum++;
                if(selectAll==true) {
                    list.add(stu);
                }
            }          
        } catch (SQLException ex) {    
            
        }

回答3:

为什么要转换成数组呢?而且还是二维的数组定长的,查询数据库的结果不是固定的,查询结果返回的可能是一条记录,可能是记录的集合,可能是一个数字