- Open the last NetBeans project
- Add new jFrame Form(I name it as table)
- Design it as following
- I added 3 Text Fields and 3 Labels
- Change label's names as ID, Name, Adress
- Change text field's variable name as follow
- Import the following libraries
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import javax.swing.JOptionPane;
- Defind the following variables in the 'table' class
(My jFrame name is table)
Connection conn=null;
ResultSet rs=null;
PreparedStatement pst=null;
- After the defin variable there is a method named "table()"
- Add the following code there
conn=dbcon.Connect
- Here,
- dbcon is we made datasabe connection class name
If you don't know about it please follow this link
- Double click on the "Add" button and add the codes below
try{
String sql="insert into empdata(ID,Name,Adress)values(?,?,?)";
pst=conn.prepareStatement(sql);
pst.setString(1, txtID.getText());
pst.setString(2, txtName.getText());
pst.setString(3, txtAdress.getText());
pst.execute();
JOptionPane.showMessageDialog(null, "saved");
}
catch(Exception e){JOptionPane.showMessageDialog(null, e);}
- Now run your project
- In the running time if you got error like this,
- go to 'service'
- Right click on Java DB
- Click 'Start Server'
- Now run the project again
Impotent things
- Add your own data fields
This programm consits only the basic
- To add this data to a database you must have this data fields in your database
If you don't know to make a database, please follow this link
No comments:
Post a Comment