































Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!
































INFS 740, Prof. A. Brodsky, IT&E, GMU. Based on material developed by Prof. X.Wang, UVM
INFS 740, Prof. A. Brodsky, IT&E, GMU. Based on material developed by Prof. X.Wang, UVM
JDBC-ODBC
Bridge
INFS 740, Prof. A. Brodsky, IT&E, GMU. Based on material developed by Prof. X.Wang, UVM
INFS 740, Prof. A. Brodsky, IT&E, GMU. Based on material developed by Prof. X.Wang, UVM
INFS 740, Prof. A. Brodsky, IT&E, GMU. Based on material developed by Prof. X.Wang, UVM
INFS 740, Prof. A. Brodsky, IT&E, GMU. Based on material developed by Prof. X.Wang, UVM
try {
Class.forName
("
com.mysql.jdbc.Driver
");
} catch
{
ClassNotFoundException
cnfe)
{
System.out.println("Error loading driver:
" cnfe);
}
String
host
=
“
natalie.emba.uvm.edu";
String dbName
=
"someName";
int port = 1234;String mysqlURL
=
"
jdbc:mysql://
"^
+^
host
":"
port
"/"
dbName
;
INFS 740, Prof. A. Brodsky, IT&E, GMU. Based on material developed by Prof. X.Wang, UVM
statement
=
connection.createStatement();
// discuss
PreparedStatements later
query
=
"SELECT
col1,
col2,
col3 FROM sometable";
ResultSet resultSet
=
statement.executeQuery
(query);
To modify
the database, use
executeUpdate
, supplying a string
that uses
UPDATE
, or
DELETE
Use
statement.setQueryTimeout
to specify a maximum delay to
wait for results
INFS 740, Prof. A. Brodsky, IT&E, GMU. Based on material developed by Prof. X.Wang, UVM
Process the Result
while(resultSet.next()) {System.out.println(resultSet.getString(1) + " " +
resultSet.getString(2) + " " +resultSet.getString(3));
} –
First column has index 1, not 0
-^
ResultSet
provides various
getXxx
methods that take a column index or
name and returns the data
Close the Connection^ connection.close(); –
As opening a connection is expensive, postpone this step if additionaldatabase operations are expected
INFS 740, Prof. A. Brodsky, IT&E, GMU. Based on material developed by Prof. X.Wang, UVM
Statement s = C.createStatement();String sql="select * from pet";s.execute(sql);ResultSet res=s.getResultSet();if (res!=null)
{
while(res.next()){//note MySql
start with 1
System.out.println("\n"+res.getString(1)
}
} c.close();} catch (SQLException E) {
System.out.println("SQLException: " + E.getMessage());System.out.println("SQLState:
" + E.getSQLState());
System.out.println("VendorError:
" + E.getErrorCode());
}
}
}
INFS 740, Prof. A. Brodsky, IT&E, GMU. Based on material developed by Prof. X.Wang, UVM
Overview^ –
A
ResultSet
contains the results of the SQL query
-^
Represented by a table with rows and columns
-^
In JDBC 1.0 you can only proceed forward through the rows using
next
Useful Methods
-^
All methods can throw a
SQLException
-^
close
-^
Releases the JDBC and database resources
-^
The result set is automatically closed when the associated
Statement
object
executes a new query
-^
getMetaDataObject
-^
Returns a
ResultSetMetaData
object containing information about the columns
in the
ResultSet
INFS 740, Prof. A. Brodsky, IT&E, GMU. Based on material developed by Prof. X.Wang, UVM
-^
findColumn
-^
Returns the corresponding integer value corresponding to thespecified column name
-^
Column numbers in the result set do not necessarily map to the samecolumn numbers in the database
getXxx
-^
Returns the value from the column specified bycolumn name or column index as an
Xxx
Java type
-^
Returns 0 or
null (
if the value is a SQL NULL)
-^
Legal
get
Xxx
types:
wasNull
-^
To check if the last
getXxx
read was a SQL
NULL
INFS 740, Prof. A. Brodsky, IT&E, GMU. Based on material developed by Prof. X.Wang, UVM
-^
From a
ResultSet
(the return type of
executeQuery
), derive a
ResultSetMetaData
object
Use that object to look up the number, names, and types ofcolumns
-^
How many columns are in the result set?
-^
What is the name of a given column?
-^
What is the data type of a specific column?
-^
What is the maximum character size of a column?
-^
Etc.
INFS 740, Prof. A. Brodsky, IT&E, GMU. Based on material developed by Prof. X.Wang, UVM
-^
Indicates whether storing a
in the column is legal
Compare the return value against
ResultSet
constants:
columnNoNulls
,^ columnNullable
,^ columnNullableUnknown
-^
Returns
true
or
false
if the column can be used in a WHERE
clause
-^
The
isReadOnly
method indicates if the column is
definitely not
writable
-^
The
isWritable
method indicates whether it is
possible for a write
to succeed
INFS 740, Prof. A. Brodsky, IT&E, GMU. Based on material developed by Prof. X.Wang, UVM
Connection connection =DriverManager.getConnection(url, username, password);//
Look up info about the database
as
a whole.
DatabaseMetaData
dbMetaData
=
connection.getMetaData();
String
productName = dbMetaData.
getDatabaseProductName
();
System.out.println("Database: " + productName);String
productVersion = dbMetaData.
getDatabaseProductVersion
();
...Statement
statement = connection.createStatement();
String
query = "SELECT * FROM pet";
ResultSet
resultSet = statement.executeQuery(query);