• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

jdbc basic

 
Rounak Jain
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can someone please explain me the basics of JDBC like --
Why do we need a JDBC driver?
Why we need ant and xalan?
Can someone just point me to a good article or book which explains the need of all this? I just want an overview of how exactly one interacts with a database and why there are so many pre-requisites?
I read "If you want to make a certain set of JAR files (or .class files) available to every Java application on the machine, then your best bet is to add those files to /Library/Java/Extensions."
Should I just add the JDBC driver and xalan folders to /Library/Java/Extensions on a Mac so that they are always available?
My initial reason for getting into JDBC was to be able to write a desktop application which interacts with a database, like an invoicing application. Is JDBC ideal for this or should I focus on something else?
Sorry for these newbie questions.

PS: I am an inter-mediate Filemaker developer. So I have a fair understanding of what relational databases are. I am used to working in a single app for my database and its user front end. I cannot grasp why everything is so complicated with Java.
 
Claude Moore
Ranch Hand
Posts: 828
7
IBM DB2 Java Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You may start reading Oracle tutorial on JDBC usage.

By the way: ant and xalan are not strictly related to jdbc usage: the first is a tool to automate build processes, the second is a Java library for XSLT processing.
And I'm in doubt if copying any kind of jar library file in JRE extension path is really a wise idea.
 
Roel De Nijs
Sheriff
Posts: 10662
144
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The JDBC Database Access Trail of the Oracle's tutorials is an excellent starting point. Happy reading/studying!

Rounak Jain wrote:So I have a fair understanding of what relational databases are. I am used to working in a single app for my database and its user front end. I cannot grasp why everything is so complicated with Java.

It's not complicated at all! You just add the JDBC driver to the classpath of your application and you can access this database and execute queries, update records, insert new records, create tables,...
 
Paul Clapham
Sheriff
Posts: 21566
33
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rounak Jain wrote:My initial reason for getting into JDBC was to be able to write a desktop application which interacts with a database, like an invoicing application. Is JDBC ideal for this or should I focus on something else?


Actually JDBC is essential if you want to access databases from Java. It might be possible to write code which accesses a database directly somehow, but that would constitute reinventing an extremely large wheel. I would heartily dis-recommend that idea.
 
Rounak Jain
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have started reading the Oracle Tutorial for JDBC. On the page http://docs.oracle.com/javase/tutorial/jdbc/basics/gettingstarted.html
The following steps configure a JDBC development environment with which you can compile and run the tutorial samples:

Install the latest version of the Java SE SDK on your computer
Install your database management system (DBMS) if needed
Install a JDBC driver from the vendor of your database
Install Apache Ant
Install Apache Xalan
Download the sample code
Modify the build.xml file
Modify the tutorial properties file
Compile and package the samples
Create databases, tables, and populate tables
Run the samples



and then below on that same page

Modify database-specific properties file

In the properties/javadb-build-properties.xml or properties/mysql-build-properties.xml file (depending on your DBMS), modify the following properties, as described in the following table:
Property Description
JAVAC The full path name of your Java compiler, javac
JAVA The full path name of your Java runtime executable, java
PROPERTIESFILE The name of the properties file, either properties/javadb-sample-properties.xml or properties/mysql-sample-properties.xml
MYSQLDRIVER The full path name of your MySQL driver. For Connector/J, this is typically <Connector/J installation directory>/mysql-connector-java-version-number.jar.
JAVADBDRIVER The full path name of your Java DB driver. This is typically <Java DB installation directory>/lib/derby.jar.
XALANDIRECTORY The full path name of the directory that contains Apache Xalan.
CLASSPATH The class path that the JDBC tutorial uses. You do not need to change this value.
XALAN The full path name of the file xalan.jar.
DB.VENDOR A value of either derby or mysql depending on whether you are using Java DB or MySQL, respectively. The tutorial uses this value to construct the URL required to connect to the DBMS and identify DBMS-specific code and SQL statements.
DB.DRIVER The fully qualified class name of the JDBC driver. For Java DB, this is org.apache.derby.jdbc.EmbeddedDriver. For MySQL, this is com.mysql.jdbc.Driver.
DB.HOST The host name of the computer hosting your DBMS.
DB.PORT The port number of the computer hosting your DBMS.
DB.SID The name of the database the tutorial creates and uses.
DB.URL.NEWDATABASE The connection URL used to connect to your DBMS when creating a new database. You do not need to change this value.
DB.URL The connection URL used to connect to your DBMS. You do not need to change this value.
DB.USER The name of the user that has access to create databases in the DBMS.
DB.PASSWORD The password of the user specified in DB.USER.
DB.DELIMITER The character used to separate SQL statements. Do not change this value. It should be the semicolon character (;).
Modify the tutorial properties file

The tutorial samples use the values in either the properties/javadb-sample-properties.xml file or properties/mysql-sample-properties.xml file (depending on your DBMS) to connect to the DBMS and initialize databases and tables, as described in the following table:
Property Description
dbms A value of either derby or mysql depending on whether you are using Java DB or MySQL, respectively. The tutorial uses this value to construct the URL required to connect to the DBMS and identify DBMS-specific code and SQL statements.
jar_file The full path name of the JAR file that contains all the class files of this tutorial.
driver The fully qualified class name of the JDBC driver. For Java DB, this is org.apache.derby.jdbc.EmbeddedDriver. For MySQL, this is com.mysql.jdbc.Driver.
database_name The name of the database the tutorial creates and uses.
user_name The name of the user that has access to create databases in the DBMS.
password The password of the user specified in user_name.
server_name The host name of the computer hosting your DBMS.
port_number The port number of the computer hosting your DBMS.


To get started with the first requirement,
Property Description
JAVAC The full path name of your Java compiler, javac

This is the relevant code in the properties/mysql-build-properties.xml file is
<property name="JAVAC" value="C:\\Java\\jdk1.7.0\\bin\\javac"/>


I am on Mac OS . My wild guess was to use which javac in terminal (which gives the path /usr/bin/javac) and paste it in the value string. Is that right way to do it?

Secondly, there is no shortcut to all this configuration. Really??
I mean to say, the tutorial was clearly intended for beginners. Was is it just not possible to eliminate or reduce the above steps? I am not complaining. I am also not intending to say that everything must be a one-click setup. I can understand that some of the above steps are merely required because the tutorial was intended for users of both Java DB and MySQL users. I want to know whether this is the way it will be for any project I intend to create. If I imagine myself writing an Invoicing application which uses JDBC and MySQL, would the users of my application have to go through this trouble to get started ?
 
Paul Clapham
Sheriff
Posts: 21566
33
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Are you kidding me? Ant? Just to get some databases set up? I'd recommend looking for some other tutorial.

As for that tutorial being for beginners: I don't really think so, even if it says that. It's more like the description of a two-month course to teach JDBC to people who have a pretty good handle on Java. And all you really need out of it is the "Processing SQL Statements with JDBC". Rowsets and "advanced data types" and stored procedures aren't something that beginners need to know.

So, yeah, I agree with you. Find another tutorial instead of that one. Just one thing: Java tutorials aren't going to teach you how to use your Mac for programming, they're going to assume you know how to use a command line already.
 
Karthik Shiraly
Bartender
Posts: 1210
25
Android C++ Java Linux PHP Python
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here's my simple 4-step tutorial to get you started. It's the simplest possible java desktop application that uses JDBC to create a DB, create a DB schema, insert some data, and read it back.
Use it as a starting point, and search/ask here if you get doubts.

1. Download Apache Derby. Derby is a database product like MySQL, but quite lightweight and ideal for desktop applications as an embedded database.

2. Create a directory somewhere for your application, let's say "~/MyJavaApp/". Extract the download into "~/MyJavaApp/libs/".

3. Write up this simple Swing desktop app. Save it under "~/MyJavaApp/" as SimpleApp.java (note the case; java is case sensitive)


4. Open a terminal and execute it with this command line:


Edit: Changed the code a bit to remove a logical bug.
DB shutdown is now outside the try block that does all the DB operations.
Earlier. shutdown was done inside the try block but all JDBC resources were closed only in finally block, which is not logically correct, even if it's handled properly by the JDBC driver.
 
Roel De Nijs
Sheriff
Posts: 10662
144
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Karthik Shiraly wrote:Here's my simple 4-step tutorial to get you started.

Have a cow for sharing this tutorial!

Just 1 remark: I don't see the real benefit of having a Swing desktop app in this case as you print the content of the table to the console. Why not get rid of the Swing dependencies? Will make the simple tutorial even simpler
 
Karthik Shiraly
Bartender
Posts: 1210
25
Android C++ Java Linux PHP Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Roel De Nijs wrote:
Karthik Shiraly wrote:Here's my simple 4-step tutorial to get you started.

Have a cow for sharing this tutorial!

Just 1 remark: I don't see the real benefit of having a Swing desktop app in this case as you print the content of the table to the console. Why not get rid of the Swing dependencies? Will make the simple tutorial even simpler

Why, thank you for that bovine gift, good Roel! True, it could be even simpler, but I felt showing it in a desktop app will make it easier for the OP to build upon, because OP mentions they'd like to eventually write a desktop app.
 
Roel De Nijs
Sheriff
Posts: 10662
144
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Karthik Shiraly wrote:True, it could be even simpler, but I felt showing it in a desktop app will make it easier for the OP to build upon, because OP mentions they'd like to eventually write a desktop app.

True! But in a JDBC tutorial I would stick to just the JDBC. That way it really focuses on JDBC, no distraction about other (possibly obscure) code. And in a Swing tutorial I would (obviously) use Swing (and AWT), but don't throw in some JDBC (for the very same reason). But the Swing code in the above code snippet is fairly easy and straightforward. It was nothing more but a suggestion. I love KISS a lot
 
Rounak Jain
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you very much for the effort. Very helpful.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic