Ask Experts Questions for FREE Help !
Ask
    vedantvyas's Avatar
    vedantvyas Posts: 2, Reputation: 1
    New Member
     
    #1

    Jun 11, 2013, 10:32 AM
    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");
    }

Check out some similar questions!

Java Program- Help Needed [ 0 Answers ]

How do I write this code? You will ask the user for 2 inputs, the total number of chocolate, and the amount of chocolate that goes into the making of the large bar. You must be able to accept decimal point values for both. The small bar value is fixed at 0.5. To solve this program, we want you...

Java Program [ 2 Answers ]

1) Code a java class for an Object SONG, that has as a minimum the following attributes:- SONG Title (a String) , SONG Group/artist (a String ), SONG genre (a String – “Pop”, “Jazz”, “Hard Rock” etc. SONG price (double), SONG duration (int number of seconds). Code all accessor and mutator methods...

Java program? [ 2 Answers ]

we have an assignment! but hold up! I would only ask what is the name of this code if I have fill up the blanks! code: puclic class problim{ public static void main(Stringarg){ int x=___; int y=___; while;(__<s){ x=____;

Java program error [ 1 Answers ]

Sir, program is for managing account details using jdbc but ,there error like some variable not accessible in specefic block . Please solve this problem. If you can.. //mypanel1-LogIn class mypanel1 extends JPanel { JTextField t1,t2; JButton b1,b2; mypanel1() { JLabel l1,l2; ...

Connection from Java to MySql [ 1 Answers ]

Hi friends, Does anybody know any link about connection from Java to Mysql? I will appreciate it ! Kind Regards, Naim


View more questions Search
 

Question Tools Search this Question
Search this Question:

Advanced Search

Add your answer here.