How do I establish database connectivity between MySQL and Eclipse Java?
Lots of ways:
Hibernate
EJB (make sure you go with version 3!)
Roll your own solution with JDO/JDBC
And I'm sure there are plenty of other ways to do it as well.
You need a JDBC driver for MySQL in your application classpath which then registers automatically with DriverManager.
You can then use the techniques described in http://java.sun.com/docs/books/tutorial/jdbc/basics/connecting.html except you need to have the connection string (starting with jdbc:...) adapted to your precise scenario. That is described in the documentation for the JDBC driver you choose to use.
There are plenty of jdbc tutorials if you want to build your own solution. MYsql has a Using MySQL With Java page that has links to tutorials, presentations, blogs etc. The first tutorial looks like it covers everything from installing the database to accessing it from the java program.
Related
Are there any other means of connecting java applications to DB other than JDBC? Like when we use Hibernate in our java apps, i believe Hibernate internally used JDBC mechanism to eventually connect to DB.
So my question is that is JDBC the way in which we connect to DB?
Thanks
In general, JDBC is practically always used to connect to SQL databases, either directy or under the hood by an ORM. NoSQL-Databases use other APIs.
In my opinion, JDBC would be the first choice so really would not be seeking alternatives. That said, one could use the JDBC-ODBC bridge. Your app is still written to JDBC but the connection to the DB is with an ODBC driver. I would much prefer the fully Java approach of a JDBC driver. Depending on what database you want to access, they may have Java libraries that expose their native API and you could write to that. Bur, your code would be completely non portable to other database if you ever wanted to expand. A well written JDBC app should be able to access different databases simply by plugging in another JDBC driver and adding the appropriate connection information. Also, if you needed an assistance later on with JDBC being fairly widely used it will be easier to get than assistance than with a native solution.
I am left wondering why you're searching for alternatives to JDBC though.
JDBC is a standard all ORM tools need to follow while connecting to DB using Java
I would like to use Derby from apache in order to include database management on a desktop application that I'm currently developing.
Unfortunately I figured out that they have stopped developing this plugin and all the tutorials I've read online refer to this plugin.
So my question is, how can I use the latest version of Derby on eclipse? What steps should I follow? I would like my application to be standalone and easily transferable.
thanks in advance
Apache Derby is just like other databases that provide interface through JDBC. You don't need a specific plugin to connect with Derby. Just use the usual JDBC routines.
Basic steps:
If you want to use the latest Derby version, first you need to download it from here
You need to include the jar in your classpath.
You just need to make connections and perform queries using JDBC, as better explained at the Oracle JDBC Tutorial.
Ditto pram.
I'm using Derby in a Java app I'm developing with Eclipse, and I didn't know there was a plug-in. I connect to Derby like I would to any other database. I use the Derby command-line interface to manage the tables. \
(One feature of other db's I would have liked to see in Derby: having SQL extensions to display db metadata, like MySql's "show" and "describe" commands. Derby has these in the command-line interface, but they're apparently implemented there rather than in the SQL engine itself, so you can't use them outside of the CLI. Bummer. When I'm developing, I often build an ad hoc query screen into the app that lets me type in and execute arbitrary queries. (And no, I don't deploy this to public websites.) But I digress.)
I don't know what the plug-in does for you. I've never used an Eclipse plug-in for any database.
The plug-in is still available in source form, and instructions for building it are available on the Derby website: http://svn.apache.org/viewvc/db/derby/code/trunk/plugins/eclipse/Readme.txt?view=markup
From time to time the instructions for building the plug-in change; you might wish to check into the status of this issue for further updates: https://issues.apache.org/jira/browse/DERBY-5272
I have been asked to learn JDBC. I do not know where to start. I have started with some books. It's using some SQL tables (I am also new to SQL). So I want any of you to assist me that the topics that should be covered for JDBC. I also want to know how to creae a simple DB in Windows, so that database will be connected to my program.
Which books, materials are useful to know for:
Java Beginners
JDBC
Connection between Java & JDBC (I do not know whether its right or not)
Relation between Java & SQL.
Java Beginners
Head to Sun Oracle's own tutorials.
The Java Tutorials - Getting started
The Java Tutorials - Learning the Java language
The Java Tutorials - Essential Classes
The Java Tutorials - Collections
JDBC
Again, the vendor's own tutorials are the best (only learn it after basic Java!):
The Java Tutorials - JDBC basics
You'd like to learn SQL first beforehand (learn it before JDBC):
W3Schools - SQL tutorial
Connection between Java & JDBC (I do not know whether its right or not)
JDBC is a Java API (a set of interfaces and classes). There is no such thing as "connect between Java and JDBC". You rather want to connect between Java and the database. You use JDBC for this. You first need to know the JDBC URL of the database in question and the login username and password. Then you can connect it like follows:
Connection connection = DriverManager.getConnection(url, username, password);
How the URL should look like depends on the DB and JDBC driver in question. You'd like to consult the documentation of the JDBC driver in question.
Relation between Java & SQL.
Nothing. Both are separate and independent languages each with an own purpose. SQL is a database communication language. Java is an object oriented programming language. In Java, you can use the JDBC API to execute SQL programmatically, but that's also really all.
I also want to know how to creae a simple DB in Windows, so that database will be connected to my program
Just choose a database server and download and install it. There are several popular choices:
MySQL - JDBC driver
PostgreSQL - JDBC driver
Oracle - JDBC driver
MS SQL Server - JDBC driver (jTDS JDBC driver is better)
IBM DB2 - (no public JDBC driver, it's included in DB2 install folder)
Each of them also ships with DB vendor specific JDBC documentation. It's also worth to get yourself through it (only if you already understand basic JDBC!).
See also:
JDBC tutorial with MySQL
DAO tutorial with MySQL
Start by focusing on accessing a Database using SQL.
JDBC just lets your write Java code to call the SQL, so you need to understand SQL first, and to understand SQL you need to understand Databases a little: Tables, Columns, Keys.
So you could work through a tutorial such as this link text.
I have thought of creating my first database application for one of my projects using Oracle and Java. I have chosen Netbeans as my development environment. I have a few questions to getting started.
This will be a data intensive (yet still for a college project) database application. I do not need 1000 user concurrency or any other very advanced features but basic stuff such as triggers, stored procedures etc. Will the 11g "Express" (XE) suffice for my requirements?
Do i need any Java to Oracle bridge (database connectivity driver eg. ODBC etc) for Netbeans to connect to the oracle database? If yes, what are they? Does Netbeans support Oracle databases natively?
Any easy to follow guide on how do I connect to the database and insert/retrieve/display data on a J2SE application? (I know that i should Google this but if there's any guide previously followed by anyone and is considered easy, it would be greatly appreciated.)
There are several different ways to access databases using Java. I'm assuming you are wanting to use JDBC, which is included in all recent JDK's. There are other layers on top of JDBC like Hibernate that may make things cleaner for larger applications, but may also be too steep a learning curve if you have a project to complete and submit.
To answer your questions in order:
I think it's highly likely that Oracle 10g Express Edition will do what you need for a college project. It's pretty much the 10g Standard Edition with a 4GB limit on data size.
You will need a JDBC driver to access the database from Java. It comes with XE, and is installed in <XE client install dir>/jdbc/lib/ojdbc14.jar
Sun have an introduction to JDBC here.
I'm new to java development, and was happy to see how much easier the database implementation was when it comes to supporting several platforms, compared to the php environment I'm used to.
There is, however, one thing I'm confused about - I read everywhere I have to do a runtime-include of the database driver I want to use, ie:
Class.forName(com.example.jdbc.Driver).newInstance();
However, omitting this seems to work fine too - so my question is, does jdbc find the driver, given the server url, automagically? And if so, why is this line included in all the tutorials i read on the subject?
Also - if anyone's got any good tips for online java learning resources (enterprise development in particular), please share!
Yes, this has improved in JDK 6. See this O'Reilly article for JDBC 4.0 improvements.
In particular:
In JDBC 4.0, we no longer need to
explicitly load JDBC drivers using
Class.forName(). When the method
getConnection is called, the
DriverManager will attempt to locate a
suitable driver from among the JDBC
drivers that were loaded at
initialization and those loaded
explicitly using the same class loader
as the current application.
See also the JavaDoc for DriverManager in JDK 6.
Didn't this change in JDK 6?