473,326 Members | 2,147 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,326 software developers and data experts.

jdbc with mysql

oll3i
679 512MB
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
5 2007
oll3i
679 512MB
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
r035198x
13,262 8TB
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
679 512MB
so what am i doing wrong?
Jun 5 '07 #4
r035198x
13,262 8TB
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
679 512MB
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

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

Similar topics

1
by: Ron | last post by:
this code is giving me an excption and I cannot tell why could someone who is familiar with jdbc help me out, I am using mysql with jdbc, although the exception does not look like it is getting far...
0
by: Nils Valentin | last post by:
Hi MySQL Fans ;-), Is it possible that the 3.08 series allows to connect to 4.0.14 versions but not to the 4.1 alpha-versions ? I get belows error when tryig to connect from DbVisualizer which...
0
by: sridhar nagabhurshana | last post by:
hello I am trying to connect to mysql database using connector/J jdbc driver this is my code import java.sql.Connection; import java.sql.DriverManager; import java.sql.SQLException;
0
by: Robert Mazur | last post by:
MySQL 5.0 alpha (binary install) on Solaris 9 -or- RedHat 8.0 mysql-connector-java-3.0.8-stable ----------------------- Is there something different going on with JDBC and the alpha version...
4
ak1dnar
by: ak1dnar | last post by:
I have downloaded and copied the this Connector/j to lib directory of my java installation dir. and this Jsp page is working with that. <%@ page language="java" import="java.sql.*" %> <%...
13
by: delram | last post by:
Hello All! I've been struggling with this problem for a while. I have a database from which I need to read and display some data on a browser. (The database is set up for remote access). ...
2
by: bevis | last post by:
I'm new to sql server and mysql but this seems like it should be a pretty straight forward jdbc connection. But I have spent almost 2 days just trying to get a jdbc connection. Please help if you...
6
by: IvanJ2EE | last post by:
Hello partners. I have an issue using jdbc and a DB in MySQL. The problem is that I want to make a seach using the Last Name of a person stored in a MySQL DB, from a JSP The problem is that I...
0
by: neuraljay | last post by:
hi everyone. I am trying to build a jsp using webwork, hibernate and mysql. And right now I am doing its unit testing. Everytime I run the test I am getting this error: Error JDBC exception on...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.