2013-12-16 70 views
0

请看看这个代码,并告诉我在哪里,我错了:无法在Java获取从MS Access数据库数据

我只是想这是通过组合框选自服务的价格我MS Access数据库 并在文本字段

service_box=new JComboBox(); 
    service_box.setSize(20,25); 
    service_box.addItem("Select a Service"); 
    service_box.addItem("Hair Cut"); 
    service_box.addItem("Facial"); 
    service_box.addItem("Bleaching"); 

显示它.....

service_box.addItemListener(this); 
    add_bt.addActionListener(this); 

...... ....

@Override 
public void itemStateChanged(ItemEvent i){ 

    service_name=(String)i.getItem(); 

} 

@Override 
public void actionPerformed(ActionEvent ie){ 


    try{ 
     Class.forName("sun.jdbc.odbc.JdbcOdbcDriver"); 
     Connection con=DriverManager.getConnection("jdbc.odbc:acescon"); 

     String query= "Select price from service table where service="+service_name; 
     Statement stmt = con.createStatement(); 

     ResultSet rs = stmt.executeQuery(query); 

     String price_value=rs.getString("price"); 

     total_tf.setText(price_value); 
    } 
    catch(SQLException | ClassNotFoundException e){ 

    } 


} 

虽然它不显示任何错误。

分贝含有3-列 1-ID -2-服务 3-价格

+0

我使用while循环也试过。而(rs.next( )){String price_value = rs.getString(“price”);} – rvsingh42

+0

It如果你在catch块或者System.out.println(e.getMessage())中做'e.printStackTrace();'会出现错误;' –

回答

0

对不起,不回答早。 我试图做这样的事情:

我与服务,并与在员工COMBOBOX ANOTHER组合框,当我点击 添加按钮,它应该得到选择的值从服务表从数据库价格与添加新的行到下面的表。 因此,这里是最后的代码,我创建,这是很好WORKING SINCE服务的数量小于100则不会创建任何性能问题,

* 但我真的很感激,如果任何人提出一个更好的主意要做到这一点* 这里是工作的代码“

String service_name; 
    String price_value = null; 
    try{ 
     service_name=(String)jComboBox1.getSelectedItem(); //GET SELECTED SERVICE 
     String query= "Select * from services where service_name='"+service_name+"'"; 

     stmt = con.createStatement(); 

     ResultSet rs = stmt.executeQuery(query); 

     while(rs.next()) 
     { 
      if(rs.getString("service_name").equalsIgnoreCase(service_name)) //GET PRICE OF SERVICE 
       price_value=rs.getString("Price"); 


     } 
     //get employee name 
     String emp_name=(String)jComboBox2.getSelectedItem(); 
      if (emp_name.equals("No one")) { 
       emp_name=""; 
      } 

      // insert the new row 

     int row_count = jTable1.getRowCount(); 
     Object[] row_data={service_name,price_value,emp_name}; 
     dtm.insertRow(row_count, row_data); 



    } 
    catch(SQLException e){ 

     javax.swing.JOptionPane.showMessageDialog(this, 
       "ERROR IN ADDING SERVICE TO BILL\n"+e.toString(),"ERROR MESSAGE", 
       javax.swing.JOptionPane.WARNING_MESSAGE); 

    } 

下面的代码是在添加按钮的actionPerformed方法

0

连接CON =的DriverManager.getConnection( “jdbc.odbc:acescon”);

代替上述语句中的jdbc.odbc:acescon它应该是jdbc:odbc:acescon。

顺便说一句,连接不应该在每次点击一个按钮时创建。相反,您可以使用相同的连接或连接池来获得性能。

+0

oops!我是jdbc毕竟新手。我只是为我的最终项目创建可行的模块,所以我确实需要那个连接。 – rvsingh42

+0

请看看上面的代码。 – rvsingh42

+0

我在课程开始时创建了连接,然后我在许多其他课程中使用它,并在上述答案中也请建议我是否有更好的方法来执行此操作。我将在整个应用程序中多次需要连接。 – rvsingh42

0
String query= "Select price from service table where service="+service_name; 

这应该是

String query= "Select price from service table where service="+"'"+service_name+"'"; 

rs.next(); 

String price_value=rs.getString("price"); 
+0

你的代码是正确的。我错误地认为应该是'可以服务'而不是服务台的名称。但是因为我有其他一些错误,我没有得到结果 – rvsingh42