jQuery in Action, 3rd edition
The moose likes JDBC and Relational Databases and the fly likes Can't insert Data Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Databases » JDBC and Relational Databases
Bookmark "Can Watch "Can New topic

Can't insert Data

Ola Kesh
Ranch Hand

Joined: Dec 24, 2002
Posts: 40
Hello all,
please i need your help. i have being trying to insert data into my access database but it has not being possible. The driver loaded successfully, likewise the connection but it wo't just execute my query. Posted below is the code:

import java.sql.*;
import javax.swing.JOptionPane;

public class DataBase extends Account{
static String title,name,h_addr,o_addr,mobile,pin,code;

Statement stmt;
Connection con;
ResultSet r;
String query;
static String data[] = new String[7];

Account count = new Account();


// Load the Driver(registers itself)
System.out.println("Driver loaded");

catch(ClassNotFoundException cnfex)
JOptionPane.showMessageDialog(null,"The driver can not be loaded...",
"Driver Error!!!",JOptionPane.ERROR_MESSAGE);


// Connect to the database
con = DriverManager.getConnection("jdbc dbc:AccountDepartment","","");
System.out.println("Connected to database");


catch(SQLException sqlex)
JOptionPane.showMessageDialog(null,"Cannot connect to the database...",
"Connection Error!!!",JOptionPane.ERROR_MESSAGE);

data[0] = title = (count.tit);
data[1] = name = (count.f1.getText());
data[2] = h_addr = (count.a1.getText());
data[3] = o_addr = (count.a2.getText());
data[4] = mobile = (count.f2.getText());
data[5] = pin = (count.f3.getText());
data[6] = code = (count.f4.getText());

stmt = con.createStatement();
String sql = "insert into Account values(" +data
[0] +"," +data[1] +"," +data[2] +"," +data[3] +","
+data[4] +"," +data[5] +"," +data[6] +");";


catch(SQLException sqlexxx)
JOptionPane.showMessageDialog(null,"Cannot insert into the database...",
"Connection Error!!!",JOptionPane.ERROR_MESSAGE);

public static void main(String args[])
DataBase bee = new DataBase();

Roger Chung-Wee
Ranch Hand

Joined: Sep 29, 2002
Posts: 1683
Try inserting a commit.

SCJP 1.4, SCWCD 1.3, SCBCD 1.3
Jamie Robertson
Ranch Hand

Joined: Jul 09, 2001
Posts: 1879

I see a couple of things

1. a semi colon ( is not required to be included in the query string. ( Even though you require it in some sql editors

2. You don't do any special character escaping ( specifically ' )

3. if any of your data[] items are strings/varchar/text in the database, they need to be enclosed in single quotes (').

If those don't solve your problem, let us know what the SQLException message you are getting.

I agree. Here's the link: http://aspose.com/file-tools
subject: Can't insert Data
It's not a secret anymore!