尝试使用SQL数据库中的数据填充JTable时出现java.lang.NullPointerException错误

时间:2013-12-01 00:04:05

标签: java

我正在尝试连接到sqllite数据库,当我尝试使用SQL数据库中的数据填充JTable时,我收到此错误。我有四个类,我认为错误发生在Informatii_evenimente.java类中。程序正在连接到数据库,因为他正确识别了id和nume(名称)

我需要一些帮助

现在程序正在从数据库中获取数据,但是没有正确显示。我对Informatii_evenimente.java进行了修改

新的Informatii_evenimente.java

package view;


import java.awt.BorderLayout;
import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.border.EmptyBorder;
import javax.swing.table.DefaultTableModel;

import java.sql.*;
import java.util.Calendar;
import java.util.Vector;

import javax.swing.*;

import net.proteanit.sql.DbUtils;
import databaseconnection.DatabaseConnection;

import java.util.GregorianCalendar;//pt afisare data si ora meniu

import net.proteanit.sql.DbUtils;

public class Informatii_evenimente extends JFrame {

    private JPanel contentPane;
    Connection cnx=null;
    ResultSet rs = null;
    PreparedStatement pst = null;   
    private JTable tabel1;


    /**
     * Create the frame.
     */


    public Informatii_evenimente() {
           cnx=DatabaseConnection.ConnecrDb();

        //  tabel1 = new JTable();

        //  tabel1.setBounds(170, 11, 254, 156);
        //  contentPane.add(tabel1);


            // The Connection is obtained
           try{
           String sql ="select * from COMPANIE";
            pst=cnx.prepareStatement(sql);
            rs=pst.executeQuery();
            // se creaza tabelul

         //   JOptionPane.showMessageDialog(null, new JScrollPane(table));


            setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            setBounds(100, 100, 526, 300);
            contentPane = new JPanel();
            contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
            setContentPane(contentPane);
            contentPane.setLayout(null);

            JScrollPane scrollPane = new JScrollPane();
            scrollPane.setBounds(170, 11, 306, 156);
            contentPane.add(scrollPane);
            JTable table = new JTable(buildTableModel(rs));
            scrollPane.setViewportView(table);


            // inchide conexiune bd
        } catch(Exception e){
            JOptionPane.showMessageDialog(null, e);
        }finally {
           try{
               rs.close();
               pst.close();
           } 
           catch(Exception e){

                      }
            }

    }

    public static DefaultTableModel buildTableModel(ResultSet rs)
            throws SQLException {

        ResultSetMetaData metaData = rs.getMetaData();

        // nume coloane
        Vector<String> columnNames = new Vector<String>();
        int columnCount = metaData.getColumnCount();
        for (int column = 1; column <= columnCount; column++) {
            columnNames.add(metaData.getColumnName(column));
        }

        // date tabel
        Vector<Vector<Object>> data = new Vector<Vector<Object>>();
        while (rs.next()) {
            Vector<Object> vector = new Vector<Object>();
            for (int columnIndex = 1; columnIndex <= columnCount; columnIndex++) {
                vector.add(rs.getObject(columnIndex));
            }
            data.add(vector);
        }

        return new DefaultTableModel(data, columnNames);

    }

}

Informatii_evenimente.java

package view;

import java.awt.BorderLayout;
import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import java.sql.*;
import java.util.Calendar;
import javax.swing.*;
import net.proteanit.sql.DbUtils;
import databaseconnection.DatabaseConnection;
import java.util.GregorianCalendar;//pt afisare data si ora meniu
import net.proteanit.sql.DbUtils;

public class Informatii_evenimente extends JFrame {

    private JPanel contentPane;
    Connection cnx=null;
    ResultSet rs = null;
    PreparedStatement pst = null;   
    private JTable tabel1;


    /**
     * Create the frame.
     */
    public Informatii_evenimente() {

        //initComponents();
        cnx=DatabaseConnection.ConnecrDb();
        Update_tabel1();
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setBounds(100, 100, 450, 300);
        contentPane = new JPanel();
        contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
        setContentPane(contentPane);
        contentPane.setLayout(null);

        tabel1 = new JTable();
        tabel1.setBounds(170, 11, 254, 156);
        contentPane.add(tabel1);
    }

    private void Update_tabel1(){

        try{
        String sql ="select * from COMPANIE";
        pst=cnx.prepareStatement(sql);
        rs=pst.executeQuery();
        tabel1.setModel(DbUtils.resultSetToTableModel(rs));
        }
        catch(Exception e){
            JOptionPane.showMessageDialog(null, e);
        }finally{
       try{
           rs.close();
           pst.close();
       } 
       catch(Exception e){

                  }
        }
    }

}

AgendaPersonala_MainFrame.java

package view;

import java.awt.BorderLayout;
import java.awt.EventQueue;

import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JButton;
import javax.swing.JTextField;

import java.awt.*;
import java.awt.event.*;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;

import javax.swing.*;

import databaseconnection.DatabaseConnection;
public class AgendaPersonala_MainFrame extends JFrame {

    private JPanel contentPane; 
    private JTextField textField_ID;
    private JTextField textField_nume;
    Connection cnx=null;
    ResultSet rs = null;
    PreparedStatement pst = null;


    //pune fereastra din spate peste prima
     public void close(){

         WindowEvent winClosingEvent=new WindowEvent(this,WindowEvent.WINDOW_CLOSING);
         Toolkit.getDefaultToolkit().getSystemEventQueue().postEvent(winClosingEvent);
     }

    /**
     * Create the frame.
     */
    public AgendaPersonala_MainFrame() {

        //initComponents();
        cnx=DatabaseConnection.ConnecrDb();
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setBounds(100, 100, 450, 300);
        contentPane = new JPanel();
        contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
        setContentPane(contentPane);
        contentPane.setLayout(null);

        JButton buton_conectare = new JButton("Conectare");
        buton_conectare.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent arg0) {


                 String sql ="select * from COMPANIE where ID=? and NUME=?";
                    try{
                     pst =cnx.prepareStatement(sql);
                     pst.setString(1,textField_ID.getText());
                     pst.setString(2,textField_nume.getText());

                     rs=pst.executeQuery();
                     if(rs.next()){
                         JOptionPane.showMessageDialog(null, "ID si NUME sunt corecte ");                                     
                     rs.close();
                     pst.close();
                     close();//pt a disparea fereastra din spate si dispose nu exit on close
                     Informatii_evenimente s= new Informatii_evenimente();
                     s.setVisible(true);

                     }
                     else{
                        JOptionPane.showMessageDialog(null, "ID sau NUME nu sunt corecte "); 
                     }
                    }
                    catch(Exception e)
                    {
                      JOptionPane.showMessageDialog(null, e);  

                    }
                    finally{
               try{
                   rs.close();
                   pst.close();
               } 
               catch(Exception e){

                          }
                    }
                }           


        }
    );

        buton_conectare.setBounds(300, 212, 96, 23);
        contentPane.add(buton_conectare);
        buton_conectare.setBackground(Color.LIGHT_GRAY);

        textField_ID = new JTextField();
        textField_ID.setBounds(323, 94, 86, 20);
        contentPane.add(textField_ID);
        textField_ID.setColumns(10);

        textField_nume = new JTextField();
        textField_nume.setBounds(323, 147, 86, 20);
        contentPane.add(textField_nume);
        textField_nume.setColumns(10);

        JLabel Label_ID = new JLabel("ID");
        Label_ID.setBounds(240, 97, 46, 14);
        contentPane.add(Label_ID);

        JLabel Label_Nume = new JLabel("Nume");
        Label_Nume.setBounds(240, 150, 46, 14);
        contentPane.add(Label_Nume);
    }
}

DatabaseConnection.java

package databaseconnection;

import java.sql.*;
import javax.swing.*;
public class DatabaseConnection {

       Connection cnx=null;
      public static Connection ConnecrDb(){

      try{
      Class.forName("org.sqlite.JDBC");
      Connection cnx =DriverManager.getConnection("jdbc:sqlite:C:\\Users\\stefan\\Documents\\NetBeansProjects\\Proiectfabrica\\fabrica.sqlite");


     //JOptionPane.showMessageDialog(null, "Conexiunea s-a realizat");


      return cnx;
      }catch (Exception e){
          JOptionPane.showMessageDialog(null, "EROARE in conexiune");
       return null;   
      }  

   } 


}

Startup.java

package startup;

import java.awt.EventQueue;
import view.AgendaPersonala_MainFrame;


/**
 * Launch the application.
 */
public class Startup {
public static void main(String[] args) {
    EventQueue.invokeLater(new Runnable() {
        public void run() {
            try {
                AgendaPersonala_MainFrame frame = new AgendaPersonala_MainFrame();
                frame.setVisible(true);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    });
}
}

0 个答案:

没有答案