将数据从数据库插入到表中

发布于 2024-11-25 02:16:34 字数 4357 浏览 1 评论 0原文

这是我的代码,它不起作用,因为显示空表,没有任何行和列 有人可以告诉问题出在哪里吗

public class test extends JFrame {

    private final JButton pridetiNaujaButton = new JButton();
    private final JButton pasalintiButton = new JButton();
    private final JButton redagotiButton = new JButton();
    private final JButton paieskaButton = new JButton();
    final GtFromDb baz=new GtFromDb();
    private Vector <Vector<String>> data;
    private Vector<String> header;
    private final JScrollPane scrollPane = new JScrollPane();
    private final JTable table = new JTable(data,header);
    public static void main(String args[]) {
        try {
            test frame = new test();
            frame.setVisible(true);
        } catch (Exception e) {
            e.printStackTrace();
        }

    }

    /**
     * Create the frame
     */
    public test() {
        super();
        setBounds(100, 100, 781, 412);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        try {
            jbInit();
        } catch (Throwable e) {
            e.printStackTrace();
        }


            GtFromDb db=new GtFromDb();
            try {
                data=db.getClient();
            } catch (Exception e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }           
            header=new Vector<String>();
            header.add("Imones pavadinimas");
            header.add("vardas");
            header.add("pavarde");
            header.add("salis");
            header.add("Adresas");
            header.add("telefonas");
        }


    private void jbInit() throws Exception {
        getContentPane().setLayout(null);

        getContentPane().add(pridetiNaujaButton);
        pridetiNaujaButton.setText("Prideti nauja");
        pridetiNaujaButton.setBounds(0, 0, 106, 26);

        getContentPane().add(pasalintiButton);
        pasalintiButton.setText("Pasalinti");
        pasalintiButton.setBounds(112, 0, 106, 26);

        getContentPane().add(redagotiButton);
        redagotiButton.setText("Redagoti");
        redagotiButton.setBounds(224, 0, 106, 26);

        getContentPane().add(paieskaButton);
        paieskaButton.setText("Paieska");
        paieskaButton.setBounds(336, 0, 106, 26);

        getContentPane().add(scrollPane);
        scrollPane.setBounds(10, 48, 745, 316);     
        scrollPane.setViewportView(table);

    }
    public class GtFromDb
    { Connection connect;
      Statement zadanie;
      String sql;
      ResultSet dane;
    //  Map<String,String> carmap =new HashMap<String,String>();
    //  Map<String,String> ownmap =new HashMap<String,String>();
    void DbConnection() throws SQLException
    {
        String baza="jdbc:odbc:dielektric_repair";
        try
        {
        Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
        }catch(Exception e){System.out.println("Connection error");}
        connect = DriverManager.getConnection(baza,"","");
    }
    public Vector getClient() throws Exception
    {
    Vector <Vector<String>> clientVector=new Vector<Vector<String>>();  
    try
    {DbConnection();
    PreparedStatement zadanie = connect.prepareStatement("SELECT * FROM Clients");

    dane = zadanie.executeQuery();
    while(dane.next()) {
    Vector <String> client=new Vector<String>();
     String imonesPav=dane.getString("Imones_pavadinimas");
     String vardas = dane.getString("Vardas");
     String pavarde = dane.getString("Pavarde");
     String salis = dane.getString("Salis");
     String adresas=dane.getString("Adresas");
     String telefonas = dane.getString("Telefonas");
     if (imonesPav != null) {imonesPav = imonesPav.trim();
     client.add(imonesPav);}
     if (vardas != null) {vardas = vardas.trim();
     client.add(vardas);}
     if (pavarde != null) {pavarde = pavarde.trim();
     client.add(pavarde);}
     if (salis != null) {salis =salis.trim();
     client.add(salis);}
     if (adresas != null) {adresas =adresas.trim();
     client.add(adresas);}
     if (telefonas != null) {telefonas = telefonas.trim();
     client.add(telefonas);}
    }zadanie.close();
    }catch(SQLException e){}
    return clientVector;    
    }
    }
    public JScrollPane getScrollPane() {
        return scrollPane;
    }
}

there's my code, it doesn't work because shows emty table without any rows ir columns
can someone tel where is problem

public class test extends JFrame {

    private final JButton pridetiNaujaButton = new JButton();
    private final JButton pasalintiButton = new JButton();
    private final JButton redagotiButton = new JButton();
    private final JButton paieskaButton = new JButton();
    final GtFromDb baz=new GtFromDb();
    private Vector <Vector<String>> data;
    private Vector<String> header;
    private final JScrollPane scrollPane = new JScrollPane();
    private final JTable table = new JTable(data,header);
    public static void main(String args[]) {
        try {
            test frame = new test();
            frame.setVisible(true);
        } catch (Exception e) {
            e.printStackTrace();
        }

    }

    /**
     * Create the frame
     */
    public test() {
        super();
        setBounds(100, 100, 781, 412);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        try {
            jbInit();
        } catch (Throwable e) {
            e.printStackTrace();
        }


            GtFromDb db=new GtFromDb();
            try {
                data=db.getClient();
            } catch (Exception e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }           
            header=new Vector<String>();
            header.add("Imones pavadinimas");
            header.add("vardas");
            header.add("pavarde");
            header.add("salis");
            header.add("Adresas");
            header.add("telefonas");
        }


    private void jbInit() throws Exception {
        getContentPane().setLayout(null);

        getContentPane().add(pridetiNaujaButton);
        pridetiNaujaButton.setText("Prideti nauja");
        pridetiNaujaButton.setBounds(0, 0, 106, 26);

        getContentPane().add(pasalintiButton);
        pasalintiButton.setText("Pasalinti");
        pasalintiButton.setBounds(112, 0, 106, 26);

        getContentPane().add(redagotiButton);
        redagotiButton.setText("Redagoti");
        redagotiButton.setBounds(224, 0, 106, 26);

        getContentPane().add(paieskaButton);
        paieskaButton.setText("Paieska");
        paieskaButton.setBounds(336, 0, 106, 26);

        getContentPane().add(scrollPane);
        scrollPane.setBounds(10, 48, 745, 316);     
        scrollPane.setViewportView(table);

    }
    public class GtFromDb
    { Connection connect;
      Statement zadanie;
      String sql;
      ResultSet dane;
    //  Map<String,String> carmap =new HashMap<String,String>();
    //  Map<String,String> ownmap =new HashMap<String,String>();
    void DbConnection() throws SQLException
    {
        String baza="jdbc:odbc:dielektric_repair";
        try
        {
        Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
        }catch(Exception e){System.out.println("Connection error");}
        connect = DriverManager.getConnection(baza,"","");
    }
    public Vector getClient() throws Exception
    {
    Vector <Vector<String>> clientVector=new Vector<Vector<String>>();  
    try
    {DbConnection();
    PreparedStatement zadanie = connect.prepareStatement("SELECT * FROM Clients");

    dane = zadanie.executeQuery();
    while(dane.next()) {
    Vector <String> client=new Vector<String>();
     String imonesPav=dane.getString("Imones_pavadinimas");
     String vardas = dane.getString("Vardas");
     String pavarde = dane.getString("Pavarde");
     String salis = dane.getString("Salis");
     String adresas=dane.getString("Adresas");
     String telefonas = dane.getString("Telefonas");
     if (imonesPav != null) {imonesPav = imonesPav.trim();
     client.add(imonesPav);}
     if (vardas != null) {vardas = vardas.trim();
     client.add(vardas);}
     if (pavarde != null) {pavarde = pavarde.trim();
     client.add(pavarde);}
     if (salis != null) {salis =salis.trim();
     client.add(salis);}
     if (adresas != null) {adresas =adresas.trim();
     client.add(adresas);}
     if (telefonas != null) {telefonas = telefonas.trim();
     client.add(telefonas);}
    }zadanie.close();
    }catch(SQLException e){}
    return clientVector;    
    }
    }
    public JScrollPane getScrollPane() {
        return scrollPane;
    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

2024-12-02 02:16:34

有几个问题:

1) 您没有将 client 对象添加到 clientVector 中。使用 clientVector.add(client) 在 ResultSet 的每次迭代后添加客户端。

2) 您已使用 null dataheader 变量初始化表。首先,初始化dataheader 变量,然后初始化table,如下所示:

private final JTable table ;

public test() {
    super();
    setBounds(100, 100, 781, 412);
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    header=new Vector<String>();
    header.add("Imones pavadinimas");
    header.add("vardas");
    header.add("pavarde");
    header.add("salis");
    header.add("Adresas");
    header.add("telefonas");

    GtFromDb db=new GtFromDb();
    try {
        data=db.getClient();
    } catch (Exception e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }  
    table = new JTable(data,header);    

    try {
        jbInit();
    } catch (Throwable e) {
        e.printStackTrace();
    }
}

3) 您捕获了一个SQLException,但没有对其执行任何操作。因此,如果运行查询时出现错误,您将不会知道。尝试打印堆栈跟踪:

try{
   ...
   dane = zadanie.executeQuery();
}catch(SQLException e) {
   e.printStackTrace();
} finally {
   zadanie.close();
}

There are a few issues:

1) You are not adding the client objects to the clientVector. Use clientVector.add(client) to add the client after each iteration of the ResultSet.

2) You have initialised the table with null data and header variables. First, initialise the data and header variables and then the table, like this:

private final JTable table ;

public test() {
    super();
    setBounds(100, 100, 781, 412);
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    header=new Vector<String>();
    header.add("Imones pavadinimas");
    header.add("vardas");
    header.add("pavarde");
    header.add("salis");
    header.add("Adresas");
    header.add("telefonas");

    GtFromDb db=new GtFromDb();
    try {
        data=db.getClient();
    } catch (Exception e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }  
    table = new JTable(data,header);    

    try {
        jbInit();
    } catch (Throwable e) {
        e.printStackTrace();
    }
}

3) You are catching a SQLException and doing nothing with it. So if there is an error running the query, you will not know about it. Try printing out the stack trace:

try{
   ...
   dane = zadanie.executeQuery();
}catch(SQLException e) {
   e.printStackTrace();
} finally {
   zadanie.close();
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文