File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes JDBC and the fly likes Can't insert Data Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Databases » JDBC
Bookmark "Can Watch "Can New topic
Author

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();

DataBase()
{


try
{
// Load the Driver(registers itself)
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
System.out.println("Driver loaded");
}


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

try
{

// 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);
}

try
{
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] +");";
System.out.println(sql);
stmt.executeUpdate(sql);
stmt.close();
con.close();

}

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.

Jamie
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Can't insert Data