Ask Me Help Desk

Ask Me Help Desk (https://www.askmehelpdesk.com/forum.php)
-   Java (https://www.askmehelpdesk.com/forumdisplay.php?f=440)
-   -   Java netbeans program- Connection between java and mysql,How to conn (https://www.askmehelpdesk.com/showthread.php?t=753199)

  • Jun 11, 2013, 10:32 AM
    vedantvyas
    Java netbeans program- Connection between java and mysql,How to conn
    which I wrote from a book but when I run it, it shows exception
    please give me the code to connect jlistbox to database when I click on btn1.
    btn coding should search "name" in database and shows all the names in listbox

    my coding:
    here-- r1,r2,r3 are radio btns and l1,l2,l3 are labels, t1,t2 are textbox, listbox= toylist
    String filter="";
    if(r3.isSelected())
    filter="where name='"+t1.getText()+"'";
    else if (r1.isSelected())
    {
    float priceL= Float.parseFloat(t1.getText());
    float priceF= Float.parseFloat(t2.getText());
    filter="where price>="+priceL+"and price<="+priceF;
    }
    else if (r2.isSelected()){
    int age=Integer.parseInt(t1.getText());
    filter="where lowerlimit<="+age+"and upperlimit>="+age;
    }
    int count=0;
    String query="select name from toys"+filter+";";
    DefaultListModel model=new DefaultListModel();
    try{
    Class.forName("java.sql.Driver");
    Connection con=DriverManager.getConnection("jdbc:my…
    Statement stmt=con.createStatement();
    ResultSet rs= stmt.executeQuery(query);
    while(rs.next()){

    String name=rs.getString("name");
    model.addElement(name);
    //model.add(count,rs.getString("name"));
    count++;
    }
    toylist.setModel(model);
    rs.close();
    stmt.close();
    con.close();
    }
    catch(Exception e){
    JOptionPane.showMessageDialog(this,"Er… In Connectivity");
    }

  • All times are GMT -7. The time now is 01:19 AM.