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:
- Open Data Sources (Start->Control Panel->Administrative Tool->Data Sources(ODBC)
- Open User DSN tab
- Add a user DSN
- Select Microsoft Access Driver(*.mdb)
- Select database name and Create the DSN name (e.g access)
- 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 databaseconnecting 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 - Strutsconnecting 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 JTable to database - JDBCConnecting 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 JavaConnecting 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 - Strutsconnecting 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
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
Problems connecting to a database. Java/SQLiteProblems
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
jsp -sevlet connecting to database using dropdownjsp -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 HTMLACCESS 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
Access 2007 database connectivityAccess 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
how to access database in applethow 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 - AppletApplet
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 accessJSP 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
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
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
how to delete the access database value in jsphow 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 jsphow 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 2010updating 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 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 ServletExtracting 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
retreiving data from microsoft access databaseretreiving 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 databasejfreechart 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
connecting databasesconnecting 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