Hello, i'm trying to add some values in the database (ms access) but i am having the following error:
Error:java.sql.SQLException: [Microsoft][ODBC Driver Manager] Data source name not found and no default driver specified

Can somebody help? Here is my code:

package db_con;
import java.sql.*;

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
/* package db_con;

/**
 *
 * @author User
 */
public class Db_con extends JPanel {

    public String name="Mary";
    public String address="Flacq";
    Connection con;
    Statement st;

    JButton b_save;
    public Db_con(){

        b_save=new JButton("Save");
        //b_save.setLocation(20,30);
        this.add(b_save);
        b_save.addActionListener(new ActionListener(){
            public void actionPerformed(ActionEvent event){

                            System.out.println("Button save:");

                            try{
                    Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
                    String filename="C:/Users/User/Documents/NetBeansProjects/db_con/Database3.accdb";
                    String database = "jdbc:odbc:Driver={Microsoft Access Driver (*.mdb, *.accdb)};DBQ=";
                    database+= filename.trim() + ";DriverID=22;READONLY=true}";

                    con=DriverManager.getConnection(database,"","");
                    st=con.createStatement();

                                        st.execute("INSERT INTO Table1(2,name,address)");

                                        st.execute("select* from Table1");
                                        ResultSet rs=st.getResultSet();

                                        if(rs!=null){
                                            while(rs.next()){
                                                System.out.println("Name:"+rs.getString("Name"));
                                            }
                                        }

                                     st.close();
                                     con.close();
                            }


                         catch(Exception e)  {
                             System.out.println("Error:"+e);
                         } 

                        }






                        });
    }
    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        // TODO code application logic here
        Db_con f1=new Db_con();
        JFrame f=new JFrame();
        f.setSize(250, 300);
        f.setContentPane(f1);
        f.setVisible(true);
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    }
}

looks to me like you don't have the appropriate driver in your path.
you'll need to add the .jar file containing these driver(s) to your project or your classpath.

hai Shania_01,

you can also connect ms access database by creating Data Source Name For your access file otherwise you need to add ms access connector related jar file to your application as stultuske said

you may try the following urls, it helps you a lot

http://www.hossainkhan.info/content/using-ms-access-java-through-jdbc-odbc-connection

this URL is for different ways of connecting ms access database from java application

http://www.planet-source-code.com/vb/scripts/ShowCode.asp?txtCodeId=2691&lngWId=2

the driver is provided with the JVM (IF you're using a Sun/Oracle JVM). The error here is the connect string which is fundamentally flawed.

As an aside, using the bridge driver and Access databases is not to be recommended, the JVM comes with its own embedded SQL database these days, see the documentation.

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.