N. Achyut java,Uncategorized Create 2D Array capable of holding all the data contained in this table from Database.

Create 2D Array capable of holding all the data contained in this table from Database.

Create 2Drray capable of holding all the data contained in this table from Database.

Create 2D Array capable of holding all the data contained in this table from Database / Fetching Data and storing in 2d Array /Fetching Data from database and storing in 2d Array

Assume that a database Named Astronomy contains the name and mass of each of the 8 planet of the solar system and its distance from the sun in a table with the schema planets (id, planet, mass, distance). Create 2Drray capable of holding all the data contained in this table. Use JDBC to populate the array from the data in the table. The 2D array should al so hold the calculated value of gravitational force between each planet and the sun. This can be calculated using the data you just retrieved. Display the data in the final 2D array in a Swing JTable component with appropriate column headers.

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package database;

import java.awt.Color;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import javax.swing.JFrame;
import static javax.swing.JFrame.EXIT_ON_CLOSE;
import javax.swing.JLabel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.table.DefaultTableCellRenderer;

/**
 *
 * @author aashish
 */
public class Planet extends JFrame {

    /**
     * @param args the command line arguments
     */
    static String column[] = {"ID", "Plante Name", "Mass", "Distance", "Force of attraction"};
    static String[][] operationalArray = new String[8][5];
    static int counter = 0;

    public static void main(String[] args) throws SQLException {
        Planet app = new Planet();
        app.getContentPane().setBackground(Color.LIGHT_GRAY);
        app.setTitle("Planet Table");
        app.setVisible(true);

    }

    public Planet() throws SQLException {

        setSize(700, 700);

        String url = "jdbc:mariadb://localhost:3306/Astronomy";
        String username = "root";
        String password = "";

        Connection connection = DriverManager.getConnection(url, username, password);
        Statement statement = connection.createStatement();

        String sql = "select * from planets";
        ResultSet rs = statement.executeQuery(sql);
        int rowcounter = 0;
        while (rs.next()) {
            operationalArray[rowcounter][0] = String.valueOf(rs.getInt("ID"));
            operationalArray[rowcounter][1] = rs.getString("planet");
            operationalArray[rowcounter][2] = String.valueOf(rs.getDouble("mass"));
            operationalArray[rowcounter][3] = String.valueOf(rs.getDouble("distance"));

            double G = 6.67430e-11;
            double Ms = 1.989e+30;
            double Mp = rs.getDouble("mass");
            double r = rs.getDouble("distance");

            double F = G * ((Ms * Mp) / Math.pow(r, 2));

            operationalArray[rowcounter][4] = String.valueOf(F);

            rowcounter = rowcounter + 1;

        }

        JTable jt = new JTable(operationalArray, column);
        jt.setBounds(30, 40, 200, 300);

        DefaultTableCellRenderer centerRenderer = new DefaultTableCellRenderer();
        centerRenderer.setHorizontalAlignment(JLabel.CENTER);
        jt.setDefaultRenderer(String.class, centerRenderer);
        JScrollPane sp = new JScrollPane(jt);

        add(sp);

        pack();
        setDefaultCloseOperation(EXIT_ON_CLOSE);

    }

}

Output of program

Download Project with database :

Related Post