PDA

View Full Version : JDBC Quick Start




ellias2007
Jan 27, 2009, 08:30 AM
Hi All,
i am Dr. ellias from canada,
i am expert in Graphic design and use usually Adobe software : Photoshop, Premiere, After Effect, InDesign ... and 3D Software and others....
i Teache also in my university Java language (for beginners) ...
i am now about learning how to build a Complete Database Application...
Many of my collegues here in the University Use Visual Basics and advise me to learn and use it.... But,
Since i Know Java Language and teache it (only General Java : the logic-loops-classes-object oriented- and so), and also LOVE it and find it nice and strong, i prefer to use it in this target ....
i am asking about the way to QUICK LEARN how to build a Complete Database Application using Jdbc-Sql ....
i am an expert in many kinds of softwares ... BUT in the level ZERO in the domain of Database-jdbc-connection ....

in fact, what i must to do to start and get a Jdbc driver and a software handeling a DataBase??
where i must write the code ??
How to compile it ??
i am Running MacOS 10.5 intel, what are the tools i must to have in order to Start Physically the work...

PLEASE HELP ME to quick learn and use Jdbc and its Family
Many Thanks
Dr. Ellias



MacRohde
Jan 27, 2009, 08:33 AM
Check this out: JDBC tutorial (http://java.sun.com/docs/books/tutorial/jdbc/index.html)

ellias2007
Jan 28, 2009, 12:15 PM
Dear, MacRohde

What i neeeeeeeed is a Help in the following :
--> The Free Tool where i write the code and compile it
--> The Free Tool where i write the database and save and access it...
--> How to connect to this Database from the JavaTool ...
--> Where to set All the parts of the stuff (Control Panel?, Extension?,...)

All the following stuffs are clear for me and i DONT need any help...
--> Using the SQL statements
--> Writing Java Code
--> Making GUI using Swing or others
--> Access The Database (Select-Update-Delete-Insert.....)
Because i learned All these theorically ....

Simply, What i need is a help how to practice these theories ..
i need some one to write me a help like this :
1) Go to www .XXX and download XXX and write your code here.
2) Go to www .XXX and download XXX and Make your DataBase here.
3) Open XXX(controlPanel?? other?? i dont know what...) in your Mac, and in order to set the connection do the following : XXX, YYY, ZZZ ...
4) ...
5) ....
And so on ...

in fact All my freinds here are Ms Windows user... and i am the only MAC user ... they use VB.net with sqlserver with a lot of available tools for their OS ... so i need only a push-up in the Software levels ...
Many Thanks

MacRohde
Jan 28, 2009, 12:55 PM
Dear, MacRohde

What i neeeeeeeed is a Help in the following :
--> The Free Tool where i write the code and compile it
--> The Free Tool where i write the database and save and access it...
--> How to connect to this Database from the JavaTool ...
--> Where to set All the parts of the stuff (Control Panel?, Extension?,...)



Well, that's the easy part.

1. For that you use your general Java IDE, fx. Eclipse, JBuilder, or IDEA IDE.

2. MySQL is used a lot with Java. So go to mysql.com and download the free community edition of MySQL database server and install it on your Mac.

3. To connect to the database you simply supply a proper connection string to JDBC.

4. Not sure what you mean.

AlmostThere
Jan 28, 2009, 05:29 PM
For a tool, you could try AquaDataStudio (ADS) from here: http://www.aquafold.com/

It is not free but there are academic discounts and a 30 day demo.
Click create new connection, select SQL Server, enter the user and server credentials. You then have a graphical tool for creating and editing tables, importing data etc. and also for developing, testing and optimising SQL. It works with just about every database out there.

If you install MySQL, you will have a System Preferences pane for starting and stopping the database and all the command line tools. You can connect to this through AquaDataStudio, as above, selecting MySQL as the database type.

If you want to develop directly in Java and will need to download the JDBC drivers as jar files. You will actually be able to find these in the ADS app folder or find the MySQL driver here http://dev.mysql.com/downloads/connector/j/5.1.html and an SQL Server driver http://msdn.microsoft.com/en-us/data/aa937724.aspx or here http://jtds.sourceforge.net/ . You simply need to put these jar files in your classpath.

Once you have installed MySQL (I don't have SQL Server to hand to check the code) and started it through the System Pref. Pane, open up a Terminal and type the following:


$mysql -u root
mysql> show databases;


If there is a database called test, ignore the next step, or you will see ERROR 1007 (HY000): Can't create database 'test'; database exists

mysql> create database test;


Then the following:

mysql> use test;
mysql> create table EXAMPLE_TABLE ( id integer, name varchar(100) );
Query OK, 0 rows affected (0.38 sec)

mysql> insert into EXAMPLE_TABLE values (1, 'Hello'), (2, 'Goodbye');
Query OK, 2 rows affected (0.37 sec)
Records: 2 Duplicates: 0 Warnings: 0


This creates a table and inserts two sample rows.

To simply connect and run a query, fire up your java IDE and enter this:

import java.sql.*;

public class ConnectToMySQL {

static {
try {
Class.forName("com.mysql.jdbc.Driver");
} catch (ClassNotFoundException cnfe) {
cnfe.printStackTrace();
System.exit(-1);
}
}

public static void main(String [] args) {

final String username = "root";
final String password = "";
final String url = "jdbc:mysql://localhost:3306/test";

final String query = " SELECT * FROM EXAMPLE_TABLE ";

try {
Connection con = DriverManager.getConnection(url, username, password);
Statement stmnt = con.createStatement();

ResultSet rs = stmnt.executeQuery(query);
while (rs.next()) {
int id = rs.getInt("id");
String name = rs.getString("name");
System.out.println("Row is id:" + id + ", name:" + name);
}

} catch (SQLException sqle) {
sqle.printStackTrace();
System.exit(-1);
}
}
}


Make sure the file mysql-connector-java-5.0.4-bin.jar file is in your classpath (or whichever version you have).

You should get some output like this:

Row is id:1, name:Hello
Row is id:2, name:Goodbye

You should be able to diagnose any problems from the stack trace.

ellias2007
Jan 31, 2009, 12:16 PM
Hi Mr. MacRohde

> Well, that's the easy part.

U r right ... this is the easy part, but it is the ambiguous part for me...


> 1. For that you use your general Java IDE, fx. Eclipse, JBuilder, or IDEA IDE.

... ok, since i used Jbuilder 4 years ago... i searched a free version on the MAC ... i did not find...:confused:

> 2. MySQL is used a lot with Java. So go to mysql.com and download the free community edition of MySQL database server and install it on your Mac.

Ok... i did ... i went to :
http://dev.mysql.com/downloads/mysql/5.1.html
i found versions running on Power PC only .... i am using an INTEL iMac ... i downloaded on of them (Mac OS X 10.5 (PowerPC, 64-bit)) ... i can not install it ...:rolleyes:

> 3. To connect to the database you simply supply a proper connection string to JDBC.

> 4. Not sure what you mean.

i mean : how to do the connection NOT in the level of writing the code ... But in the level of setting the physical part : a control panel - physical files - ....
thanks

ellias2007
Jan 31, 2009, 12:24 PM
Hi Mr. AlmostThere and all other freinds...
In fact, i am now in the following step :
1) i Downloaded SQLEditor
(http://www.malcolmhardie.com/sqleditor/releases/1.4.1/SQLEditor-1-4-1.dmg)
and maked a small database (2 tables for trying)...
i tried to fill some records (Rows)... i can not find where to enter the Data ...
i think that i set only the DataBase there and then enter Records from the Java Program itself ...true??

2) i downloaded NetBeans and i try now to connect to the DataBase i made ...
This is My problem now :
a> WHERE to connect and HOW ???
b> When i write : Class.forName("What i must to put here????");
c> is there a driver i must to download???

All the tutorial i read, give helps for Ms.Windows user !!!! Please Mac experts help me Please ...

Many Thanks for any clear Help

eddietr
Jan 31, 2009, 05:28 PM
Did you find the intel build of mysqld?

Just a tip, I think you'll find that most professional Java developers who use Macs use Eclipse. If you're one who likes/needs explicit instructions it may help to use the more popular IDE.

As for the driver, you can find that on the mysql site as well. Just put that driver in the classpath and go from there.

Also, there isn't anything Mac specific about what you are trying to do here. Whether you are using Linux, Solaris, Mac, Windows this task is the same.

ellias2007
Feb 2, 2009, 10:46 AM
Hi eddietr

Did you find the intel build of mysqld?

Yes, ok thanks.. i found and downloaded it ....

Just a tip, I think you'll find that most professional Java developers who use Macs use Eclipse.

Eclipse as what ??? as DataBase program or as Java-IDE ??
in other words : Does Eclipse take the same role as MySql in the overall operation?? i think Eclispse in Only a Java IDE !?


As for the driver, you can find that on the mysql site as well. Just put that driver in the classpath and go from there.

HERE IS THE QUESTION : Please explain More and More

...Just put that driver in the classpath and go from there...

i downloaded Connector/J for the Mac from mysql.com... i get the file :
mysql-connector-java-5.0.8.tar ... i expanded it, then i get the folder :
mysql-connector-java-5.1.7 ... and inside it 9 items... i read the documentation and tried to load the driver and make the connection many times .... the result alaways in negative ...:confused:

i wrote the following code in the IDE and compile it :

public class Testt {

public static void main(String[] args) {
try {Class.forName("com.mysql.jdbc.Driver").newInstance();
System.out.println("it is Okey");
}
catch (Exception E)
{System.out.println("Erroooooor!");
}

and i tried to change the path ("com.mysql.jdbc.Driver") for many other forms and locations ...
Always i get : Erroooooor!... :mad:

Many Many thanks for any clear and complete help ...:o

eddietr
Feb 2, 2009, 05:55 PM
Well, I thought you said you teach Java in a university?

Do you know how to put a jar in your classpath?

And do you know how to print a stack trace in an exception handler?