By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,375 Members | 1,111 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,375 IT Pros & Developers. It's quick & easy.

jdbc with mysql

oll3i
100+
P: 679
my librarybean

Expand|Select|Wrap|Line Numbers
  1.  
  2. package library.ejb;
  3. import java.sql.*;
  4. import javax.ejb.*;
  5. import library.common.*;
  6.  
  7. @Stateless @Remote
  8. public class LibraryBean implements LibraryInterface{
  9. ResultSet resultset;
  10. Statement statement;
  11.   public LibraryBean() {
  12.   }
  13.   public ResultSet getBookByAuthor(String author){
  14.       try {
  15.           Class.forName("com.mysql.jdbc.Driver");
  16.  
  17.           //Define URL of database server for
  18.           // database named JunkDB on the localhost
  19.           // with the default port number 3306.
  20.           String url =
  21.                 "jdbc:mysql://localhost:3306/Library";
  22.  
  23.           //Get a connection to the database for a
  24.  
  25.           Connection connection =DriverManager.getConnection(
  26.                             url,"kasia", "pass");
  27.           statement = connection.createStatement();
  28.           resultset =  statement.executeQuery("SELECT * " +
  29.           "from books WHERE author = "+author+" ORDER BY name");
  30.           connection.close();
  31.       }catch( Exception e ) {
  32.               e.printStackTrace();
  33.  
  34.             }//end catch
  35.       return resultset;
  36.   }
  37.   public ResultSet getBookByName(String name){
  38.       try {
  39.           Class.forName("com.mysql.jdbc.Driver");
  40.  
  41.           //Define URL of database server for
  42.           // database named JunkDB on the localhost
  43.           // with the default port number 3306.
  44.           String url =
  45.                 "jdbc:mysql://localhost:3306/Library";
  46.  
  47.           //Get a connection to the database for a
  48.  
  49.           Connection connection =DriverManager.getConnection(
  50.                             url,"kasia", "pass");
  51.           statement = connection.createStatement();
  52.           resultset =  statement.executeQuery("SELECT * " +
  53.           "from books WHERE name ="+name+" ORDER BY name");
  54.           connection.close();
  55.       }catch( Exception e ) {
  56.               e.printStackTrace();
  57.  
  58.             }//end catch
  59.  
  60.       return resultset;
  61.   }
  62.   public ResultSet getBookByISBN(String isbn){
  63.       try {
  64.           Class.forName("com.mysql.jdbc.Driver");
  65.  
  66.           //Define URL of database server for
  67.           // database named JunkDB on the localhost
  68.           // with the default port number 3306.
  69.           String url =
  70.                 "jdbc:mysql://localhost:3306/Library";
  71.  
  72.           //Get a connection to the database for a
  73.  
  74.           Connection connection =DriverManager.getConnection(
  75.                             url,"kasia", "pass");
  76.           statement = connection.createStatement();
  77.           resultset =  statement.executeQuery("SELECT * " +
  78.           "from books WHERE ISBN ="+isbn+" ORDER BY name");
  79.           connection.close();
  80.       }catch( Exception e ) {
  81.               e.printStackTrace();
  82.  
  83.             }//end catch
  84.       return resultset;
  85.   }
  86.   public ResultSet getAllBooks(){
  87.       try {
  88.           Class.forName("com.mysql.jdbc.Driver");
  89.  
  90.           //Define URL of database server for
  91.           // database named JunkDB on the localhost
  92.           // with the default port number 3306.
  93.           String url =
  94.                 "jdbc:mysql://localhost:3306/Library";
  95.  
  96.           //Get a connection to the database for a
  97.  
  98.           Connection connection =DriverManager.getConnection(
  99.                             url,"kasia", "pass");
  100.           statement = connection.createStatement();
  101.           resultset =  statement.executeQuery("SELECT * " +
  102.           "from books ORDER BY name");
  103.           connection.close();
  104.       }catch( Exception e ) {
  105.               e.printStackTrace();
  106.  
  107.             }//end catch
  108.       return resultset;
  109.   }
  110.  
  111.  
  112.  
  113.  
  114.   }
  115.  
  116.  
  117.  
  118.  
  119.  
i dont know how to write where clause with a variable passed eg author
should it be
Expand|Select|Wrap|Line Numbers
  1. resultset =  statement.executeQuery("SELECT * " +
  2.           "from books WHERE author = "+author+" ORDER BY name");
  3.  
or
Expand|Select|Wrap|Line Numbers
  1. resultset =  statement.executeQuery("SELECT * " +
  2.           "from books WHERE author = :author  ORDER BY name");
because my app doesnt display records after a search
i checked jdbc with mysql first and it worked
do i need to set up a new connection pool in jdbc?
Jun 4 '07 #1
Share this Question
Share on Google+
5 Replies


oll3i
100+
P: 679
when i run it from bat i get


D:\JAVA_P~1\ZAD5_K~1\JAVACL~1>runClient.bat

D:\JAVA_P~1\ZAD5_K~1\JAVACL~1>java -cp .;C:SunSDKbin/lib/appserv-rt.jar;C:SunSDK
bin/lib/javaee.jar; library.client.LibraryClient JAVA_CLIENT
javax.naming.NoInitialContextException: Need to specify class name in environmen
t or system property, or as an applet parameter, or in an application resource f
ile: java.naming.factory.initial
at javax.naming.spi.NamingManager.getInitialContext(N amingManager.java:6
45)
at javax.naming.InitialContext.getDefaultInitCtx(Init ialContext.java:288
)
at javax.naming.InitialContext.getURLOrDefaultInitCtx (InitialContext.jav
a:325)
at javax.naming.InitialContext.lookup(InitialContext. java:392)
at library.client.LibraryClient.init(LibraryClient.ja va)
at library.client.LibraryClient.<init>(LibraryClient. java)
at library.client.LibraryClient.main(LibraryClient.ja va)
java.lang.NullPointerException
at library.client.LibraryClient.actionPerformed(Libra ryClient.java)
at javax.swing.AbstractButton.fireActionPerformed(Abs tractButton.java:19
95)
at javax.swing.AbstractButton$Handler.actionPerformed (AbstractButton.jav
a:2318)
at javax.swing.DefaultButtonModel.fireActionPerformed (DefaultButtonModel
.java:387)
at javax.swing.DefaultButtonModel.setPressed(DefaultB uttonModel.java:242
)
at javax.swing.plaf.basic.BasicButtonListener.mouseRe leased(BasicButtonL
istener.java:236)
at java.awt.Component.processMouseEvent(Component.jav a:6038)
at javax.swing.JComponent.processMouseEvent(JComponen t.java:3260)
at java.awt.Component.processEvent(Component.java:580 3)
at java.awt.Container.processEvent(Container.java:205 8)
at java.awt.Component.dispatchEventImpl(Component.jav a:4410)
at java.awt.Container.dispatchEventImpl(Container.jav a:2116)
at java.awt.Component.dispatchEvent(Component.java:42 40)
at java.awt.LightweightDispatcher.retargetMouseEvent( Container.java:4322
)
at java.awt.LightweightDispatcher.processMouseEvent(C ontainer.java:3986)

at java.awt.LightweightDispatcher.dispatchEvent(Conta iner.java:3916)
at java.awt.Container.dispatchEventImpl(Container.jav a:2102)
at java.awt.Window.dispatchEventImpl(Window.java:2429 )
at java.awt.Component.dispatchEvent(Component.java:42 40)
at java.awt.EventQueue.dispatchEvent(EventQueue.java: 599)
at java.awt.EventDispatchThread.pumpOneEventForFilter s(EventDispatchThre
ad.java:273)
at java.awt.EventDispatchThread.pumpEventsForFilter(E ventDispatchThread.
java:183)
at java.awt.EventDispatchThread.pumpEventsForHierarch y(EventDispatchThre
ad.java:173)
at java.awt.EventDispatchThread.pumpEvents(EventDispa tchThread.java:168)

at java.awt.EventDispatchThread.pumpEvents(EventDispa tchThread.java:160)

at java.awt.EventDispatchThread.run(EventDispatchThre ad.java:121)

D:\JAVA_P~1\ZAD5_K~1\JAVACL~1>
Jun 4 '07 #2

10K+
P: 13,264
when i run it from bat i get


D:\JAVA_P~1\ZAD5_K~1\JAVACL~1>runClient.bat

D:\JAVA_P~1\ZAD5_K~1\JAVACL~1>java -cp .;C:SunSDKbin/lib/appserv-rt.jar;C:SunSDK
bin/lib/javaee.jar; library.client.LibraryClient JAVA_CLIENT
javax.naming.NoInitialContextException: Need to specify class name in environmen
t or system property, or as an applet parameter, or in an application resource f
ile: java.naming.factory.initial
at javax.naming.spi.NamingManager.getInitialContext(N amingManager.java:6
45)
at javax.naming.InitialContext.getDefaultInitCtx(Init ialContext.java:288
)
at javax.naming.InitialContext.getURLOrDefaultInitCtx (InitialContext.jav
a:325)
at javax.naming.InitialContext.lookup(InitialContext. java:392)
at library.client.LibraryClient.init(LibraryClient.ja va)
at library.client.LibraryClient.<init>(LibraryClient. java)
at library.client.LibraryClient.main(LibraryClient.ja va)
java.lang.NullPointerException
at library.client.LibraryClient.actionPerformed(Libra ryClient.java)
at javax.swing.AbstractButton.fireActionPerformed(Abs tractButton.java:19
95)
at javax.swing.AbstractButton$Handler.actionPerformed (AbstractButton.jav
a:2318)
at javax.swing.DefaultButtonModel.fireActionPerformed (DefaultButtonModel
.java:387)
at javax.swing.DefaultButtonModel.setPressed(DefaultB uttonModel.java:242
)
at javax.swing.plaf.basic.BasicButtonListener.mouseRe leased(BasicButtonL
istener.java:236)
at java.awt.Component.processMouseEvent(Component.jav a:6038)
at javax.swing.JComponent.processMouseEvent(JComponen t.java:3260)
at java.awt.Component.processEvent(Component.java:580 3)
at java.awt.Container.processEvent(Container.java:205 8)
at java.awt.Component.dispatchEventImpl(Component.jav a:4410)
at java.awt.Container.dispatchEventImpl(Container.jav a:2116)
at java.awt.Component.dispatchEvent(Component.java:42 40)
at java.awt.LightweightDispatcher.retargetMouseEvent( Container.java:4322
)
at java.awt.LightweightDispatcher.processMouseEvent(C ontainer.java:3986)

at java.awt.LightweightDispatcher.dispatchEvent(Conta iner.java:3916)
at java.awt.Container.dispatchEventImpl(Container.jav a:2102)
at java.awt.Window.dispatchEventImpl(Window.java:2429 )
at java.awt.Component.dispatchEvent(Component.java:42 40)
at java.awt.EventQueue.dispatchEvent(EventQueue.java: 599)
at java.awt.EventDispatchThread.pumpOneEventForFilter s(EventDispatchThre
ad.java:273)
at java.awt.EventDispatchThread.pumpEventsForFilter(E ventDispatchThread.
java:183)
at java.awt.EventDispatchThread.pumpEventsForHierarch y(EventDispatchThre
ad.java:173)
at java.awt.EventDispatchThread.pumpEvents(EventDispa tchThread.java:168)

at java.awt.EventDispatchThread.pumpEvents(EventDispa tchThread.java:160)

at java.awt.EventDispatchThread.run(EventDispatchThre ad.java:121)

D:\JAVA_P~1\ZAD5_K~1\JAVACL~1>
But the exception you are getting is for something else.

It says NoInitialContextException meaning your beans are never found anyway.
Jun 5 '07 #3

oll3i
100+
P: 679
so what am i doing wrong?
Jun 5 '07 #4

10K+
P: 13,264
so what am i doing wrong?
The LibrayClient class is the one throwing the exception. Check how you got your context in it
Jun 5 '07 #5

oll3i
100+
P: 679
this is my LibraryClient

Expand|Select|Wrap|Line Numbers
  1.  
  2. package library.client;
  3.  
  4.  
  5.  
  6.  
  7. import java.awt.BorderLayout;
  8. import java.awt.Color;
  9. import java.awt.FlowLayout;
  10. import java.awt.event.ActionEvent;
  11. import java.awt.event.ActionListener;
  12. import java.lang.reflect.Method;
  13. import java.sql.ResultSet;
  14. import javax.ejb.*;
  15. import javax.naming.*;
  16. import javax.swing.*;
  17.  
  18. import library.common.*;
  19.  
  20.  
  21. @SuppressWarnings("serial")
  22. public class LibraryClient extends JFrame implements ActionListener {
  23.  
  24.   @EJB
  25.   private static LibraryInterface li;
  26.  
  27.   private JTextField book_author = new JTextField(10);
  28.   private JTextField book_title = new JTextField(10);
  29.   private JTextField book_isbn = new JTextField(10);
  30.   private JLabel author_label = new JLabel("Podaj autora:");
  31.   private JLabel book_title_label = new JLabel("Podaj tytul ksiazki");
  32.   private JLabel isbn_label = new JLabel("Podaj ISBN");
  33.   private JTextArea display_books = new JTextArea(20,50);
  34.   private JButton search = new JButton("Search");
  35.   private ResultSet resultset;
  36.   public LibraryClient() {
  37.     this("DEFAULT_CONTEXT");
  38.   }
  39. private JPanel createPanel1(){
  40.      JPanel panel = new JPanel();
  41.      panel.add(author_label);
  42.      panel.add(book_author);
  43.  
  44.      panel.add(book_title_label);
  45.      panel.add(book_title);
  46.  
  47.      panel.add(isbn_label);
  48.      panel.add(book_isbn);
  49.  
  50.      panel.add(search);
  51.      ////search.setActionCommand("ISBN");
  52.      search.addActionListener(this);
  53.      return panel;
  54. }
  55. private JPanel createPanel2(){
  56.      JPanel panel = new JPanel();
  57.      panel.add(display_books);
  58.      return panel;
  59. }
  60.  
  61. private JPanel createPanels(){
  62.     JPanel panel = new JPanel();
  63.     //Use default FlowLayout.
  64.     panel.setLayout(new BorderLayout());
  65.     panel.add(createPanel1(), BorderLayout.NORTH);
  66.  
  67.     panel.add(createPanel2(), BorderLayout.SOUTH);
  68.     return panel;
  69. }
  70.   public  LibraryClient(String appType) {
  71.  
  72.     init(appType);
  73.     JFrame frame = new JFrame("Library");
  74.     frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  75.  
  76.     frame.setContentPane(createPanels());
  77.     frame.pack();
  78.  
  79.     frame.setVisible(true); 
  80.   }
  81.  
  82.   public void actionPerformed(ActionEvent e) {
  83.  
  84.         try {
  85.             display_books.setText("");
  86.             resultset=li.getAllBooks();
  87.  
  88.             while(resultset.next()){
  89.  
  90.                 String name = resultset.getString("name");
  91.                    String author =resultset.getString("author");
  92.                    String isbn = resultset.getString("ISBN");
  93.                    display_books.append("\n"+ name + " " + author +" "+ isbn);
  94.              }
  95.         } catch(Exception exc) { exc.printStackTrace(); }
  96.   }
  97.  
  98.   private void init(String type) {
  99.     if (type.equals("JAVA_APP")) li = new library.ejb.LibraryBean();
  100.     else if (type.equals("JAVA_CLIENT")) {
  101.       try {
  102.         InitialContext ic = new InitialContext();
  103.         li = (LibraryInterface) ic.lookup("library.common.LibraryInterface");
  104.       } catch (NamingException e) {
  105.         e.printStackTrace();
  106.       }
  107.     }
  108.   }
  109.  
  110.  
  111.  
  112.   public static void main(String[] args) {
  113.     if (args.length >= 1) new LibraryClient(args[0]);
  114.     else new LibraryClient();
  115.   }
  116.  
  117.  
  118. }
  119.  
Jun 5 '07 #6

Post your reply

Sign in to post your reply or Sign up for a free account.