如何使用JTable在数据库中显示数据

时间:2015-05-26 22:47:31

标签: java mysql

我无法使用JTable在数据库中显示数据,出了什么问题?

MyButton Here;

public void actionPerformed(ActionEvent e) {

            String sorgu="SELECT arac_marka=? , arac_model=? , arac_yas=? , arac_adet=? , arac_orjinallik=? , arac_renk=? , arac_firma='?' , urunkodu=? FROM calisan_urun_ekle";
        DBConnection.Goster(urunkodu.getText(),arac_marka.getText(), arac_model.getText(), arac_yas.getText(), arac_adet.getText(), arac_orjinal.getText(), arac_renk.getText(), arac_firma.getText(), sorgu);      

        calisan_tablosu.setModel(DBConection);

        }
    }

和DBConnection

public static void Goster(String arac_marka, String arac_model, String arac_yas, String arac_adet,String arac_orjinal,String arac_renk,String arac_firma,String urunkodu,String sorgu){


connection();

try
{
    Connection connect = DriverManager.getConnection(host, username , pass);
    PreparedStatement statement = (PreparedStatement) connect.prepareStatement(sorgu);

    statement.setString(1, arac_marka);
    statement.setString(2, arac_model);
    statement.setString(3, arac_yas);
    statement.setString(4, arac_adet);
    statement.setString(5, arac_orjinal);
    statement.setString(6, arac_renk);
    statement.setString(7, arac_firma);
    statement.setString(7, urunkodu);


    statement.executeQuery();
    statement.close();
    connect.close();

和MyTable

calisan_tablosu = new JTable();
    calisan_tablosu.setBounds(28, 250, 590, 184);
    layeredPane.add(calisan_tablosu);

1 个答案:

答案 0 :(得分:0)

 try{
        String url="jdbc:mysql://127.0.0.1/db1";
    ResultSet rs=null;
        Connection con = DriverManager.getConnection(url,"root","");
       String query="select * from customer";
        PreparedStatement pst=null;
        pst=con.prepareStatement(query);
       rs=pst.executeQuery();

        DefaultTableModel tm=(DefaultTableModel)jTable.getModel(); 
        tm.setRowCount(0);

        while(rs.next())
        {
           Object o[]={rs.getInt("Column1_name_in_table(ie:-Cid)"),rs.getString("Cname"),rs.getString("Ccity")}; 
           tm.addRow(o);
        }

   }
    catch(SQLException ex){
       JOptionPane.showMessageDialog(null, ex.getMessage()); 
    }