Insert Problem jdbc odbc

I have problem with my Insertion code. here is my code:-

import java.awt.*;

import java.awt.event.*;

import javax.swing.*;

import java.sql.*;

import java.lang.Class;

import java.util.*;

public class BookForm extends JFrame implements ActionListener

{

//attributes

private JTextField name, address, ICno;

private JButton cancel, book;

private JLabel lb1,lb2, lb3, lb4;

private Container c;

private Connection con;

private ResultSet rs;

private Statement stmt;

private String looks;

private String Busno, $seatno, $date, pric;

public BookForm(String $Busno, String seatno, String date, String $price)

{

super("Seat No. "+seatno);

c = getContentPane();

c.setLayout(null);

c.setSize(260,260);

Busno = $Busno;

$seatno = seatno;

$date = date;

pric = $price;

// ********** SET UP GUI **********

try{

looks = UIManager.getSystemLookAndFeelClassName();

UIManager.setLookAndFeel(looks);

SwingUtilities.updateComponentTreeUI(this);

}//end of try

catch(Exception e)

{

System.out.println("Error in UIManager");

}

lb1 = new JLabel("Name");

lb1.setSize(60,20);

lb1.setLocation(30,30);

lb2 = new JLabel("Address");

lb2.setSize(60,20);

lb2.setLocation(30,60);

lb3 = new JLabel("IC No.");

lb3.setSize(60,20);

lb3.setLocation(30,90);

lb4 = new JLabel("Seat No.: "+$seatno);

lb4.setSize(100,20);

lb4.setLocation(30,120);

name = new JTextField();

name.setSize(150,20);

name.setLocation(100,30);

address = new JTextField();

address.setSize(150,20);

address.setLocation(100,60);

ICno = new JTextField();

ICno.setSize(150,20);

ICno.setLocation(100,90);

cancel = new JButton("Cancel");

cancel.setSize(70,30);

cancel.setLocation(30,190);

cancel.setMnemonic('L');

book = new JButton("Print & Book");

book.setSize(100,30);

book.setLocation(140,190);

book.setMnemonic('R');

c.add(lb1);

c.add(lb2);

c.add(lb3);

c.add(lb4);

c.add(name);

c.add(address);

c.add(ICno);

c.add(cancel);

c.add(book);

cancel.addActionListener(this);

book.addActionListener(this);

}//end o BookForm()

public void actionPerformed(ActionEvent e)

{

Object source = e.getSource();

if(source == book)

{

String nam = name.getText();

String add = address.getText();

String ic = ICno.getText();

String busn = Busno;

String seatn = $seatno;

String dat = $date;

boolean validation = validationFunction();

if(validation == false)

JOptionPane.showMessageDialog(null,"Please Fill the Form Completely");

else{

try{

Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");

con = DriverManager.getConnection("jdbc:odbc:Bus","","");

stmt=con.createStatement();

stmt.executeUpdate("Insert Into PassengerBooking (Name,Address,ICno,BusNo,SeatNo,Date) VALUES ('"+nam+"' , '"+add+"', '"+ic+"', '"+Busno+"', '"+$seatno+"', '"+$date+"')");

stmt.close();

con.close();

}

catch(SQLException exc)

{

exc.printStackTrace();

}

catch(Exception exc)

{

exc.printStackTrace();

}

}

}

else if(source == cancel)

{

this.setVisible(false);

}

}

public boolean validationFunction()

{

boolean check = true;

String nam = name.getText();

String add = address.getText();

String ic = ICno.getText();

if(name.equals(""))

check = false;

else if(add.equals(""))

check = false;

else if(ic.equals(""))

check = false;

return check;

}

}

I get syntax error in my Insert query. my database column name is: Name, Address, ICno, BusNo, SeatNo, Date

and I have an auto number (BookNo) for that table as a primary key.

Since I am a student and new in java, I dont know which code I type wrongly..

Please Help me, This my Assignment which to be submited next week.

Message was edited by:

Triagip

[4328 byte] By [Triagipa] at [2007-11-26 21:23:42]
# 1
Hi Triagip ,Mention the Data source name with the user name and password of the database and the database name or else Set the datasource using the System DSN.con = DriverManager.getConnection("jdbc:odbc:Bus","","");Send me the Error tooCheers
Chellama at 2007-7-10 3:03:19 > top of Java-index,Database Connectivity,Java Database Connectivity (JDBC)...
# 2
take the insert statement first put it in string, then println the string, copy paste in query editor, execute it... check for error there
G_Abubakra at 2007-7-10 3:03:19 > top of Java-index,Database Connectivity,Java Database Connectivity (JDBC)...
# 3

Note that there is a return value involved. I suggest you try the following first.

executeUpdate

public int executeUpdate(String sql)

throws SQLException

Executes the given SQL statement, which may be an INSERT, UPDATE, or

DELETE statement or an SQL statement that returns nothing, such as an

SQL DDL statement.

Parameters:

sql - an SQL INSERT, UPDATE or DELETE statement or an SQL statement

that returns nothing

Returns:

either the row count for INSERT, UPDATE or DELETE statements, or 0 for

SQL statements that return nothing

Throws:

SQLException - if a database access error occurs or the given SQL

statement produces a ResultSet object

- - - -

try {

int returnValue = 0; // Added

Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");

System.out.println("Step 1 complete");

con = DriverManager.getConnection("jdbc:odbc:Bus","","");

System.out.println("Step 2 complete");

stmt=con.createStatement();

System.out.println("Step 3 complete");

returnValue = stmt.executeUpdate("Insert Into PassengerBooking "

+"(Name,Address,ICno,BusNo,SeatNo,Date) "

+"VALUES ('"+nam+"' , '"+add+"', '"+ic+"', '"+Busno+"', '"+$seatno+"', '"+$date+"')");

System.out.println("Inserted ["+returnValue"+"] rows");

stmt.close();

con.close();

}

I absolutely agree with the advise you received in isolating the SQL pblms, by making sure the insert statement works apart from the Java wrapper code.

abillconsla at 2007-7-10 3:03:19 > top of Java-index,Database Connectivity,Java Database Connectivity (JDBC)...