connecting to access database

connecting to access database

print("code sample");Hi I Write java projrame in notepad,I use 3 notepad pages to write this program,when i run this there is no error but my data is not going to my Acess Database. There is working exception which I wrote("SQL ERROR") Please help me to slove this problem. here is my code,

//first page I save as LibraryGUI.java import java.awt.*; import javax.swing.*;

public class LibraryGUI{

public static JButton add,update,del,view; public static JTextField autxt,idtxt,nametxt;

public static void main(String[] arg){

JFrame f  =new JFrame("My Library");
JPanel p = new JPanel();
p.setLayout(new BorderLayout());

JPanel center = new JPanel();
center.setLayout(new GridLayout(3,2));

center.add(new JLabel("Book ID"));
idtxt=new JTextField(10);
center.add(idtxt);

center.add(new JLabel("Book Name"));
nametxt=new JTextField(10);
center.add(nametxt);

center.add(new JLabel("Auther"));
autxt=new JTextField(10);
center.add(autxt);

p.add(center,BorderLayout.CENTER);

JPanel south = new JPanel();

add = new JButton("Add");
add.addActionListener(new LibraryHandler());
update = new JButton("Update");
update.addActionListener(new LibraryHandler());
del = new JButton("Delete");
del.addActionListener(new LibraryHandler());
view = new JButton("View");
view.addActionListener(new LibraryHandler());

south.add(add);
south.add(update);
south.add(del);
south.add(view);

p.add(south,BorderLayout.SOUTH);

f.add(p);
f.pack();
f.setVisible(true);

}

}

//My second page Isave as LibraryConnector.java import java.awt.event.*;

public class LibraryHandler extends LibraryGUI implements ActionListener{

LibraryConnector c=new LibraryConnector();

public void actionPerformed(ActionEvent e){

String id=idtxt.getText();
String name = nametxt.getText();
String auther = autxt.getText();

if(e.getSource()==add){ c.add(id,name,auther); } else if(e.getSource()==update){ c.update(); } else if(e.getSource()==del){ c.delete(id); } else if(e.getSource()==view){ c.view(); }

}

}

//this is my thired page I save as LibraryConnector.java import java.sql.*;

public class LibraryConnector{ public static void connect(String query){ try{ Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");

}catch(ClassNotFoundException e){ System.out.print("No Suitable Driver"); } try{ Connection con = DriverManager.getConnection("jdbc:odbc:bookss"); System.out.print("Connected to "+con.getCatalog()); Statement st = con.createStatement(); st.executeUpdate(query); System.out.print(query); }catch(SQLException e){ System.out.print("CONNECTION FAILD "); }

} public void add(String id,String name, String auther){

String query="INSERT INTO books VALUES(' "+id+" ',' "+name+" ',' "+auther+" ')";
System.out.print(""+id+""+name+""+auther);
connect(query);
}

public void delete(String id){ String query="DELETE FROM books WHERE bookid='"+id+"'"; connect(query); } public void update(){} public void view(){}

}

View Answers

November 17, 2010 at 12:53 PM

Hello Friend,

Have you set the dsn connection properly? If not then follow these steps:

  1. Open Data Sources (Start->Control Panel->Administrative Tool->Data Sources(ODBC)
  2. Open User DSN tab
  3. Add a user DSN
  4. Select Microsoft Access Driver(*.mdb)
  5. Select database name and Create the DSN name (e.g access)
  6. Click "Ok" and then restart your compiler and compile the following code:

1)LibraryGUI.java:

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

public class LibraryGUI{
public static JButton add,update,del,view;
public static JTextField autxt,idtxt,nametxt;
public static void main(String[] arg){
JFrame f  =new JFrame("My Library");
JPanel p = new JPanel();
p.setLayout(new BorderLayout());
JPanel center = new JPanel();
center.setLayout(new GridLayout(3,2));
center.add(new JLabel("Book ID"));
idtxt=new JTextField(10);
center.add(idtxt);
center.add(new JLabel("Book Name"));
nametxt=new JTextField(10);
center.add(nametxt);
center.add(new JLabel("Author"));
autxt=new JTextField(10);
center.add(autxt);
p.add(center,BorderLayout.CENTER);
JPanel south = new JPanel();
add = new JButton("Add");
add.addActionListener(new LibraryHandler());
update = new JButton("Update");
update.addActionListener(new LibraryHandler());
del = new JButton("Delete");
del.addActionListener(new LibraryHandler());
view = new JButton("View");
view.addActionListener(new LibraryHandler());
south.add(add);
south.add(update);
south.add(del);
south.add(view);
p.add(south,BorderLayout.SOUTH);
f.add(p);
f.pack();
f.setVisible(true);
}
}

2)LibraryHandler.java:

import java.awt.event.*;

public class LibraryHandler extends LibraryGUI implements ActionListener{
LibraryConnector c=new LibraryConnector();
public void actionPerformed(ActionEvent e){
String id=idtxt.getText();
String name = nametxt.getText();
String auther = autxt.getText();
if(e.getSource()==add){
    c.add(Integer.parseInt(id),name,auther);
    }
else if(e.getSource()==update){
    c.update();
    }
else if(e.getSource()==del){
    c.delete(Integer.parseInt(id));
    }
else if(e.getSource()==view){
    c.view();
    }
}
}

3)LibraryConnector.java:

import java.sql.*;

public class LibraryConnector{
public static void connect(String query){
try{
    Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
    Connection con = DriverManager.getConnection("jdbc:odbc:access");
    System.out.print("Connected to "+con.getCatalog());
    Statement st = con.createStatement();
    st.executeUpdate(query);
    System.out.print(query);
    }catch(Exception e){
        System.out.print(e);
    }
   }
public void add(int id,String name, String auther){
String query="INSERT INTO book VALUES( "+id+" ,' "+name+" ',' "+auther+" ')";
System.out.print(""+id+""+name+""+auther);
connect(query);
}
public void delete(int id){
    String query="DELETE FROM book WHERE bookid="+id+"";
    connect(query);
    }
public void update(){}
public void view(){}
}

For the above code, we have created a table book(bookid(Number), name(Text), author(Text))

Thanks









Related Tutorials/Questions & Answers:
connecting to access database
connecting to access database  print("code sample");Hi I Write java... Add a user DSN Select Microsoft Access Driver(*.mdb) Select database name... this there is no error but my data is not going to my Acess Database. There is working
Connecting to a database through the Proxy.
Connecting to a database through the Proxy.  Connecting to a database through the Proxy I want to connect to remote database using a program that is running in the local network behind the proxy. Is that possible
Advertisements
connecting to database - Struts
connecting to database  Hi I am having problems with connection to MS SQL Server 2005 database. My first is what do i write in struts... information via the database in my web page. Thanks Tayo  Hi friend
Connecting code of reset password to database
Connecting code of reset password to database  connecting code of reset password to database
Connecting JTable to database - JDBC
Connecting JTable to database  Hi.. I am doing a project on Project... to store this JTable content in my database table.. This is a very important...("jdbc:odbc:access"); for(int i=0;i  Hi Friend, Make one change
Connecting to MYSQL Database in Java
Connecting to MYSQL Database in Java  I've tried executing the code... to the database"); conn.close(); System.out.println("Disconnected from database"); } catch (Exception e) { System.out.println("Error
connecting with database - Struts
connecting with database  I am creating an application where when jsp page is displayed, it contains the combo box where data is populated from the database.it has 3 buttons and the functionality for all buttons is different
Connecting Oracle database with struts - Struts
Connecting Oracle database with struts  Can anyone please provide me some solutions on Connection between Oracle database and struts
Connect database in Access to Netbean?
Connect database in Access to Netbean?  how to connect database in micrsoft access to Netbean?i know it can be connected by using JDBC-ODBC bridge, can i know the steps in connecting the database? Is there any source code
connecting to a database dynamically - JSP-Servlet
connecting to a database dynamically   abc.html... with database dynamically. Plz debug the code and explain the reasons for the exception... code where 'access' is my data source name: Thanks
Problems connecting to a database. Java/SQLite
Problems connecting to a database. Java/SQLite  `print("try { con = DriverManager.getConnection("jdbc:sqlite:db/Freepark.sqlite"); } catch... on an SQL database but i am having problems connecting to it, I think the problem
code for connecting reset password code to database.
code for connecting reset password code to database.  code for connecting reset password code to database.   Hello Friend, Do you want to change your password and update the password to database
Connecting to Database from a hyperlink in JSP - JSP-Servlet
Connecting to Database from a hyperlink in JSP  How can I connect to database by clicking on a hyperlink in a JSP Page.Can you please give me sample... which is connect to database using jdbc database
jsp -sevlet connecting to database using dropdown
jsp -sevlet connecting to database using dropdown  How can I get my dropdown list from oracle database and then submit it to another table in JSP. I... to the database and fetches an array of strings from a database table and then sends
ACCESS DATABASE FROM HTML
ACCESS DATABASE FROM HTML  I want to access sql 2008 database in html page without help of ADODB connection.. because if access through ADODB means there is a security problem. so, Access database in html page(client side
how to send emails to many using jsp by connecting to ms access dabase.
how to send emails to many using jsp by connecting to ms access dabase.  Hi. I am trying to send multiple emails and the email ids are stored in a table in a msaccess database.I am using jsp for this.And finally there should
Access 2007 database connectivity
Access 2007 database connectivity  i design an application form... source and destination. pls tell me the code of connectivity with access 2007 database using JComboBox.thanks
link hibernate to MS ACCESS database
link hibernate to MS ACCESS database  how to link hibernate to ms access database instead of sql database
Connecting to the Database Using JDBC and Pure Java driver
Connecting to the Database JDBC Driver In our search engine we are using MySQL database server and MM.MySQL Driver for connecting our application to the database. MM.MySQL Driver
how to access database in applet
how to access database in applet  HI... I'm having an applet where we should display the database values in the applet... It works fine in the local system(same network)... but when its in the server, we r getting null values
How to access the database from JSP?
How to access the database from JSP?  Hi, What is the process... you can access the database by embedding the JDBC code. But this is not the best... database from JSP which explains you how to access the database by embedding
Applet database access - Applet
Applet database access  HI... I'm having an applet where we should display the database values in the applet... It works fine in the local system... but when its in the server, we r getting null values in the local system.. I
JDBC access database
JDBC access database       JDBC... and interfaces for connecting the front end in Java application with database connections... that helps in understanding JDBC access database. The code illustrates the list
JSP and Database access
JSP and Database access  Hi, Please help me with the following program. I am not able to update all the pa column values in my database. csea.jsp: <html> <body> <%@page import="java.sql.*"%> <form method
how to access the MS ACCESS database with java
how to access the MS ACCESS database with java   how to access the MS ACCESS database with java how can we insert,delete,update,search records of ms access with java   Java MS Access database connectivity Follow
The JDBC API to access a desktop database like Microsoft Access over the network.
The JDBC API to access a desktop database like Microsoft Access over the network.  How can I use the JDBC API to access a desktop database like Microsoft Access over the network
how to access the MS ACCESS database with java - Java Beginners
how to access the MS ACCESS database with java  how can we insert,delete,update,search records of ms access with java
Migrating sql database to Access through coding
Migrating sql database to Access through coding  How can i migrate SQL database(table) to My access through coding
code for insert the value from jsp to access database
code for insert the value from jsp to access database  code for insert the value from jsp to access database
Connecting to a MySQL Database in Java
Connecting to a MySQL Database in Java   ... access or retrieve data form MySQL database. We are going to make a program on connecting to a MySQL database, after going through this program you
Error in connecting to the mySQL database in TOMCAT using more than one PC (database connection pooling)
Error in connecting to the mySQL database in TOMCAT using more than one PC (database connection pooling)  how do i implement connection pooling... to access the same application/database, I get a java.lang.NullPointer
How to access (MySQL)database from J2ME?
How to access (MySQL)database from J2ME?  I am new to J2ME. I am using NetBeans. Can anyone help me? How to access (MySQL)database from J2ME? ( I search a lot I found that there is need to access database through servlet
Connecting to MySQL database and retrieving and displaying data in JSP page
Connecting to MySQL database and retrieving and displaying data in JSP page...; This tutorial shows you how to connect to MySQL database and retrieve the data from the database. In this example we will use tomcat version 4.0.3 to run our
how to delete the access database value in jsp
how to delete the access database value in jsp  i loaded my database data into the dropdown list box...now i want to delete the value..plz send the source code to delete the value in dropdown list box and also from database
how to delete the access database value in jsp
how to delete the access database value in jsp  i loaded my database data into the dropdown list box...now i want to delete the value..plz send the source code to delete the value in dropdown list box and also from database
updating an access database with visual basic 2010
updating an access database with visual basic 2010  I am building a program in visual basic 2010 that connects to an access database. I... the information is entered I need it to update the information in access
jfreechart display from access database data.
jfreechart display from access database data.  I have made a database... to retrieve the data from the access database using prepared statement and then display... is to be done in a servlet.. Note that it is a access made database. How can I
How To Connect MS ACCESS 2003 Database in C Program with Graphics.
How To Connect MS ACCESS 2003 Database in C Program with Graphics.  How To Connect MS ACCESS 2003 Database in C Program with Graphics
display data from a table in Access Database in a HTML page
display data from a table in Access Database in a HTML page  how to display data from a table in Access Database in a HTML page in a Java Program
how to connect to MS access database in JSP?
how to connect to MS access database in JSP?  how to connect to MS access database in JSP? Any seetings/drivers need to be set or installed before... and select the driver Microsoft Access Driver(*.mdb). 3)After selecting the driver
applet connected to table in MS Access database
applet connected to table in MS Access database   i have connected my java code with the MS access database and this is my code, can anyone tell me...:DRIVER={Microsoft Access Driver (*.mdb)};DBQ=faltu.mdb"; // String url = "jdbc
Extracting table from Access Database to Servlet
Extracting table from Access Database to Servlet  Sir, I have a table with 4 field deptid, headid, normalexpend and projectexpend. Now I have to display a consolidated data where sum of normal_expend under particular head
Create Reports in Java with access database - Java Beginners
Create Reports in Java with access database  Hello Sir How to Create Reports of Any Type using Java when i submit data is stored in the MS access database,but when I click on PRINT Button then it will display it into either Text
retreiving data from microsoft access database
retreiving data from microsoft access database  How can i retrieve data from microsoft access when i have select the vaules in combo box and text box. When i select these values... i want to retrieve the corresponding columns
jfreechart displaying chart from access database
jfreechart displaying chart from access database  I have these 2 codes. array.java----in which i retrieve the values from the database . import... javax.servlet.http.HttpServletResponse; /** * * @author AARUSHI */ class database { int roll; String
Problem with JRadioButton and Access Database - Java Beginners
in Access Databse, I want to store course type in to database and also other fields...Problem with JRadioButton and Access Database  Hello sir ,Here I have...(); JOptionPane.showMessageDialog(p1,"Data is successfully inserted into database."); con.close
to get picture from ms access database by jsp-servlet....
to get picture from ms access database by jsp-servlet....  I have inserted a picture in ms access data base,,,how we can retrieve that picture by using jsp
connecting databases
connecting databases  I need to connect mysql on 2 or more remote pc'c. how can i giv the ip address for 2 or more systems. is it possible to connect to the required systems by user specifying the database and table name my
Null pointer exceptation-Java Servlet web application,Problem connecting with MYSQL database
Null pointer exceptation-Java Servlet web application,Problem connecting... system won't be able to connect to database in pooling environment. i try follow some.../database_name" auth="Container" maxActive="100" maxIdle="30" maxWait
HTML(Registration form) to Jsp to stored into MS ACCESS database
HTML(Registration form) to Jsp to stored into MS ACCESS database  i am sending one html file that contain 18 fields these are stored in ms-access database by using jsp code.i want to urgent jsp code. please urgent sir. thank

Ads