[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 248
  • Last Modified:

First SQl string will not run

I have just started to work with databases and i am having trouble inputing data.  For some reason when i run one INSERT statement it does not populate the table.  However, when i run a second different INSERT statement the first one then does take effect.  Below is my code, can someone explain to me what i am doing wrong...thanks.

-----------------------
package ies;

import java.sql.*;

public class ConnectToDB {

    public static Statement connectToDB(){

      // Load the driver
      try
      {
        // Load the driver class
        Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");

        // Define the data source for the driver
        String sourceURL = "jdbc:odbc:iesDataStore";

        // Create a connection through the DriverManager
        Connection databaseConnection =
                                  DriverManager.getConnection( sourceURL );

        statement = databaseConnection.createStatement();
      }
      catch(ClassNotFoundException cnfe)
      {
        System.err.println(cnfe);
      }
      catch(SQLException sqle)
      {
        System.err.println(sqle);
      }
      return statement;
    }

    public static void main( String[] args ){
      Statement poo = ConnectToDB.connectToDB();

      try{
        poo.execute( "INSERT INTO Estudiante (apellidos, nombre) VALUES ('goo', 'foo')" );
        poo.execute( "INSERT INTO Asignaturas (codigo, titulo, horario) VALUES ('1', '2', '3')" );
      }
      catch( SQLException sqle ){}
    }

    static Statement statement;
}
0
menreeq
Asked:
menreeq
1 Solution
 
Venci75Commented:
Try to call commit() method of your database connection after you have executed the INSERT statement
0
 
menreeqAuthor Commented:
Its a access database, so do you know what it is i would have to do?
0
 
pellepCommented:
try
poo.executeUpdate
instead of
poo.execute
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
fundoosathishCommented:
since its an insert statement try out

poo.executeQuery.

0
 
shyamkumarreddyCommented:
Yeah Men
Use executeQuery to insert into database.
Shyam
0
 
menreeqAuthor Commented:
Don´t understand why i would run executeQuery when inserting into the database but i tried it and it did now work, even when i repeated the line twice.  I also tried to run executeUpdate with no success.  Any other ideas?
0
 
shyamkumarreddyCommented:
MenReeg
You question is a good question. According to the documentation it should be possible to insert using execute/executeQuery/executeUpdate.
I pratically feel it should be driver incapability to handle. Let me check it with other databases also becoming confirming it. I just referred and even literally now i feel it is good question.

Shyam
0
 
menreeqAuthor Commented:
Don´t understand why i would run executeQuery when inserting into the database but i tried it and it did now work, even when i repeated the line twice.  I also tried to run executeUpdate with no success.  Any other ideas?
0
 
menreeqAuthor Commented:
I just added a (;) to the end of the first sql string and it now executes and commits.

So the line reads
poo.execute( "INSERT INTO Estudiante (apellidos, nombre) VALUES ('goo', 'foo');" );

0
 
shyamkumarreddyCommented:
Men
Great Job done man.
Hey Sorry i remembered this.
use con.commit() after your insert
Or
Use ; after it i think so.
Shyam
0
 
menreeqAuthor Commented:
so is it normal that i end all my sql commands with a semicolon?
0
 
shyamkumarreddyCommented:
You need to commit it man to see the changes
that is off sure.
Anyway autocommit mode is true. Yet u need to do manually commit.
Shyam
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now