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

