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

Hello Friend,
Have you set the dsn connection properly? If not then follow these steps:
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
If you are facing any programming issue, such as compilation errors or not able to find the code you are looking for.
Ask your questions, our development team will try to give answers to your questions.