my dog learned polymorphism*
The moose likes Beginning Java and the fly likes connecting java application to JAVADB...database Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Reply locked New topic
Author

connecting java application to JAVADB...database

aditya vikram singh
Greenhorn

Joined: Jan 02, 2012
Posts: 6


guys this is my project and i am not able to connect with database i am using...JAVA DB....PLEASE help me out its realyy urgent...i am not able to save any data to database or update or delete....if any body is having idea about this error tell me how to fix it..and if any error is there please post me with corrected code...please i need help....thank you guys...i have attched the file naming java code ..

package sails.manager;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.*;
import javax.swing.*;



public class SailsManager {
JFrame f;
JPanel p1,p2,p3;
JTabbedPane tp;
ImageIcon btnimg1,btnimg2;
JLabel l1, l2, l3, l4,l5,l6,l7,l8,l9,l10;
JTextField tf1,tf2,tf3,tf4,tf5,tf6,tf7,tf8,tf9,tf10;
JScrollPane sp1;
JButton savebtn,resetbtn,editbtn1,editbtn2,deletebtn ;

SailsManager(){
f=new JFrame("SAILES MANAGER");
p1=new JPanel(new GridLayout(5,3));
p2=new JPanel(new GridLayout(5,3));
p3=new JPanel(new GridLayout(2,3));
tp=new JTabbedPane();
l1=new JLabel("ID:");
l2=new JLabel("NAME:");
l3=new JLabel("CUS_ORDER:");
l4=new JLabel("PRICE:");
l5=new JLabel("Enter Customer ID to delete data:");

l7=new JLabel("ID:");
l8=new JLabel("NAME:");
l9=new JLabel("CUS_ORDER");
l10=new JLabel("PRICE:");
tf1=new JTextField(12);
tf2=new JTextField(12);
tf3=new JTextField(12);
tf4=new JTextField(12);
tf5=new JTextField(12);
tf6=new JTextField(12);
tf7=new JTextField(12);
tf8=new JTextField(12);
tf9=new JTextField(12);
tf10=new JTextField(12);
savebtn=new JButton(" Save ");
resetbtn=new JButton(" Reset");
editbtn1=new JButton(" Edit ");
editbtn2=new JButton(" Save");
deletebtn=new JButton("Delete");
p1.add(l1);
p1.add(tf1);
p1.add(l2);
p1.add(tf2);
p1.add(l3);
p1.add(tf3);
p1.add(l4);
p1.add(tf4);
p1.add(savebtn);
p1.add(resetbtn);

p2.add(l7);
p2.add(tf7);
p2.add(l8);
p2.add(tf8);
p2.add(l9);
p2.add(tf9);
p2.add(l10);
p2.add(tf10);
p2.add(editbtn1);
p2.add(editbtn2);

p3.add(l5);
p3.add(tf5);
p3.add(deletebtn);
resetbtn.addActionListener(new ActionListener() {

public void actionPerformed(ActionEvent ae){
tf1.setText("");
tf2.setText("");
tf3.setText("");
tf4.setText("");
}
});
savebtn.addActionListener(new ActionListener() {

@Override
public void actionPerformed(ActionEvent ae){
String value1=tf1.getText();
String value2=tf2.getText();
String value3=tf3.getText();
String value4=tf4.getText();
Connection con = null;
String url = "jdbc:derby://localhost:1527/SAILS MANAGER";
String db = "SAILSMANAGER";
String driver = "org.apache.derby.jdbc.ClientDriver";
String user = "sailsmanager";
String pass = "sails";
System.out.println(value1+value2+value3+value4);
try{
Class.forName(driver);
con = DriverManager.getConnection(url+db, user, pass);
PreparedStatement st=con.prepareStatement("insert into CUSTOMER(ID,NAME,CUS_ORDER,PRICE" );
st.setString(1,value1);
st.setString(2,value2);
st.setString(3,value3);
st.setString(4,value4);
st.executeUpdate();
JOptionPane.showMessageDialog(p1,"Data is successfully inserted into database.");
con.close();
}
catch(Exception e){
JOptionPane.showMessageDialog(p1,"Error in submitting data!");
}
}
});

deletebtn.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent ae){

String value1=tf5.getText();
Connection con = null;
String url = "jdbc:derby://localhost:1527/SAILS MANAGER";
String db = "SAILSMANAGER";
String driver = "org.apache.derby.jdbc.ClientDriver";
String user = "sailsmanager";
String pass = "sails";
try{
Class.forName(driver);
con = DriverManager.getConnection(url+db, user, pass);
PreparedStatement st=con.prepareStatement("DELETE FROM CUSTOMER WHERE ID = ?");
st.setString(1,value1);
st.executeUpdate();
JOptionPane.showMessageDialog(p3,"Data is deleted successfully.");
con.close();
}
catch(Exception exp3)
{
JOptionPane.showMessageDialog(p3,"Error in deleting data.");
}
}
});
editbtn1.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent ae){

String value=tf7.getText();
Connection con = null;
String url = "jdbc:derby://localhost:1527/SAILS MANAGER";
String db = "SAILSMANAGER";
String driver = "org.apache.derby.jdbc.ClientDriver";
String user = "sailsmanager";
String pass = "sails";
try{
Class.forName(driver);
con = DriverManager.getConnection(url+db, user, pass);
PreparedStatement st=con.prepareStatement("select * from CUSTOMER where ID=?");
st.setString(1,value);
ResultSet res=st.executeQuery();
res.next();
tf7.setText(Integer.toString(res.getInt(1)));
tf8.setText(res.getString(2));
tf9.setText(res.getString(3));
tf10.setText(Integer.toString(res.getInt(4)));
con.close();
}
catch(Exception e)
{
JOptionPane.showMessageDialog(p2,"Can not edit data");
}
}
});
editbtn2.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent ae){
Connection con = null;
String url = "jdbc:derby://localhost:1527/SAILS MANAGER";
String db = "SAILS MANAGER";
String driver = "org.apache.derby.jdbc.ClientDriver";
String user = "sailsmanager";
String pass = "sails";
try
{
int x=JOptionPane.showConfirmDialog(p2,"Confirm edit? All data will be replaced");
if(x==0){
try{
String value1=tf7.getText();
String value2=tf8.getText();
String value3=tf9.getText();
String value4=tf10.getText();

Class.forName(driver);
con = DriverManager.getConnection(url+db, user, pass);
Statement st=con.createStatement();
st.executeUpdate("update CUSTOMER set NAME='"+value2+"', CUS_ORDER='"+value3+"',PRICE=('"+value4+"' where ID='"+value1+"'");
JOptionPane.showMessageDialog(p2,"Updated successfully");
con.close();
}
catch(Exception ex)
{
JOptionPane.showMessageDialog(p2,"Error in updating edit Data");
}
}
}
catch(Exception ex)
{
JOptionPane.showMessageDialog(p2,"Error");
}
}
});
}
void dis()
{
f.getContentPane().add(tp);
tp.addTab("Add Data",p1);
tp.addTab("Edit Data",p2);
tp.addTab("Delete Data",p3);

f.setSize(600,400);
f.setVisible(true);
f.setResizable(true);
}
public static void main(String z[]){
SailsManager pro=new SailsManager();
pro.dis();
}
}


Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38107
    
  22
Welcome to the Ranch

There is no such thing as “urgent” here. Please don’t ask the same question twice; since you posted on our JDBC forum, I shall close this thread. Please go back to your JDBC post and click the edit button and add code tags; then we can actually read the code.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: connecting java application to JAVADB...database
 
Similar Threads
Getting sql error [Microsoft ][odbc microsoft access driver]numeric index out of range
MY swing database application can not connect
Connecting Java Application with a Database
connecting java application to JAVADB database
getText() is not working?