• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Tim Cooke
  • Devaka Cooray
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Rob Spoor
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Piet Souris
  • Mikalai Zaikin
Bartenders:
  • Carey Brown
  • Roland Mueller

is exist any multi record catalogue? or better to use DB? or array?

 
Ranch Hand
Posts: 102
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Dear All,
Im trying to write a book trading project. where buyer and sellers are trading some books. here, I need to use a catalogue (or sth like that)to save the list of available books with their details in it . like for examle an array for book : "JAVA PROGRAMMING BOOK", 100,10 ,25,5 ,7,2. which shows book title, book price, discount rate, quality, quality rate and deadline, deadline rate respectively. I saw in one example that they use Hashtable catalogue to build a catalogue for saving all the book in it. and then retriving information from it. But they use just 2 record like "JAVA POGRAMMING BOOK", "100" represent book title and price. can I use a kind of catalogue which can held more records for me? or I should use an array?

Thank you,
Sahar
 
Marshal
Posts: 79699
381
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Welcome to JavaRanch

Is this a college assignment or something for real-life use?

You should probably be using HashMap rather than Hashtable. Remember a Map contains a Key and a Value, paired off; the Value can be any sort of object which can have lots of fields, you you can have price, discount, etc fields.
If you have an array, how are you going to retrieve a book?
But I suspect you really need a database. I shall see what other people say, and might transfer this thread to another forum later.
 
sahar sa
Ranch Hand
Posts: 102
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
wow! thank you Ritchie!
fisrt,its my project for my degree! second, I dont know if i have to use DB or not? actually I have a table like data set but is not that much long, it has about 30 book with their details inside. more important thing is to retrive information cuz actually we just add information 1 time but retrive it several times! so, is there any advantageas of using DB rather than catalogue?

Thanks,
Sahar.
 
Campbell Ritchie
Marshal
Posts: 79699
381
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
How much database programming do you know? How much time do you have for this project? Have you been given any guidance what sort of applications to use? Those questions really should give you an answer.

In a database it is very easy to have a "quantity_in_stock" column, linked to a book. Then you can simply reduce the number by 1 whenever you sell a book. A 30-book table can be expanded to a 30000-book table by (would you believe) adding 29970 books! And it will work just as well. But a 30-member Map can be expanded to a 30000-member Map and still work.

Have a word with your supervisor. I can't speak for your module, but if were I setting it, I would give better marks to somebody who successfully implemented a link to databases. But I shall repeat the crucial bit of advice:




Have a word with your supervisor.
 
sahar sa
Ranch Hand
Posts: 102
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Dear,
sure, I make an appointment with him. I had try some examples of hashmap, sounds simple and enough usefull for me. but may I know that usually by using such these small datasets using DB is more efficient or hashMap?? (especialy inc concern with time)?
thank you soooo much,
Sahar.
 
Bartender
Posts: 4116
72
Mac TypeScript Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

sahar sa wrote:I had try some examples of hashmap, sounds simple and enough usefull for me. but may I know that usually by using such these small datasets using DB is more efficient or hashMap?? (especialy inc concern with time)?



It all depends I think. Using Maps etc... only going to keep the data till the application runs, then all vanished. That's why you need to persist (store) the data (in a database/file etc..) to be used later. Of course as Campbell said using the database for storing the data will give you more marks (and that will add a bit more work) than using in memory data structure.
 
Bartender
Posts: 10336
Hibernate Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Efficiancy is not the reason you would choose an in-memory data structure over a database. I'd repeat what Cambpell suggests; often assignments use the term catalogue to just mean an in-memory structure so you can learn about Collections. have a quick word with whoever set the assignment, find out wha they are after (it could be that a database is over kill within the remit of your assignment).
 
sahar sa
Ranch Hand
Posts: 102
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I like this forum.
thank you all!!!
 
sahar sa
Ranch Hand
Posts: 102
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
actually it's not assignment, its my master project. and the only thing that my advisor is consern about is time fficiency and performance,.. !
Thnx,
Sahar.
 
Paul Sturrock
Bartender
Posts: 10336
Hibernate Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

sahar sa wrote:actually it's not assignment, its my master project. and the only thing that my advisor is consern about is time efficiency and performance,.. !
Thnx,
Sahar.



... and not whether the data is persisted? An odd requirement, but if this is the case don't bother with a database.
 
Campbell Ritchie
Marshal
Posts: 79699
381
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

sahar sa wrote:actually it's not assignment, its my master project . . .
Thnx,
Sahar.

That is rather like an assignment . . .

Please to be able to help
 
sahar sa
Ranch Hand
Posts: 102
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Dear,
I find that I can use JDBC to connect to a database. But I wanna know usually which version of SQL should I use for programming with java in this case?
I checked www.mysql.com there is a long list of sql versions. Im confused which one is suitable for me?
ttank you all,
Sahar.
 
Campbell Ritchie
Marshal
Posts: 79699
381
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Get whatever is described on the MySQL website as the most recent stable version. Remember some things, eg views, are only available in the more recent versions of MySQL.
 
sahar sa
Ranch Hand
Posts: 102
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi all,
Im trying to make a connection between java and mysql through JDBC, but it fail to connect to DB, is some thing wrong in this code?
database name: "booktrading" and table name: "tseller"

try{
Class.forName("com.mysql.jdbc.Driver");

String url = "jdbc:mysql://localhost/booktrading";
String user = "root";
String pw = "444";
Connection con = DriverManager.getConnection(url,user,pw);

Statement s = con.createStatement();
String select = "Select title, price"+"from tseller order by price";
ResultSet rows = s.executeQuery(select);
while(rows.next())
{
String title = rows.getString("title");
int price = rows.getInt("price");
System.out.println("the table shows: ");
System.out.println(title + " : " + "(" + price+ ")");
}
}
catch (ClassNotFoundException e)
{
System.out.println(e.getMessage());
System.out.println("failed to connect to DB");
System.exit(0);
}
catch(SQLException e)
{
System.out.println(e.getMessage());
System.exit(0);
}


thnx,
Sahar.
 
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
There can be many possible errors in the posted code.

1) mysql-jdbc-connector jar not present in classpath.

2)
doesn't have a space between *price* and *from*, where your query will fail...

and so on...

Please post the error you get.
Use printStackTrace() method to print stack trace of exceptions. It will help you locate the exact problem.
 
sahar sa
Ranch Hand
Posts: 102
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Dear ,
I cheked the classpath is right, I used printStacktTrace() and the below errors appear:

java.lang.ClassNotFoundException: com.mysql.jdbc.Driver
com.mysql.jdbc.Driver
at java.net.URLClassLoader$1.run(URLClassLoader.java:200)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:188)
at java.lang.ClassLoader.loadClass(ClassLoader.java:306)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:276)
failed to connect to DB
at java.lang.ClassLoader.loadClass(ClassLoader.java:251)
at java.lang.ClassLoader.loadClassInternal(ClassLoader.java:319)
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Class.java:169)
at cataloguetest.Main.main(Main.java:72)

thnx,
Sahar.
 
sahar sa
Ranch Hand
Posts: 102
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
But something else:
I use these two lines as was explained in my book(and most of examples i find ):
Class.forName("com.mysql.jdbc.Driver");
String url = "jdbc:mysql://localhost/booktrading";
How can I find what is my server name? i just put "localhost" as it was in the examples.
may it cuz the problem?
thnx,
sahar.
 
Campbell Ritchie
Marshal
Posts: 79699
381
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Find the handbook for whichever version of MySQL you have (here is 5.1) the find the section about connectors, the subsection about Java, and follow the instructions given to the letter. The tiniest error and it won't work. Remember to include wherever you have your Connector in the classpath.
If you have the database on the same computer you would usually use "localhost" as your server name.

Since this thread is now principally about JDBC, I shall move it there.
 
Narendira Sarma
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

sahar sa wrote:
java.lang.ClassNotFoundException: com.mysql.jdbc.Driver
com.mysql.jdbc.Driver



Your stacktrace clearly suggests that the mysql-jbdc-connector jar is not in the classpath.
 
sahar sa
Ranch Hand
Posts: 102
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Dear,
I check but still it throws some exception I can not find what is wrong in this code? and what could be the reason?

here is my code:
try{
Class.forName("com.mysql.jdbc.Driver");

Connection con= DriverManager.getConnection("jdbc:mysql://localhost/booktrading?"+"user=root&password=444");
Statement s = con.createStatement();
String select = "Select title, price"+" from tseller order by price";
ResultSet rows = s.executeQuery(select);
while(rows.next())
{
String title = rows.getString("title");
int price = rows.getInt("price");
System.out.println("the table shows: ");
System.out.println(title + " : " + "(" + price+ ")");
}
}
catch (ClassNotFoundException e)
{
System.out.println(e.getMessage());
e.printStackTrace();
System.out.println("failed to connect to DB");
System.exit(0);
}
catch(SQLException ex)
{
System.out.println("SQLException: " + ex.getMessage());
System.out.println("SQLState: " + ex.getSQLState());
System.out.println("VendorError: " + ex.getErrorCode());
}

and here the errors:
run:
com.mysql.jdbc.Driver
java.lang.ClassNotFoundException: com.mysql.jdbc.Driver
at java.net.URLClassLoader$1.run(URLClassLoader.java:200)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:188)
at java.lang.ClassLoader.loadClass(ClassLoader.java:306)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:276)
failed to connect to DB
at java.lang.ClassLoader.loadClass(ClassLoader.java:251)
at java.lang.ClassLoader.loadClassInternal(ClassLoader.java:319)
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Class.java:169)
at cataloguetest.Main.main(Main.java:73)

thank you !
Sahar.
 
Campbell Ritchie
Marshal
Posts: 79699
381
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Which directory did you put the jar with the connection classes in? Have you included it in your classpath? You can find a bit about the classpath options for the java tool here
 
sahar sa
Ranch Hand
Posts: 102
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Dear,
It works now! and the problem was I DIDN'T KNOW I SHOULD ADD THE JAR FILE INTO LIBRARY IN ADDITION TO CLASSPATHS.
thak you so much!
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

sahar sa wrote:I DIDN'T KNOW I SHOULD ADD THE JAR FILE INTO LIBRARY IN ADDITION TO CLASSPATHS.


Is this a web application, or a normal stand-alone Java application? If it's a normal stand-alone Java application, putting the JAR file in the classpath is enough. You should not need to put the JAR file into a library (whatever you mean exactly by that).
 
sahar sa
Ranch Hand
Posts: 102
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Dear,
I need to declare an interval variable in mysql between [0,100], It can not be double data type cuz it would be 78.65 and can not be integer cuz it includes decimal precision. what is your data type suggestion?
thnx!
Sahar.
 
Paul Sturrock
Bartender
Posts: 10336
Hibernate Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


It can not be double data type cuz it would be 78.65 and can not be integer cuz it includes decimal precision. what is your data type suggestion?



What? Are you asking for a type of number that includes stuff after the decimal point but is not a floating point number?
 
BWA HA HA HA HA HA HA! Tiny ad:
We need your help - Coderanch server fundraiser
https://coderanch.com/wiki/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic