1

hello i am trying to load my jdbc diver through classloader

here i am code but why i get this error if possible than give me some example

i don not what to set class path variable

i am making a database application and this application need to connect database again and again and i want to give this application to my friend but my friend not know about class path he is like normal user ,

my application can connect 4 type of database MS-Access,MySQL,Oracle,SQLlite... in user system i have to set 5 class path variable and provide 5 jar file

if i give this application 100 people than they have set set class path variable

i can include jar file with my application but how can i set class path dynamically .... please provide some example...

  package classload;

import java.io.File;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLClassLoader;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

public class ClassLoad {

    static Connection con;
    public static void main(String[] args) {

         File jar = new File("C:\\query\\Driver.jar").getAbsoluteFile();
          if(jar.exists()){
            System.out.print("File exits");  
          }

          URL urls[] = null;
        try {
            urls = new URL[] {
                jar.toURI().toURL()
              };
        } catch (MalformedURLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

          ClassLoader cl = new URLClassLoader(urls);

          try {
              Class.forName("com.mysql.jdbc.Driver", true, cl);
            con=DriverManager.getConnection("jdbc:mysql://localhost", "root", "anil");
            Statement stm=con.createStatement();
            ResultSet result=stm.executeQuery("select *from actor");
            while(result.next()){
                System.out.print(result.getInt(1)+" "+result.getString(2)+" "+result.getString(3));
                System.out.println("");

            }

        } catch (SQLException e) {
            System.out.println(e);

        }catch(ClassNotFoundException e){
            System.out.println(e);
        }



    }

}

exception is

java.sql.SQLException: No suitable driver found for jdbc:mysql://localhost
user3226704
  • 91
  • 1
  • 5
  • 1
    Because you load jdbc driver in separate classloader, so DriverManager can't see it. Replace current classloader: Thread.getCurrentThread.setClassLoader(cl); before DriverManager.getConnection. – Alex Chernyshev Aug 04 '14 at 16:48

2 Answers2

0

Just use One-Jar to package the application and all of the dependencies into single fat jar. Your solution is no good. Your friend would have to use the same folder structure as you are in order for it to work.

dimoniy
  • 5,820
  • 2
  • 24
  • 22
0

This error is coming probably because the required jar file mysql-connector has not been included in your project. Try including jar file as shown here. And try this code to load Driver class:

Class.forName("com.mysql.jdbc.Driver");
con=DriverManager.getConnection("jdbc:mysql://localhost:3306/jlcstudents","root","password");
Community
  • 1
  • 1
jcool
  • 314
  • 1
  • 3
  • 11