📄 indy.java~1~
字号:
/*
* @(#)Indy
*
* Copyright (c) 1998 Karl Moss. All Rights Reserved.
*
* You may study, use, modify, and distribute this software for any
* purpose provided that this copyright notice appears in all copies.
*
* This software is provided WITHOUT WARRANTY either expressed or
* implied.
*
* @author Karl Moss
* @version 1.0
* @date 17Apr98
*
*/
package javaservlets.tunnel;
import java.sql.*;
/**
* <p>Implements the IndyInterface to provide query capabilities
* into the Indianapolis 500 database.
*/
public class Indy implements IndyInterface
{
// The JDBC Connection
Connection m_connection = null;
// A prepared statement to use to query the database
PreparedStatement m_ps = null;
/**
* <p>Connects to the database.
*
* @return True if the database connection was established
*/
public boolean connect()
{
boolean rc = false;
try {
// Load the Bridge
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver").newInstance();
// Connect to the Access database
m_connection =
DriverManager.getConnection("jdbc:odbc:MyAccessDataSource");
// Go ahead and create a prepared statement
m_ps = m_connection.prepareStatement
("SELECT Year, Driver, AvgSpeed from IndyWinners " +
"WHERE Year = ?");
rc = true;
}
catch (Exception ex) {
ex.printStackTrace();
}
return rc;
}
/**
* <p>Closes the database connection
*/
public void close()
{
// Close the connection if it was opened
if (m_connection != null) {
try {
m_connection.close();
}
catch (SQLException ex) {
ex.printStackTrace();
}
m_connection = null;
}
}
/**
* <p>Given the year return the corresponding Indianapolis
* 500 record
*
* @param year Year of the race
* @return Indy 500 record or null if not found
*/
public IndyRecord query(int year)
{
IndyRecord record = null;
try {
// Set the year parameter
m_ps.setInt(1, year);
// Execute the query
ResultSet rs = m_ps.executeQuery();
// Make sure a record exists
if (rs.next()) {
// Create a new IndyRecord object
record = new IndyRecord();
// Set the values
record.year = rs.getInt(1);
record.driver = rs.getString(2);
record.speed = rs.getDouble(3);
}
rs.close();
}
catch (SQLException ex) {
ex.printStackTrace();
record = null;
}
return record;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -