使用Java从数据库中检索数据

时间:2013-07-28 16:00:50

标签: java sqlite

我在从数据库获取数据以输出到控制台时遇到问题。而不是检索此信息String sql =“SELECT * FROM COMPANY”;我的输出就是这个

org.sqlite.RS@18893021
1 John javax.swing.JLabel[,0,42,195x16,invalid,alignmentX=0.0,alignmentY=0.0,border=,flags=8388608,maximumSize=,minimumSize=,preferredSize=,defaultIcon=,disabledIcon=,horizontalAlignment=LEADING,horizontalTextPosition=TRAILING,iconTextGap=4,labelFor=,text=Age: ,verticalAlignment=CENTER,verticalTextPosition=CENTER] javax.swing.JLabel[,0,84,195x16,invalid,alignmentX=0.0,alignmentY=0.0,border=,flags=8388608,maximumSize=,minimumSize=,preferredSize=,defaultIcon=,disabledIcon=,horizontalAlignment=LEADING,horizontalTextPosition=TRAILING,iconTextGap=4,labelFor=,text=Address: ,verticalAlignment=CENTER,verticalTextPosition=CENTER] javax.swing.JLabel[,0,126,195x16,invalid,alignmentX=0.0,alignmentY=0.0,border=,flags=8388608,maximumSize=,minimumSize=,preferredSize=,defaultIcon=,disabledIcon=,horizontalAlignment=LEADING,horizontalTextPosition=TRAILING,iconTextGap=4,labelFor=,text=Salary: ,verticalAlignment=CENTER,verticalTextPosition=CENTER]

继承我的代码:

package userinterface;

import javax.swing.JFrame;

import java.awt.FlowLayout;

import javax.swing.JButton;

import javax.swing.JDialog;

import javax.swing.JLabel;

import javax.swing.JOptionPane;

import javax.swing.JTextField;

import backend.SQLiteDb;

import business.Person;

import java.awt.event.ActionListener;

import java.awt.event.ActionEvent;

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

public class GUI {

    static SQLiteDb db;

    public static void main(String[] args) {

        final JFrame jframe = new JFrame();

        JButton btnEnterInfo = new JButton("Enter Info");

        jframe.getContentPane().add(btnEnterInfo, BorderLayout.CENTER);

        jframe.setVisible(true);

        jframe.setSize(300, 300);

        btnEnterInfo.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent e) {

                JLabel fullName = new JLabel("Full Name: ");

                JTextField nameField = new JTextField();

                JLabel age = new JLabel("Age: ");

                JTextField ageField = new JTextField();

                JLabel address = new JLabel("Address: ");

                JTextField addressField = new JTextField();

                JLabel salary = new JLabel("Salary: ");

                JTextField salaryField = new JTextField();

                Object[] ob = { fullName, nameField, age, ageField, address,
                        addressField,

                        salary, salaryField };

                int result = JOptionPane.showConfirmDialog(null, ob, "Part 5",

                JOptionPane.OK_CANCEL_OPTION);

                jframe.getContentPane().setLayout(new FlowLayout());

                if (result == JOptionPane.OK_OPTION) {

                    db = new SQLiteDb();

                    db.createPersonnelTable();
                    String fullName1 = nameField.getText();
                    String userAge = ageField.getText();
                    String userAddress = addressField.getText();
                    String userSalary = salaryField.getText();
                    Person person = new Person(fullName1, Integer
                            .parseInt(userAge), userAddress,

                    Double.parseDouble(salaryField.getText()));

                    db.addPerson(person);

                    String sql = "SELECT * FROM COMPANY";

                    Statement stmt = null;

                    try {
                        java.sql.Statement s;

                        Connection connection = DriverManager
                                .getConnection("JDBC:sqlite:Andy.db");
                        stmt = connection.createStatement();
                        ResultSet rs = stmt.executeQuery(sql);
                        while (rs.next()) {
                            int id_col = rs.getInt("ID");
                            String name = rs.getString("name");
                            int age1 = rs.getInt("Age");
                            float address1 = rs.getFloat("Address");
                            double salary1 = rs.getInt("Salary");
                            System.out.println(rs);
                            String output = id_col + " " + name + " " +          age + " " + address + " " + salary;
                            System.out.println(output);
                            rs.close();
                        }
                    } catch (SQLException ee) {
                        System.out.println("ee error");
                        ee.printStackTrace();

                    } finally {
                        if (stmt != null) {
                            try {
                                stmt.close();
                            } catch (SQLException e1) {
                                // TODO Auto-generated catch block
                                System.out.println("e1 error");
                             e1.printStackTrace();
                            }
                        }
                    }


                }

            }
        });

    }

    public static void viewTable(Connection con, String dbName)
            throws SQLException {

    }

1 个答案:

答案 0 :(得分:2)

你打错了。您应该age1而不是age

String output = id_col + " " + name + " " +          age1 + " " + address + " " + salary;

ageJLabel个对象。