0

Got error while I'm tried to connect SQL database using cmd. Here is my program.I use jdk 6 version to compile and run Thanks in Advance.

import java.io.*;
import java.sql.*;

class Dbs
{
   public static void main(String args[]) throws Exception
  {
      try
      {
       Connection con = null;
       Statement s = null;
       ResultSet rs = null;

       Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
       String bala = "jdbc:odbc:Driver={Microsoft Access Driver (*.mdb)};DBQ=c:\\ss.mdb";

        con = DriverManager.getConnection(bala,"","");
        s = con.createStatement();
        rs =s.executeQuery("select * from Table1");
       
       while(rs.next())
       {
           System.out.println("Name"+rs.getString("name"));
           System.out.println("No"+rs.getString("num"));
       }
     }

     catch(Exception e)
     {
     System.out.print(e);
     }
  }
}

Error:

enter image description here

Community
  • 1
  • 1
Bala555
  • 119
  • 1
  • 6

2 Answers2

1

Are you in the correct directory ? It seems you are in your jdk directory, where you should be in your program's directory (where your Dbs.class resides).

Also, you missed the public keyword. Here, your Dbs is package local, so it won't be visible outside the package. Depending on where you use it, it may trigger the error.

Try:

public class Dbs {
   // code
}

Also,

If you don’t explicitly specify a package, your classes and interfaces end up in an unnamed packaged, also known as the default package. Best practice is not to use the default package for any production code.

more here.

Derlin
  • 9,572
  • 2
  • 32
  • 53
0

the error says the class you are trying to get is not where it must, check the the jdbc driver is in the correct place ,check if you have your JAVA_PATH set and put a try catch to see if there is another error who is causing that

Pavul Zavala
  • 427
  • 5
  • 11