Solved

First SQl string will not run

Posted on 2002-04-29
12
216 Views
Last Modified: 2010-03-31
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
Comment
Question by:menreeq
12 Comments
 
LVL 9

Expert Comment

by:Venci75
ID: 6977735
Try to call commit() method of your database connection after you have executed the INSERT statement
0
 

Author Comment

by:menreeq
ID: 6977775
Its a access database, so do you know what it is i would have to do?
0
 
LVL 4

Expert Comment

by:pellep
ID: 6978216
try
poo.executeUpdate
instead of
poo.execute
0
ScreenConnect 6.0 Free Trial

At ScreenConnect, partner feedback doesn't fall on deaf ears. We collected partner suggestions off of their virtual wish list and transformed them into one game-changing release: ScreenConnect 6.0. Explore all of the extras and enhancements for yourself!

 

Expert Comment

by:fundoosathish
ID: 6979720
since its an insert statement try out

poo.executeQuery.

0
 
LVL 3

Expert Comment

by:shyamkumarreddy
ID: 6979892
Yeah Men
Use executeQuery to insert into database.
Shyam
0
 

Author Comment

by:menreeq
ID: 6980154
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
 
LVL 3

Expert Comment

by:shyamkumarreddy
ID: 6980172
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
 

Author Comment

by:menreeq
ID: 6980192
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
 

Author Comment

by:menreeq
ID: 6980241
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
 
LVL 3

Expert Comment

by:shyamkumarreddy
ID: 6980253
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
 

Author Comment

by:menreeq
ID: 6980274
so is it normal that i end all my sql commands with a semicolon?
0
 
LVL 3

Accepted Solution

by:
shyamkumarreddy earned 50 total points
ID: 6980284
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

What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
how do i compare an object based on two fields 6 54
tomcat administrtor 12 47
hibernate example for saving data 19 39
Java: anonymous class 4 19
An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
Viewers learn about the “while” loop and how to utilize it correctly in Java. Additionally, viewers begin exploring how to include conditional statements within a while loop and avoid an endless loop. Define While Loop: Basic Example: Explanatio…
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…

773 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question