2 Replies - 3730 Views - Last Post: 19 June 2013 - 09:18 AM Rate Topic: -----

#1 MJ007  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 17-September 10

jdbc odbc and ms access issue ?

Posted 19 June 2013 - 02:06 AM

Iwant to store a record but don't know what to do. When i start entering the details of a customer, at that time database connectivity is successfully created but I fail to store the data in the database.

The procedure is correct to create the database but I can't enter the details, what can I do?

Here is the code:
import java.awt.Container;
  import java.awt.event.ActionEvent;
  import java.awt.event.ActionListener;
  import java.sql.*;
  import javax.swing.*;
  //package p1;
  public abstract class New_Customer extends JFrame implements ActionListener
  {
   JTextField textFieldId;
   JTextField textFieldName;
   JTextField textFieldContactNo;
   JLabel l1;
   JLabel l2;
   JLabel l3;
   JLabel l4;
   JLabel l5;
JLabel l6;
    JComboBox combo;
    String course[] = {"Navakal","SandhyaKal","Pudhari","MidDay","Inqlab","BusinessLine","Mumbai Samachar","GujrajSamachar","KarnatakMalla","Vartahar","PunyaNagari"};
JButton b1;
JButton b2;
Container c = getContentPane();
New_Customer()
{
    super("Shree DattaDigambar Samarth");
    setBounds(140,250,777,555);
    c.setLayout(null);
    textFieldId = new JTextField();
    textFieldName = new JTextField();
    textFieldContactNo = new JTextField();
    l1 = new JLabel("New Customer Entry");
    l2 = new JLabel("Building No");
    l3 = new JLabel("Customer Name");
    l4 = new JLabel("Contact No");
    l5 = new JLabel("Paper");
            combo = new JComboBox(course);
    l1.setBounds(10,10,340,20);
    l2.setBounds(10,20,140,70);
    l3.setBounds(110,20,140,70);
    l4.setBounds(300,50,140,20);
    l5.setBounds(400,50,140,20);        
    textFieldId.setBounds(10,70,70,20);
    textFieldName.setBounds(110,70,180,20);
    textFieldContactNo.setBounds(300,70,90,20);
            combo.setBounds(400,70,130,20);   
    b1 = new JButton("Add paper");
            b2 = new JButton("Ok");
    b1.setBounds(10,100,100,20);
    b2.setBounds(10,160,50,20);      
            c.add(combo); 
    c.add(b1);
    c.add(b2);
    c.add(l1);
    c.add(l2);
    c.add(l3);
    c.add(l4);
    c.add(l5);
    c.add(textFieldId);
    c.add(textFieldName);
    c.add(textFieldContactNo);
            setVisible(true);
    setDefaultCloseOperation(EXIT_ON_CLOSE);
            b1.addActionListener(this);
            b2.addActionListener(this);
}
    public static void main(String[] args) 
    {
        New_Customer nc=new New_Customer() {};
    }
     public void actionPerformed(ActionEvent e)
        {

            System.out.println("You clicked the button");
            if(e.getSource()==b1)
            {

            }
            if(e.getSource()==b2)
            {
                try 
                {
                    Connection con;
                    Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
                    con = DriverManager.getConnection("jdbc:odbc:devendra");
                    try 
                    {

                        java.sql.Statement st = con.createStatement();
                        PreparedStatement ps = con.prepareStatement(null);
                        ResultSet rs = ps.executeQuery("insert into Customer values(?,?,?,?)");
                        while(rs.next())
                        {
                        ps.setString(1,textFieldId.getText());
                        ps.setString(2,textFieldName.getText());
                        ps.setString(3,textFieldContactNo.getText());
                        ps.setString(4,combo.getName());
                        }

                    } 
                    catch (SQLException s) 
                    {
                        System.out.println("SQL code does not execute.");
                    }
                } 
                catch (ClassNotFoundException | SQLException ee) 
                {
                    System.out.println("Error:connection not created");
                }
            }
        }
}

This post has been edited by modi123_1: 19 June 2013 - 06:39 AM
Reason for edit:: highlight the text and just click the 'code' button in the text editor


Is This A Good Question/Topic? 0
  • +

Replies To: jdbc odbc and ms access issue ?

#2 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10778
  • View blog
  • Posts: 40,134
  • Joined: 27-December 08

Re: jdbc odbc and ms access issue ?

Posted 19 June 2013 - 08:38 AM

Moved to Java.

First, don't ignore your Exceptions. Use the printStackTrace() method on them to see where you are going wrong.

Second, please adopt a legible indentation convention. Your code is a bit hard to read.
Was This Post Helpful? 0
  • +
  • -

#3 g00se  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2815
  • View blog
  • Posts: 11,915
  • Joined: 20-September 08

Re: jdbc odbc and ms access issue ?

Posted 19 June 2013 - 09:18 AM

Quote

When i start entering the details of a customer, at that time database connectivity is successfully created
Not with that code it isn't. That code wouldn't even compile. Please post the code you're actually using, otherwise you're just wasting everyone's time
Apologies - i was misleading myself. There's not seemingly good reason for the abstract class btw

Quote

 ResultSet rs = ps.executeQuery("insert into Customer values(?,?,?,?)");
should be

                    PreparedStatement ps = con.prepareStatement("insert into Customer values(?,?,?,?)");
                    ps.executeUpdate();



And then you need to close all db resources

This post has been edited by g00se: 19 June 2013 - 09:30 AM
Reason for edit:: Clarification

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1