How to search for data on a database through Java GUI. We will be using a list to search for data in a table according to its primary key and showing its details on the text-fields.
JDBC |
import java.sql.*;
try {
Class.forName(“com.mysql.jdbc.Driver”);
Connection con= (Connection) DriverManager.getConnection(“jdbc:mysql://localhost:3306/database1“,”root”,””);
Statement stmt=con.createStatement();
String num = (String) jList1.getSelectedValue();
String sql1 = “Select * from library where no = ‘” + (num) + “‘”;
ResultSet rs = stmt.executeQuery(sql1);
while (rs.next())
{
String no = rs.getString(“NO”);
String title= rs.getString(“TITLE”);
String auth= rs.getString(“AUTHOR”);
jTextField1.setText(“”+no);
jTextField2.setText(“”+title);
jTextField3.setText(“”+auth);
}
}
catch(Exception e) {
JOptionPane.showMessageDialog(this, e.getMessage());
}
7 Change the codes in blue according to your database. Finally, run the file(Shift +F6)
Run View |
Add some labels and uncheck the editable property of text-fields to make the design look better.
After searching the data, you can allow users to update or delete rows.
// watch a video tutorial for java database connectivity on the videos page
@Anonymous(pls write ur name also)
☻check the coding again.
its root and not Root
☻Write your database_name and not 'database1'
☻type the password in the last "" …if u have set in MySQl
Iv Done All of this but i want to use a combo box list instead if just simple list what changes will i have to make, to this code, would really appreciate any feedback.
Just Need to know the changes made on on step 5
Thanks
@khanny
DefaultComboBoxModel model=(DefaultComboBoxModel) jComboBox1.getModel();
——————————–
while(rs.next()){
String no=rs.getString("no");
model.addElement(no);
}
Ok Thanks, Ive managed to do that,
Can you do a tutorial on how to apply a auto-complete function onto a jtextfield , from a row of SQL, if you can, or can you refer me to any links, i would really appreciate any feedback I've been trying for 5 days now no look.
Thanks
@Khanny Sorry! I can't help u this time, I have never tried this in Java and I have no time because of my exams. I will surely look at it later.
@Anonymous for simplicity and for ensuring that user enters the correct date, use 3 text-fields differently for date-month-year.
concat the 3 variables with '-' in between and then pass it through the sql query.
@Anonymous
list.clear();
Add the above code BELOW the line DefaultListModel list=(DefaultListModel) jList1.getModel(); on the Search button.
This comment has been removed by the author.
Hey guys this site is really good. It helped me when doing my project.
hello
I have a problem at the third point.I
ve inserted a list swing control, but how can I create a new list model, I don
t understand? I need urgent help.@nekken100 i don't why u are not able to do it, it's simple just Right-Click your list which u have dropped on the form. Now, go to properties and find the where model is written on the left-side. Click on the model and press ctrl+space, a dialog would open and now from the drop-down menu which is situated at top select custom-code. Finally, paste the code given in the article above.
I am using textfield(to search) instead of list.What should i do?
rohan tried it and it worked but the button's code when clicking it several times it displays alot of the same output
how to display data in database using jlable
can modified the search code to search like %, so upper case or lower case can search
hi i nid help, cant find the code to search a record in mysql using java swing
how to create a login using access 2013 in java netbeans
cant compile, am getting this error.
Updating property file: C:UsersBillionsDocumentsNetBeansProjectsOJDBCbuildbuilt-jar.properties
Compiling 1 source file to C:UsersBillionsDocumentsNetBeansProjectsOJDBCbuildclasses
C:UsersBillionsDocumentsNetBeansProjectsOJDBCsrcoracleconWorkers.java:372: error: cannot find symbol
jList1.setModel(newDefaultListModel());
symbol: method newDefaultListModel()
location: class Workers
Note: C:UsersBillionsDocumentsNetBeansProjectsOJDBCsrcoracleconWorkers.java uses unchecked or unsafe operations.
Note: Recompile with -Xlint:unchecked for details.
1 error
what do i do?