• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Can't insert Data

 
Ola Kesh
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1683
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Try inserting a commit.
 
Jamie Robertson
Ranch Hand
Posts: 1879
MySQL Database Suse
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic