Derby Database :
It is a light weight database comes along with java jdk.
It has two versions:
- Stand alone : It will be access only in the application which is using it . It will be embedded in to the application and run in the JVM.
- Client/Server : It can access from outside as a server running separately. Multiple user can access it.
We are you to take the first One . embedded one . We are going to create the following table in DB from our java code and then we will insert data by using swing and displayed to the user in the textarea using swing components.
Table : data
create table data(name varchar(40), id varchar(40), age varchar(40), place varchar(40) )
For accessing Derby Embedded we need two jars:
- derby.jar
- derbytools.jar
This can be obtained from the lib folder of jdk installation directory.where you can find a folder called Sun it can be exists in the installation directory or in Program files .which have a folder called JavaDB/lib.From here you can obtain these jars and add these to your class path.
Code :
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import javax.swing.JOptionPane;
public class Home extends javax.swing.JFrame {
/* Driver for Embedded Derby Database */
String driver="org.apache.derby.jdbc.EmbeddedDriver";
/*Create Database with name derbyDB */
String connectionURL="jdbc:derby:derbyDB;create=true";
Connection c=null;
PreparedStatement preparedStatement=null;
public Home() {
initComponents();
setConnections();
setSize(800,500);
}
public void insert()
{
try
{
preparedStatement=c.prepareStatement("insert into data values(?,?,?,?)");
preparedStatement.setObject(1, jTextField1.getText());
preparedStatement.setObject(2, jTextField2.getText());
preparedStatement.setObject(3, jTextField3.getText());
preparedStatement.setObject(4, jTextField4.getText());
preparedStatement.executeUpdate();
preparedStatement=c.prepareStatement("select * from data");
ResultSet r= preparedStatement.executeQuery();
while(r.next())
{
jTextArea1.append("\nName:"+" "+r.getString(1)+"\n"+"Id:"+ " "+r.getString(2)+"\n"+"Age:"+" "+r.getString(3)+"\n"+"Place:"+" "+r.getString(4)+"\n"+"\n");
}
}catch(Exception e){
JOptionPane.showMessageDialog(this, e.getMessage());
}
}
public void setConnections()
{
try
{
Class.forName(driver).newInstance();
c= DriverManager.getConnection(connectionURL);
preparedStatement=c.prepareStatement("create table data(name varchar(40), id varchar(40), age varchar(40), place varchar(40) )");
preparedStatement.execute();
}
catch(Exception e){
JOptionPane.showMessageDialog(this, e.getMessage());
}
}
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {
jLabel1 = new javax.swing.JLabel();
jLabel2 = new javax.swing.JLabel();
jLabel3 = new javax.swing.JLabel();
jLabel4 = new javax.swing.JLabel();
jTextField1 = new javax.swing.JTextField();
jTextField2 = new javax.swing.JTextField();
jTextField3 = new javax.swing.JTextField();
jTextField4 = new javax.swing.JTextField();
jButton1 = new javax.swing.JButton();
jScrollPane1 = new javax.swing.JScrollPane();
jTextArea1 = new javax.swing.JTextArea();
jLabel5 = new javax.swing.JLabel();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
getContentPane().setLayout(null);
jLabel1.setText("Name :");
getContentPane().add(jLabel1);
jLabel1.setBounds(30, 50, 80, 20);
jLabel2.setText("Id :");
getContentPane().add(jLabel2);
jLabel2.setBounds(30, 80, 60, 14);
jLabel3.setText("Age :");
getContentPane().add(jLabel3);
jLabel3.setBounds(26, 110, 50, 14);
jLabel4.setText("Place :");
getContentPane().add(jLabel4);
jLabel4.setBounds(20, 144, 60, 10);
jTextField1.setText(" ");
getContentPane().add(jTextField1);
jTextField1.setBounds(110, 50, 180, 20);
jTextField2.setText(" ");
getContentPane().add(jTextField2);
jTextField2.setBounds(110, 80, 180, 20);
jTextField3.setText(" ");
getContentPane().add(jTextField3);
jTextField3.setBounds(110, 110, 180, 20);
jTextField4.setText(" ");
getContentPane().add(jTextField4);
jTextField4.setBounds(110, 140, 180, 20);
jButton1.setText("Add");
String connectionURL="jdbc:derby:derbyDB;create=true";
Connection c=null;
PreparedStatement preparedStatement=null;
public Home() {
initComponents();
setConnections();
setSize(800,500);
}
public void insert()
{
try
{
preparedStatement=c.prepareStatement("insert into data values(?,?,?,?)");
preparedStatement.setObject(1, jTextField1.getText());
preparedStatement.setObject(2, jTextField2.getText());
preparedStatement.setObject(3, jTextField3.getText());
preparedStatement.setObject(4, jTextField4.getText());
preparedStatement.executeUpdate();
preparedStatement=c.prepareStatement("select * from data");
ResultSet r= preparedStatement.executeQuery();
while(r.next())
{
jTextArea1.append("\nName:"+" "+r.getString(1)+"\n"+"Id:"+ " "+r.getString(2)+"\n"+"Age:"+" "+r.getString(3)+"\n"+"Place:"+" "+r.getString(4)+"\n"+"\n");
}
}catch(Exception e){
JOptionPane.showMessageDialog(this, e.getMessage());
}
}
public void setConnections()
{
try
{
Class.forName(driver).newInstance();
c= DriverManager.getConnection(connectionURL);
preparedStatement=c.prepareStatement("create table data(name varchar(40), id varchar(40), age varchar(40), place varchar(40) )");
preparedStatement.execute();
}
catch(Exception e){
JOptionPane.showMessageDialog(this, e.getMessage());
}
}
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {
jLabel1 = new javax.swing.JLabel();
jLabel2 = new javax.swing.JLabel();
jLabel3 = new javax.swing.JLabel();
jLabel4 = new javax.swing.JLabel();
jTextField1 = new javax.swing.JTextField();
jTextField2 = new javax.swing.JTextField();
jTextField3 = new javax.swing.JTextField();
jTextField4 = new javax.swing.JTextField();
jButton1 = new javax.swing.JButton();
jScrollPane1 = new javax.swing.JScrollPane();
jTextArea1 = new javax.swing.JTextArea();
jLabel5 = new javax.swing.JLabel();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
getContentPane().setLayout(null);
jLabel1.setText("Name :");
getContentPane().add(jLabel1);
jLabel1.setBounds(30, 50, 80, 20);
jLabel2.setText("Id :");
getContentPane().add(jLabel2);
jLabel2.setBounds(30, 80, 60, 14);
jLabel3.setText("Age :");
getContentPane().add(jLabel3);
jLabel3.setBounds(26, 110, 50, 14);
jLabel4.setText("Place :");
getContentPane().add(jLabel4);
jLabel4.setBounds(20, 144, 60, 10);
jTextField1.setText(" ");
getContentPane().add(jTextField1);
jTextField1.setBounds(110, 50, 180, 20);
jTextField2.setText(" ");
getContentPane().add(jTextField2);
jTextField2.setBounds(110, 80, 180, 20);
jTextField3.setText(" ");
getContentPane().add(jTextField3);
jTextField3.setBounds(110, 110, 180, 20);
jTextField4.setText(" ");
getContentPane().add(jTextField4);
jTextField4.setBounds(110, 140, 180, 20);
jButton1.setText("Add");
/* Whenever button is pressed this action happens */
jButton1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton1ActionPerformed(evt);
}
});
getContentPane().add(jButton1);
jButton1.setBounds(90, 200, 170, 23);
jTextArea1.setColumns(20);
jTextArea1.setRows(5);
jScrollPane1.setViewportView(jTextArea1);
getContentPane().add(jScrollPane1);
jScrollPane1.setBounds(340, 50, 190, 210);
jLabel5.setText("Values from Database :");
getContentPane().add(jLabel5);
jLabel5.setBounds(340, 30, 140, 14);
pack();
}
// call insert on clicking of Add button
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
insert();
}
public static void main(String args[]) {
new Home().setVisible(true);
}
// Variables declaration - do not modify
private javax.swing.JButton jButton1;
private javax.swing.JLabel jLabel1;
private javax.swing.JLabel jLabel2;
private javax.swing.JLabel jLabel3;
private javax.swing.JLabel jLabel4;
private javax.swing.JLabel jLabel5;
private javax.swing.JScrollPane jScrollPane1;
private javax.swing.JTextArea jTextArea1;
private javax.swing.JTextField jTextField1;
private javax.swing.JTextField jTextField2;
private javax.swing.JTextField jTextField3;
private javax.swing.JTextField jTextField4;
// End of variables declaration
}
Output:
------------------------------------------------
blogs
May I know where does the data persist?
ReplyDeleteMay I know where does the data persist?
ReplyDeleteIn the same working directory of the application.
Deleteit's not working buddy ...
Deleteafter placed all the values when select to add it's shows messege ok but the values are not displayed in that frame ...
Deletewating for ur reply ... thanks
DeleteWhat's the Exception you are getting...
DeleteGreat Article
ReplyDeleteJava Web Services Online Training | Web Services Course | Web Services Training Courses | Java Web Services Training in Chennai | Online Java Training | Java Training in Chennai