In the last session we learned about how to connect to mysql database using Netbeans.
Now we will learn how to display contents of database in a jTable and insert value in Database.
You need to download a jar file to display database values in jTable
https://docs.google.com/uc?authuser=0&id=0BzIr4IDDKJEcdDE1YTlzbmtkMzg&export=download
2.Expand your database and then click new.
3.Name the table basic and select the first column and add values name - id, type - int, length - 255, A_I - checked.
In the second column set name - fname, type - varchar, length - 50.
In the third column set name - lname, type - varchar, length - 50.
4.Click Save
jar/file -> select your file ->click ok.
2.Insert a jTable in your frame and change the variable name to tab.
3.Go to the source tab and above the class name import
import javax.swing.table.DefaultTableModel;
import net.proteanit.sql.DbUtils;
4.Create a function named tab inside the class parenthesis.
public void tab(){
try{
String sql = "select id,fname,lname from basic";
pst = conn.prepareStatement(sql);
rs=pst.executeQuery();
tab.setModel(DbUtils.resultSetToTableModel(rs));
}catch(Exception ex){
JOptionPane.showMessageDialog(rootPane,ex);
}
}
5.After init components add this line : conn = Data.ConnectDb(); and tab();
6.Run the file.
is jTextField2 and last name is jTextField3.
5.Run the file and test.
Now we will learn how to display contents of database in a jTable and insert value in Database.
You need to download a jar file to display database values in jTable
https://docs.google.com/uc?authuser=0&id=0BzIr4IDDKJEcdDE1YTlzbmtkMzg&export=download
Creating Table
1.Start your mysql database then go to administration->open.2.Expand your database and then click new.
3.Name the table basic and select the first column and add values name - id, type - int, length - 255, A_I - checked.
In the second column set name - fname, type - varchar, length - 50.
In the third column set name - lname, type - varchar, length - 50.
4.Click Save
Displaying contents in jTable
1.After downloading import the jar file into your project.Rclick on your project->libraries->addjar/file -> select your file ->click ok.
2.Insert a jTable in your frame and change the variable name to tab.
3.Go to the source tab and above the class name import
import javax.swing.table.DefaultTableModel;
import net.proteanit.sql.DbUtils;
4.Create a function named tab inside the class parenthesis.
public void tab(){
try{
String sql = "select id,fname,lname from basic";
pst = conn.prepareStatement(sql);
rs=pst.executeQuery();
tab.setModel(DbUtils.resultSetToTableModel(rs));
}catch(Exception ex){
JOptionPane.showMessageDialog(rootPane,ex);
}
}
5.After init components add this line : conn = Data.ConnectDb(); and tab();
6.Run the file.
Inserting values in database
1.You need to insert three text field's.
3.Double click the button and type the code :
String sql = "Insert into basic (id,fname,lname) values (?,?,?)";
try{
pst = conn.prepareStatement(sql);
pst.setString(1,jTextField1.getText());
pst.setString(2,jTextField2.getText());
pst.setString(3,jTextField3.getText());
pst.execute();
tab();
JOptionPane.showMessageDialog(null,"Inserted Successfully!");
}catch(Exception ex)
{
JOptionPane.showMessageDialog(null,ex);
}
4.You must make sure that the text field in which you are going to type your id is jTextField1, nameis jTextField2 and last name is jTextField3.
5.Run the file and test.
No comments:
Post a Comment