Java教程

JAVA查询结果按照对象返回结果

本文主要是介绍JAVA查询结果按照对象返回结果,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

主程序

package com.pp.bean;

import com.pp.util.JdbcUtil;

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

public class SelJson {
    public static void main( String[] args)
            throws SQLException, ClassNotFoundException {

        try {
            ArrayList<Student>  list  = new ArrayList<Student>();
            Connection conn=JdbcUtil.getCconnection();
            String sqls="select id,tname,ywen,shux from t1  ";
            PreparedStatement stmt = conn.prepareStatement(sqls);
            ResultSet rs1 = stmt.executeQuery();

            while ( rs1.next() ) {
                Student student=new Student();
                student.setId(rs1.getInt(1));
                student.setName(rs1.getString(2));
                student.setYwen(rs1.getFloat(3));
                student.setShux(rs1.getFloat(4));
                list.add(student);
            }
            System.out.println(list);
            JdbcUtil.CloseResource(conn,stmt,rs1);
        } catch( Exception e) {
            e.printStackTrace();
        }
        return;
    }
}

Student类

package com.pp.bean;

public class Student {
    private int id;
    private String name;
    private Float ywen;
    private Float shux;

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Float getYwen() {
        return ywen;
    }

    public void setYwen(Float ywen) {
        this.ywen = ywen;
    }

    public Float getShux() {
        return shux;
    }

    public void setShux(Float shux) {
        this.shux = shux;
    }

    @Override
    public String toString() {
        return "Student{" +
                "id=" + id +
                ", name='" + name + '\'' +
                ", ywen='" + ywen + '\'' +
                ", shux='" + shux + '\'' +
                '}';
    }
}

JdbcUtil类

package com.pp.util;

import java.sql.*;
import java.util.ResourceBundle;

public class JdbcUtil {
     private static  String driver ;
     private static  String url ;
     private static  String user ;
     private static  String password ;

    static {
        try {
            ResourceBundle bundle = ResourceBundle.getBundle("db");
            driver=bundle.getString("driver");
            url=bundle.getString("url");
            user=bundle.getString("user");
            password=bundle.getString("password");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static Connection getCconnection(){
        try {
            Class.forName(driver);
            Connection connection=DriverManager.getConnection(url,user,password);
            return connection;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    public static void CloseConnection(Connection connection){
        if(connection!=null){
            try {
                connection.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
            connection=null;
        }

    }
    public static void CloseStatement(Statement statement){
        if(statement!=null){
            try {
                statement.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
            statement=null;
        }

    }
    public static void CloseResult(ResultSet resultSet){
        if(resultSet!=null){
            try {
                resultSet.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
            resultSet=null;
        }

    }
    public static void CloseResource(Connection connection,Statement statement,ResultSet resultSet){
        CloseResult(resultSet);
        CloseStatement(statement);
        CloseConnection(connection);
    }
}

数据库连接仍然是调用了db.properties和JdbcUtil

执行之后的返回结果如下:

[Student{id=1, name='张三', ywen='90.0', shux='91.0'}, Student{id=2, name='李四', ywen='91.0', shux='91.0'}, Student{id=3, name='王五', ywen='92.0', shux='90.0'}, Student{id=4, name='刘六', ywen='93.0', shux='89.0'}, Student{id=5, name='钱七', ywen='94.0', shux='88.0'}, Student{id=6, name='赵八', ywen='95.0', shux='87.0'}]

这篇关于JAVA查询结果按照对象返回结果的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!