Latest Tutorials| Questions and Answers|Ask Questions?|Site Map



Home Answers Viewqa JDBC connecting to access database
Login         

View Questions and Answers by Category

Advertisements


 
Have Programming Question? Ask it here!
 
 
 


A.K.G.M Dilrukshi
connecting to access database
1 Answer(s)      4 years and a month ago
Posted in : JDBC


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(){}

}


Advertisement
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... Access Driver(*.mdb) Select database name and Create the DSN name (e.g access... 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 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 code of reset password to database
Connecting code of reset password to database  connecting code of reset password to database
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
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
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
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
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
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
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
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
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
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
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
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
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
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
Connecting to MySQL database and retrieving and displaying data in JSP page
Connecting to MySQL database and retrieving and displaying data in JSP page...;org.gjt.mm.mysql.Driver").newInstance(); Connecting to your database To connect...; This tutorial shows you how to connect to MySQL database and retrieve the data
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 successfully connect using one PC. But when I use another PC to access
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
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
Advertisements
 

 

 

DMCA.com