473,703 Members | 3,355 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

jdbc with mysql

oll3i
679 Contributor
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 2039
oll3i
679 Contributor
when i run it from bat i get


D:\JAVA_P~1\ZAD 5_K~1\JAVACL~1> runClient.bat

D:\JAVA_P~1\ZAD 5_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.No InitialContextE xception: 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.fac tory.initial
at javax.naming.sp i.NamingManager .getInitialCont ext(NamingManag er.java:6
45)
at javax.naming.In itialContext.ge tDefaultInitCtx (InitialContext .java:288
)
at javax.naming.In itialContext.ge tURLOrDefaultIn itCtx(InitialCo ntext.jav
a:325)
at javax.naming.In itialContext.lo okup(InitialCon text.java:392)
at library.client. LibraryClient.i nit(LibraryClie nt.java)
at library.client. LibraryClient.< init>(LibraryCl ient.java)
at library.client. LibraryClient.m ain(LibraryClie nt.java)
java.lang.NullP ointerException
at library.client. LibraryClient.a ctionPerformed( LibraryClient.j ava)
at javax.swing.Abs tractButton.fir eActionPerforme d(AbstractButto n.java:19
95)
at javax.swing.Abs tractButton$Han dler.actionPerf ormed(AbstractB utton.jav
a:2318)
at javax.swing.Def aultButtonModel .fireActionPerf ormed(DefaultBu ttonModel
.java:387)
at javax.swing.Def aultButtonModel .setPressed(Def aultButtonModel .java:242
)
at javax.swing.pla f.basic.BasicBu ttonListener.mo useReleased(Bas icButtonL
istener.java:23 6)
at java.awt.Compon ent.processMous eEvent(Componen t.java:6038)
at javax.swing.JCo mponent.process MouseEvent(JCom ponent.java:326 0)
at java.awt.Compon ent.processEven t(Component.jav a:5803)
at java.awt.Contai ner.processEven t(Container.jav a:2058)
at java.awt.Compon ent.dispatchEve ntImpl(Componen t.java:4410)
at java.awt.Contai ner.dispatchEve ntImpl(Containe r.java:2116)
at java.awt.Compon ent.dispatchEve nt(Component.ja va:4240)
at java.awt.Lightw eightDispatcher .retargetMouseE vent(Container. java:4322
)
at java.awt.Lightw eightDispatcher .processMouseEv ent(Container.j ava:3986)

at java.awt.Lightw eightDispatcher .dispatchEvent( Container.java: 3916)
at java.awt.Contai ner.dispatchEve ntImpl(Containe r.java:2102)
at java.awt.Window .dispatchEventI mpl(Window.java :2429)
at java.awt.Compon ent.dispatchEve nt(Component.ja va:4240)
at java.awt.EventQ ueue.dispatchEv ent(EventQueue. java:599)
at java.awt.EventD ispatchThread.p umpOneEventForF ilters(EventDis patchThre
ad.java:273)
at java.awt.EventD ispatchThread.p umpEventsForFil ter(EventDispat chThread.
java:183)
at java.awt.EventD ispatchThread.p umpEventsForHie rarchy(EventDis patchThre
ad.java:173)
at java.awt.EventD ispatchThread.p umpEvents(Event DispatchThread. java:168)

at java.awt.EventD ispatchThread.p umpEvents(Event DispatchThread. java:160)

at java.awt.EventD ispatchThread.r un(EventDispatc hThread.java:12 1)

D:\JAVA_P~1\ZAD 5_K~1\JAVACL~1>
Jun 4 '07 #2
r035198x
13,262 MVP
when i run it from bat i get


D:\JAVA_P~1\ZAD 5_K~1\JAVACL~1> runClient.bat

D:\JAVA_P~1\ZAD 5_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.No InitialContextE xception: 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.fac tory.initial
at javax.naming.sp i.NamingManager .getInitialCont ext(NamingManag er.java:6
45)
at javax.naming.In itialContext.ge tDefaultInitCtx (InitialContext .java:288
)
at javax.naming.In itialContext.ge tURLOrDefaultIn itCtx(InitialCo ntext.jav
a:325)
at javax.naming.In itialContext.lo okup(InitialCon text.java:392)
at library.client. LibraryClient.i nit(LibraryClie nt.java)
at library.client. LibraryClient.< init>(LibraryCl ient.java)
at library.client. LibraryClient.m ain(LibraryClie nt.java)
java.lang.NullP ointerException
at library.client. LibraryClient.a ctionPerformed( LibraryClient.j ava)
at javax.swing.Abs tractButton.fir eActionPerforme d(AbstractButto n.java:19
95)
at javax.swing.Abs tractButton$Han dler.actionPerf ormed(AbstractB utton.jav
a:2318)
at javax.swing.Def aultButtonModel .fireActionPerf ormed(DefaultBu ttonModel
.java:387)
at javax.swing.Def aultButtonModel .setPressed(Def aultButtonModel .java:242
)
at javax.swing.pla f.basic.BasicBu ttonListener.mo useReleased(Bas icButtonL
istener.java:23 6)
at java.awt.Compon ent.processMous eEvent(Componen t.java:6038)
at javax.swing.JCo mponent.process MouseEvent(JCom ponent.java:326 0)
at java.awt.Compon ent.processEven t(Component.jav a:5803)
at java.awt.Contai ner.processEven t(Container.jav a:2058)
at java.awt.Compon ent.dispatchEve ntImpl(Componen t.java:4410)
at java.awt.Contai ner.dispatchEve ntImpl(Containe r.java:2116)
at java.awt.Compon ent.dispatchEve nt(Component.ja va:4240)
at java.awt.Lightw eightDispatcher .retargetMouseE vent(Container. java:4322
)
at java.awt.Lightw eightDispatcher .processMouseEv ent(Container.j ava:3986)

at java.awt.Lightw eightDispatcher .dispatchEvent( Container.java: 3916)
at java.awt.Contai ner.dispatchEve ntImpl(Containe r.java:2102)
at java.awt.Window .dispatchEventI mpl(Window.java :2429)
at java.awt.Compon ent.dispatchEve nt(Component.ja va:4240)
at java.awt.EventQ ueue.dispatchEv ent(EventQueue. java:599)
at java.awt.EventD ispatchThread.p umpOneEventForF ilters(EventDis patchThre
ad.java:273)
at java.awt.EventD ispatchThread.p umpEventsForFil ter(EventDispat chThread.
java:183)
at java.awt.EventD ispatchThread.p umpEventsForHie rarchy(EventDis patchThre
ad.java:173)
at java.awt.EventD ispatchThread.p umpEvents(Event DispatchThread. java:168)

at java.awt.EventD ispatchThread.p umpEvents(Event DispatchThread. java:160)

at java.awt.EventD ispatchThread.r un(EventDispatc hThread.java:12 1)

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

It says NoInitialContex tException meaning your beans are never found anyway.
Jun 5 '07 #3
oll3i
679 Contributor
so what am i doing wrong?
Jun 5 '07 #4
r035198x
13,262 MVP
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 Contributor
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
2352
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 enough for that to matter. Statement stmt = PoolDB.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_UPDATABLE); ResultSet Picks = stmt.executeQuery("SELECT * FROM picks where week="+week); // get all the cloums and...
0
2262
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 was working fine until a while ago. I haven't watched it recently, and haven't changed anything I am aware off right now. After I realized the problem I tried many 3.08 versions with the same symptom. Watch were it says "Unknown system variable...
0
2563
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
4242
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 of MySQL 5.0? When trying to connect I am getting: ** BEGIN NESTED EXCEPTION **
4
2727
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.*" %> <% String driver = "org.gjt.mm.mysql.Driver"; Class.forName(driver).newInstance();
13
2439
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). I'm using the following JSP/JDBC code to do that. ---------------------------------------------------------------------------------------------- Class.forName ("com.mysql.jdbc.Driver").newInstance();
2
4552
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 can. I'm using Eclipse 3.2.2 and I have installed mysql-connector-java-5.0.6-bin.jar. I am trying to connect to SQL Enterprise Manager version 8.0 innstall on a Windows 2003 Server Enterprise Edition. I have been able to successfully test...
6
1625
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 cannot determine which sentence to use for this purpose, that can help me to display the information of the person who match with the search criteria. This is my actual Servlet. import java.io.IOException;
0
6515
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 Hibernate data access; nested exception is org.hibernate.exception.GenericJDBCException: Could not execute JDBC batch update org.springframework.orm.hibernate3.HibernateJdbcException: JDBC exception on Hibernate data access; nested exception is...
0
8662
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9109
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9002
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
1
6585
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5922
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4420
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
3113
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2434
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2057
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.