Sei sulla pagina 1di 5

JDBC EXAMPLE

JDBC is Java's solution to Microsoft's ADODB. JDBC in my

opinion is much, much better. It uses a standard set of APIs to

access a variety of databases. I assume in this article that you

have knowledge of some form of standard database API, SQL,

and how relational databases work. I'll also be using mySQL as

an example database. So, let's begin.

There are three core objects to JDBC: Connection, Statement,

and ResultSet. A ResultSet is what everyone else calls a record

set, so keep that in mind. Our first task is to get a Connection

object to our database. To get one, you need to load a driver to

your selected database into JDBC, and create a Connection

object using a JDBC URL, username, and password. This entire

process with throw about four exceptions. JDBC drivers are out

there, just use a search engine. For mySQL, I use MM.MySQL,

which is available at http://mmmysql.sourceforge.net/. If you

want some, I suggest looking at Sun's JDBC driver database at

http://industry.java.sun.com/products/jdbc/drivers. Let's look


at the whole process to get a Connection and I'll go over it. Be

sure you import the java.sql package.

try {

Class.forName("org.gjt.mm.mysql.Driver").newIns

tance();//1

Connection c =

DriverManager.getConnection("jdbc:mysql://local

host:3306/test?

user=admin;password=admin","","");//2

} catch (Exception e) { //3

e.printStackTrace();

Line 1 is where we install the JDBC driver to mySQL. Installing

a driver is easy. Just create a new instance of the driver class.

Be aware Class.newInstance() and Class.forName() throw a lot

of exceptions. Line 2 actually connects to the database. I pass


the JDBC URL (which varies from driver to driver), and the

username/password. I set username and password because

MM.MySQL lets you specify them in the URL.

Now you have the Connection object, let's do some queries! As

any newbie SQL programmer knows, the most important thing

to know is how to execute a SELECT statement. It's not hard.

Here's the code, and again I'll go over it.

// create the Connection...

Statement stmt =

connection.createStatement();//1

ResultSet rs = stmt.executeQuery("select

* from mytable");//2

while(rs.next())//3

System.out.println("teststr=" +

rs.getString("teststr"));//4
}

Line 1 creates a blank statement for use on the Connection

that created it. Line 2 actually executes the query and returns

a ResultSet. Line 3 calls the ResultSet.next() method. This

moves the ResultSet to the next row, and if it is not at EOF, it

returns true. Line 4 grabs a string from this row. You call

rs.getType(), where Type is the type of data you want. You can

either pass the String name of the column, or the integer index

of the column.

Now it's time to clean up. Not too hard, as you will see :)

rs.close();

stmt.close();

connection.close();

OK, now you can do SELECT statements all you want :). Now,

how do we put data back into the database? Since I'm a

mySQL junkie, I'll create my tables with the primary key as

auto_increment and do something like this:


stmt.executeUpdate("INSERT INTO mytable

(teststr) VALUES ('this is a test!')");

However, since this is vendor-specific this is baaad :) You can

use the ResultSet object to insert records. I won't go into detail

here, but just try the ResultSet.updateType() methods, and the

ResultSet.updateRow() and ResultSet.deleteRow() methods.

I now leave you with a few paths to follow. To increase your

database efficiency, try using a connection pool. Here's one I

made: http://www.pscode.com/vb/scripts/ShowCode.asp?

txtCodeId=2663&lngWId=2. You should read up on the JDBC

javadoc at java.sun.com, check out the new javax.sql package,

and learn about PreparedStatements. By the way, I URGE!!!

you to ensure that you replace any ' with \' WHENEVER you

insert strings into queries! This is a major hole in many

websites! Anyway, I'll see you in database land!

Potrebbero piacerti anche