Got error while I'm tried to connect database

java database

34 просмотра

2 ответа

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

Автор: Bala555 Источник Размещён: 08.11.2019 10:55

Ответы (2)


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 Размещён: 20.08.2016 05:39

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 Размещён: 20.08.2016 05:47
Вопросы из категории :
32x32