this is the portion of the code that is responsible for inserting contents into the database....but it doesn't work when i click on it....delete,search and exit and update are working.....please help
if(e.getSource()==btn)
{
try
{
st.executeUpdate("Insert into ant values('"+t.getText()+"','"+t1.getText()+"')");
JOptionPane.showMessageDialog(f,"Data Inserted");
}
JAVA database connectivity
Started by Ananta2010, Jul 03 2010 05:44 AM
3 replies to this topic
#1
Posted 03 July 2010 - 05:44 AM
|
|
|
#2
Posted 03 July 2010 - 05:47 AM
What error message are you getting from the database? You also might want to specify which fields you are populating, as your current statement will fail if ant contains more than two fields.
st.executeUpdate("Insert into ant (field1, field2) values('"+t.getText()+"','"+t1.getText()+"')");
st.executeUpdate("Insert into ant (field1, field2) values('"+t.getText()+"','"+t1.getText()+"')");
#3
Posted 03 July 2010 - 06:18 AM
i am not getting any kind of error message but when i click on my insert button to insert the two fields,there is no information dialog box displayed nor is the data inserted in the database(Access 2007).the code compiles successfully...i produce the entire code below....
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.sql.*;
class DemoOfDatabaseConnectivity implements ActionListener
{
Connection con;
Statement st;
ResultSet rs;
JFrame f=new JFrame("Demo Of Database Connectivity");
JPanel p=new JPanel();
JLabel l=new JLabel("User ID");
JLabel l1=new JLabel("Password");
JTextField t=new JTextField(20);
JPasswordField t1=new JPasswordField(20);
JButton btn=new JButton("INSERT");
JButton btn1=new JButton("DELETE");
JButton btn2=new JButton("UPDATE");
JButton btn3=new JButton("SELECT");
JButton btn4=new JButton("EXIT");
DemoOfDatabaseConnectivity()
{
f.getContentPane().add(p);
p.setLayout(null);
l.setBounds(50,50,100,20);
l1.setBounds(50,90,100,20);
t.setBounds(170,50,100,20);
t1.setBounds(170,90,100,20);
btn.setBounds(20,140,100,20);
btn1.setBounds(130,140,100,20);
btn2.setBounds(240,140,100,20);
btn3.setBounds(50,180,100,20);
btn4.setBounds(160,180,100,20);
p.add(l);
p.add(l1);
p.add(t);
p.add(t1);
p.add(btn);
p.add(btn1);
p.add(btn2);
p.add(btn3);
p.add(btn4);
f.setSize(400,300);
f.show();
btn.addActionListener(this);
btn1.addActionListener(this);
btn2.addActionListener(this);
btn3.addActionListener(this);
btn4.addActionListener(this);
}
public void actionPerformed(ActionEvent e)
{
try
{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
con=DriverManager.getConnection("jdbc:odbc:mydsn");
st=con.createStatement();
}
catch(Exception e1){}
if(e.getSource()==btn)
{
try
{
st.executeUpdate("Insert into ant (UserId,Password) values ('"+t.getText()+"','"+t1.getText()+"')");
JOptionPane.showMessageDialog(f,"Data Inserted");
}
catch(Exception e1){}
}
else if(e.getSource()==btn1)
{
try
{
st.executeUpdate("Delete from ant where UserID='"+t.getText()+"'");
JOptionPane.showMessageDialog(f,"DataDeleted");
}
catch(Exception e1){}
}
else if(e.getSource()==btn2)
{
try
{
st.executeUpdate("Update ant set Password='"+t1.getText()+"' where UserID='"+t.getText()+"'");
JOptionPane.showMessageDialog(f,"DataUpdated");
}
catch(Exception e1){}
}
else if(e.getSource()==btn3)
{
int a=0;
try
{
rs=st.executeQuery("Select * from ant");
while(rs.next())
{
System.out.println("1");
if(rs.getString(1).equals(t.getText()))
{
t1.setText(rs.getString(2));
a=1;
break;
}
}
if(a==0)
JOptionPane.showMessageDialog(f,"NotFound");
}
catch(Exception e1){System.out.println(e);}
}
else
{
System.exit(0);
}
}
public static void main(String [] args)
{
new DemoOfDatabaseConnectivity();
}
}
Edited by WingedPanther, 03 July 2010 - 07:29 AM.
add code tags (the # button)
#4
Posted 03 July 2010 - 07:30 AM
You're catching the exception, and then not displaying it. The result is you DO have an error, but are not displaying it to assist you with trouble-shooting.


Sign In
Create Account

Back to top









