This discussion is archived
4 Replies Latest reply: Oct 9, 2012 9:09 AM by Tolls RSS

java.lang.NullPointerException in JDBC

usman_noshahi Newbie
Currently Being Moderated
hi gurus,

i am using
----------------------------------------------------------------
Oracle Database 10g Enterprise Edition Release 10.2.0.1.0 - Prod
PL/SQL Release 10.2.0.1.0 - Production
CORE    10.2.0.1.0      Production
TNS for 32-bit Windows: Version 10.2.0.1.0 - Production
NLSRTL Version 10.2.0.1.0 - Production

-----------------------------------------------------------------
I am trying to insert a row in oracle table using java. I got this code from a demo..
package jdbc;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.Statement;
public  class insertRecord {

    /**
     * @param args the command line arguments
     */

     
    public static void main(String[] args) {
     
    String driver="sun.jdbc.odbc.JdbcOdbcDriver";
    String cs="jdbc:odbc:connect_oracle";
    String user = "HR";
    String pwd = "HR";
    String sqlstmt="INSERT INTO JTEST VALUES(1,'Steve')";
    Connection con = null;
    Statement st = null;
    try
    {
    Class.forName(driver);
    }
    catch(Exception e)
    {
    System.out.println(e);
    }
    System.out.println("Driver loaded");
    try
    {
    con=DriverManager.getConnection(cs,user,pwd);
    System.out.println("Connected to the Oracle Database");
    st = con.createStatement();//creates a Statement object for sending SQL statements to the database.
    int updatecount=st.executeUpdate(sqlstmt);//return either the row count for INSERT, UPDATE or DELETE statements, or 0 for SQL statements that return nothing
        System.out.println(updatecount+" row inserted");
    }
    catch(Exception e)
    {
    System.out.println(e);
    }
    try
    {
    st.close();
    con.close();
    }
    catch(Exception e)
    {
    System.out.println(e);
    }
    }//main()
    }//cla
when I run this code , it shows me
Driver loaded
java.sql.SQLException: [Microsoft][ODBC Driver Manager] Data source name not found and no default driver specified
java.lang.NullPointerException
I placed CLASS12.JAR in
C:\<oraclehome>\db_1\jdbc\lib

and also set this path using command prompt
set CLASSPATH = C:\<oraclehome>\db_1\jdbc\lib

please guide what am i missing,,,

thanks
  • 1. Re: java.lang.NullPointerException in JDBC
    rp0428 Guru
    Currently Being Moderated
    >
    I am trying to insert a row in oracle table using java. I got this code from a demo..
    . . .
    String driver="sun.jdbc.odbc.JdbcOdbcDriver";
    String cs="jdbc:odbc:connect_oracle";
    . . .
    I placed CLASS12.JAR in
    C:\<oraclehome>\db_1\jdbc\lib
    >
    What demo? Post the link.

    If your driver is ODBC then why are you using the oracle 1.2 jar file? You need the jar file that has the JdbcOdbcDriver class in it. That class is in the rt.jar which is part of the Sun JRE. Are you using the Sun JRE or another JRE?

    Your error message is
    >
    java.sql.SQLException: [Microsoft][ODBC Driver Manager] Data source name not found and no default driver specified
    >
    Do you have datasource with the name you are using?
    >
    jdbc:odbc:connect_oracle
  • 2. Re: java.lang.NullPointerException in JDBC
    Tolls Journeyer
    Currently Being Moderated
    To expand on rp0428s post.
    You should really be using the driver associated with the database you are connecting to.
    You should essentially never have to use the JDBC-ODBC bridge driver (which is the one you are asking your code to use with that connection string 'jdbc:odbc:etc etc').

    Each driver has a specific string associated with it which you will find in the documentation for that driver.

    For Oracle you want to use the driver that matches your target JRE to the version of Oracle you are using.
    classes12 is for Java 1.2 and 1.3, which I hope you are not stuck on as they are several years out of date.
    The link here will tell you which one to use:
    http://www.oracle.com/technetwork/database/enterprise-edition/jdbc-10201-088211.html
  • 3. Re: java.lang.NullPointerException in JDBC
    usman_noshahi Newbie
    Currently Being Moderated
    thanks for your replies ..

    my problem is solved now , thanks
    actually I was missing Data Source name in windows Administrator tools while configuring ODBC..


    thanks again
  • 4. Re: java.lang.NullPointerException in JDBC
    Tolls Journeyer
    Currently Being Moderated
    usman_noshahi wrote:
    thanks for your replies ..

    my problem is solved now , thanks
    actually I was missing Data Source name in windows Administrator tools while configuring ODBC..


    thanks again
    Yes.
    You could do that and completely ignore the fact that you shouldn't be using the JDBC/ODBC bridge at all.
    Or you could do it properly...

Legend

  • Correct Answers - 10 points
  • Helpful Answers - 5 points