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);
}
}