] >

<..sybdriver = get a sybase driver..>
SybDriver sybdriver =
(SybDriver) Class
. forName("com.sybase.jdbc2.jdbc.SybDriver")
. newInstance();
sybdriver.setVersion(
com.sybase.jdbcx.SybDriver.VERSION_5 );
DriverManager.registerDriver(sybdriver);
-_-_-
javac -classpath /usr/local/jConnect-5_2/classes/jconn2.jar:/usr/local/jConnect-5_2/classes:. filename.java
java -classpath /usr/local/jConnect-5_2/classes/jconn2.jar:/usr/local/jConnect-5_2/classes:. filename "username" "password"
<..DBdriver.java..>
import java.sql.*;
import com.sybase.jdbcx.SybDriver;
public class DBdriver {
public static void main(String args[]) {
try {
<.sybdriver = get a sybase driver.>
} catch(Exception e) {
System.err.println("error " + e);
} } }
-_-_-
[java.lang.Class, java.sql, com.sybase.jdbcx.SybDriver]
static Connection getConnection(String url, String username, String password)
jdbc:subprotocol:database-name
The subprotocol is provided by the database developer. For SyBase we have sybase:Tds, where Tds refers to a driver type.
<..connection = connect to database..>
Connection connection = null;
String server = "database";
String port = "4101";
String database = "c670ab";
String username = args[0];
String password = args[1];
String url = "jdbc:sybase:Tds:"
+ server + ":"
+ port + "/"
+ database;
connection =
DriverManager.getConnection(url, username, password);
-_-_-
<..Connect.java..>
import java.sql.*;
import com.sybase.jdbcx.SybDriver;
public class Connect {
public static void main(String args[]) {
try {
<.sybdriver = get a sybase driver.>
<.connection = connect to database.>
if(!connection.isClosed()){
System.out.println("Hurray!");
connection.close();
}
} catch(Exception e) { System.err.println("error " + e); }
} }
-_-_-
Statement createStatement()
int executeUpdate(String sql)
<..ddl and update..>
Statement stat = connection.createStatement();
stat.executeUpdate(
"CREATE TABLE DBupdate (Name CHAR(10))" );
stat.executeUpdate(
"INSERT INTO DBupdate VALUES ( ’my name’)" );
stat.executeUpdate(
"DROP TABLE DBupdate" );
stat.close();
-_-_-
ResultSet executeQuery(String sql)
boolean next()
| int | getInt(col) |
| long | getLong(col) |
| short | getShort(col) |
| byte | getByte(col) |
| double | getDouble(col) |
| float | getFloat(col) |
| boolean | getBoolean(col) |
| String | getString(col) |
| byte [] | getBytes(col) |
| Date | getDate(col) |
| Time | getTime(col) |
| Timestamp | getTimestamp(col) |
| Ref | getRef(col) |
| InputStream | getAsciiStream(col) |
| InputStream | getBinaryStream(col) |
| Reader | getCharacterStream(col) |
| URL | getURL(col) |
boolean wasNull()
<..java query instructions..>
Statement stat = connection.createStatement();
stat.executeUpdate(
"CREATE TABLE DBquery (Name CHAR(10))" );
stat.executeUpdate(
"INSERT INTO DBquery VALUES ( ’my name’)" );
stat.executeUpdate(
"INSERT INTO DBquery VALUES ( ’your name’)" );
ResultSet query =
stat.executeQuery("SELECT * FROM DBquery");
while( query.next() ){
System.out.println( query.getString( "Name" ) );
}
query.close();
stat.executeUpdate( "DROP TABLE DBquery" );
stat.close();
-_-_-
| type | |
| TYPE_FORWARD_ONLY | asks for forward moving cursor |
| TYPE_SCROLL_INSENSITIVE TYPE_SCROLL_SENSITIVE | ask for scrollable tables and indicate whether changes by others are allowed |
| CLOSE_CURSORS_AT_COMMIT HOLD_CURSORS_OVER_COMMIT | close object when commit is called? |
| concurrency | |
| CONCUR_READ_ONLY CONCUR_UPDATABLE | available concurrency modes |
Statement createStatement(int resultSetType, int resultSetConcurrency)
| move the cursor to the specified row | ||||||||||||||||
| boolean isBeforeFirst() boolean isAfterLast() boolean isFirst() boolean isLast() | is the cursor in the specified location? | ||||||||||||||||
| int getRow() | provides the current row number |
int findColumn(String columnName)
<..java navigation instructions..>
Statement stat = connection.createStatement(
ResultSet.TYPE_SCROLL_SENSITIVE,
ResultSet.CONCUR_READ_ONLY);
stat.executeUpdate(
"CREATE TABLE DBnav (Name CHAR(10))" );
stat.executeUpdate(
"INSERT INTO DBnav VALUES ( ’my name’)" );
stat.executeUpdate(
"INSERT INTO DBnav VALUES ( ’your name’)" );
ResultSet query =
stat.executeQuery("SELECT * FROM DBnav");
while( query.relative( 1 ) ){
System.out.println( query.getString( "Name" ) );
}
// query.close();
-_-_-
void cancelRowUpdates()
void updateRow()
<..update result table..>
Statement stat = connection.createStatement(
ResultSet.TYPE_SCROLL_SENSITIVE,
ResultSet.CONCUR_UPDATABLE);
stat.executeUpdate(
"CREATE TABLE DBupdatetbl (Name CHAR(10))" );
stat.executeUpdate(
"INSERT INTO DBupdatetbl VALUES ( ’my name’)" );
ResultSet query =
stat.executeQuery("SELECT * FROM DBupdatetbl");
query.absolute(1);
// not supported?
query.updateString("Name", "your name");
query.updateRow();
-_-_-
void setAutoCommit(boolean autoCommit)
void commit()
void rollback()
<..commit transactions..>
connection.setAutoCommit(false);
stat.executeUpdate(
"INSERT INTO DBcommit VALUES ( ’my name’)" );
stat.executeUpdate(
"INSERT INTO DBcommit VALUES ( ’your name’)" );
connection.commit();
connection.setAutoCommit(true);
-_-_-
PreparedStatement prepareStatement(String sql)
int executeUpdate()
<..prepared statements..>
Statement stat = connection.createStatement();
stat.executeUpdate(
"CREATE TABLE DBprepare (Name CHAR(10), Age int)" );
PreparedStatement prep = connection.prepareStatement(
"INSERT INTO DBprepare VALUES ( ?, ?)" );
prep.setString( 1, "my name" );
prep.setInt( 2, 10 );
prep.executeUpdate();
stat.executeUpdate( "DROP TABLE DBprepare" );
stat.close();
-_-_-
ResultSetMetaData getMetaData()
| int | getColumnCount() | number of columns |
| String | getColumnLabel(int column) | column’s title for displays |
| String | getColumnName(int column) | column’s name |
| int | getColumnType(int column) | column’s SQL type |
| String | getTableName(int column) | column’s table name |
| int | isNullable(int column) | is NULL permitted? |
<..info about tables..>
ResultSet query =
stat.executeQuery( "SELECT * FROM DBinfotbl" );
ResultSetMetaData infoTbl = query.getMetaData();
System.out.println(
"number of columns: " + infoTbl.getColumnCount() +
"\ncolumn label: " + infoTbl.getColumnLabel( 1 ) +
"\ncolumn name: " + infoTbl.getColumnName( 1 ) +
"\ncolumn type: " + infoTbl.getColumnType( 1 ) +
"\ntable name: " + infoTbl.getTableName( 1 )
);
query.last();
System.out.println(
"number of rows: " + query.getRow()
);
query.close();
-_-_-
DatabaseMetaData getMetaData()
<..db meta info..>
DatabaseMetaData meta = connection.getMetaData();
System.out.println(
"product name: " + meta.getDatabaseProductName() +
"\ndriver name: " + meta.getDriverName() +
"\nsupports full ANSI 92 SQL: " +
meta.supportsANSI92FullSQL() +
"\nsupports outer joins: " +
meta.supportsOuterJoins()
);
-_-_-
Due: Mo, Mar 9, midnight
Provide a Java program with embedded SQL code which performs the following operations.
Notes
Reference: Ch. 9.4 in textbook; sample Jhyton program (contributed by Rick Holbert)