My code is :

/* Database.java*/

import java.sql.*;
import java.awt.*;
import javax.swing.*;

public class Database{
	private static Connection con = null;
	public static Statement statement = null;
	
	public static void makeConnection() {
		JOptionPane.showMessageDialog(null, "database");
		String driver = "sun.jdbc.odbc.JdbcOdbcDriver";
		String url = "jdbc:odbc:lib";
		String username = ""; 
		String password = "";

		try {
			Class.forName(driver);
			con = DriverManager.getConnection(url, username, password);
			statement = con.createStatement();
			
		} catch (ClassNotFoundException cnfex) {
			System.err.println("Failed to load JDBC/ODBC driver.");
			cnfex.printStackTrace();
			System.exit(1);
		} catch (SQLException sqlex) {
			System.err.println("Unable to connect");
			sqlex.printStackTrace();
		}
	}

	public static void closeConnection() {
		try {
			statement.close();
			con.close();
		} catch (SQLException sqlex) {
			System.out.println("Error in closing connection. Terminating program.");
			System.exit(1);
		}	
	}
	
	public static ResultSet executeQuery(String query) throws SQLException{
		return statement.executeQuery(query);
	}
	
	public static int executeUpdate(String query) throws SQLException {
		return statement.executeUpdate(query);
	}
}

And my output is

java.sql.SQLException: [Microsoft][ODBC Driver Manager] Data source name not found and no default driver specified

How can i solve this problem?

At what line you get that error?

At what line you get that error?

It dont have any error, it just cannot make a connection with the database

Dear Friend, the data source name is 'lib' which you have specified in the String url. You have to create that data source name from the control panel. The steps are: Open Control Panel > Administrative Tools > Data Sources (ODBC) > Click Add button there > Select the driver for the database you're using ( MS Access/SQL Server/ DBase etc). Select the one which you're using now. After you select one from that list click on Finish button > THen you have to enter the datasource name there. In your case it is 'lib'. Then click 'OK' and run the program again and see.

Dear Friend, the data source name is 'lib' which you have specified in the String url. You have to create that data source name from the control panel. The steps are: Open Control Panel > Administrative Tools > Data Sources (ODBC) > Click Add button there > Select the driver for the database you're using ( MS Access/SQL Server/ DBase etc). Select the one which you're using now. After you select one from that list click on Finish button > THen you have to enter the datasource name there. In your case it is 'lib'. Then click 'OK' and run the program again and see.

sorry i really a begginer, where can i download the driver, i using window 7 ultimate and it dint provide me the driver

Driver will surely be there in the Data Sources (ODBC). Kindly cross check it.

Driver will surely be there in the Data Sources (ODBC). Kindly cross check it.

I check the ODBC, there is 2 for SQL but none is for Ms Access so i wonder do i need to download it or else

My code is :

/* Database.java*/

import java.sql.*;
import java.awt.*;
import javax.swing.*;

public class Database{
    private static Connection con = null;
    public static Statement statement = null;

    public static void makeConnection() {
        JOptionPane.showMessageDialog(null, "database");
        String driver = "sun.jdbc.odbc.JdbcOdbcDriver";
        String url = "jdbc:odbc:lib";
        String username = ""; 
        String password = "";

        try {
            Class.forName(driver);
            con = DriverManager.getConnection(url, username, password);
            statement = con.createStatement();

        } catch (ClassNotFoundException cnfex) {
            System.err.println("Failed to load JDBC/ODBC driver.");
            cnfex.printStackTrace();
            System.exit(1);
        } catch (SQLException sqlex) {
            System.err.println("Unable to connect");
            sqlex.printStackTrace();
        }
    }

    public static void closeConnection() {
        try {
            statement.close();
            con.close();
        } catch (SQLException sqlex) {
            System.out.println("Error in closing connection. Terminating program.");
            System.exit(1);
        }   
    }

    public static ResultSet executeQuery(String query) throws SQLException{
        return statement.executeQuery(query);
    }

    public static int executeUpdate(String query) throws SQLException {
        return statement.executeUpdate(query);
    }
}

And my output is

java.sql.SQLException: [Microsoft][ODBC Driver Manager] Data source name not found and no default driver specified

How can i solve this problem?

you can maked the settings , because odbc connection need the control panel--> administrative tools-->odbc settings-->
then onlyypu run your program. it's my guess .this is one of t sql exception

you can maked the settings , because odbc connection need the control panel--> administrative tools-->odbc settings-->
then onlyypu run your program. it's my guess .this is one of t sql exception

Yah, but in window 7, there is no driver for Ms Access. That y i wonder do where i can i download it

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.