Win a copy of Spark in Action this week in the Open Source Projects forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Bear Bibeault
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Tim Cooke
  • Liutauras Vilda
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • fred rosenberger
  • salvin francis
Bartenders:
  • Piet Souris
  • Frits Walraven
  • Carey Brown

Can't insert Data

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

}
 
Ranch Hand
Posts: 1683
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Try inserting a commit.
 
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
 
What are you doing? You are supposed to be reading this tiny ad!
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
    Bookmark Topic Watch Topic
  • New Topic