📄 indy.java
字号:
/*
* @(#)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.Date;
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:MyAccessDataSource1");
// Go ahead and create a prepared statement
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 index=0;
try
{ m_ps = m_connection.prepareStatement
("SELECT * from wang " );
ResultSet as = m_ps.executeQuery();
while(as.next())
{ index=index+1;
}
}
catch (SQLException ex) {
ex.printStackTrace();
System.out.println("rs is bad");
}
IndyRecord record[]=new IndyRecord[index];
try {
ResultSet rs = m_ps.executeQuery();
int j=0;
while(rs.next())
{ //我要在这里写一个将结果集所有纪录写入一个对象数组中
IndyRecord indy=new IndyRecord();
indy.taskID=rs.getString(1);
indy.describe = rs.getString(2);
indy.ygongqi=rs.getInt(3);
indy.sgongqi=rs.getInt(4);
indy.centigrade=rs.getFloat(5);
indy.es=Date.valueOf(rs.getDate(6).toString());
indy.ef=Date.valueOf(rs.getDate(7).toString());
indy.floatime=rs.getInt(8);
record[j]=indy;
System.out.println("I wang to get rs.getString:"+record[j].taskID);
j++;
}
rs.close();
}
catch (SQLException ex) {
ex.printStackTrace();
System.out.println("rs is bad");
}
System.out.println("rs is good");
return record;
}
public void delete(String taskid)
{
try { m_ps = m_connection.prepareStatement
("DELETE from wang WHERE taskID=?" );
// Set the taskid parameter
m_ps.setString(1, taskid);
// Execute the delete
System.out.println("Execute the delete");
m_ps.executeUpdate();
}
catch (SQLException ex) {
ex.printStackTrace();
}
}
public void insert(IndyRecord record)
{ System.out.println("I want to insert:"+record.centigrade);
try { m_ps = m_connection.prepareStatement
("INSERT INTO wang(taskID,describe,ygongqi,sgongqi,floatime,es,ef) VALUES(?,?,?,?,?,?,?)");
// Execute the insert
m_ps.setString(1,record.taskID);
m_ps.setString(2,record.describe);
m_ps.setInt(3,record.ygongqi);
m_ps.setInt(4,record.sgongqi);
Float f=new Float(record.centigrade);
// m_ps.setFloat(8,f.floatValue());
m_ps.setDate(6,record.es);
m_ps.setDate(7,record.ef);
m_ps.setInt(5,record.floatime);
m_ps.executeUpdate();
System.out.println("Has executed insert methods reocrd.taskid"+record.taskID);
}
catch (SQLException ex) {
ex.printStackTrace();
}
}
public void update(IndyRecord record)
{ System.out.println("I want to update:"+record.taskID);
try { m_ps = m_connection.prepareStatement
("UPDATE wang SET taskID=?,describe=? ,ygongqi=?,sgongqi=?,floatime=?,es=?,ef=? WHERE taskID=?");
// Execute the update
m_ps.setString(8,record.taskID);
m_ps.setString(1,record.taskID);
m_ps.setString(2,record.describe);
m_ps.setInt(3,record.ygongqi);
m_ps.setInt(4,record.sgongqi);
//Float f=new Float(record.centigrade);
//m_ps.setFloat(8,f.floatValue());
m_ps.setDate(6,record.es);
m_ps.setDate(7,record.ef);
m_ps.setInt(5,record.floatime);
int a= m_ps.executeUpdate();
System.out.println("Has executed UPDATE methods reocrd.taskid"+record.taskID);
System.out.println("a= "+a);
}
catch (SQLException ex) {
ex.printStackTrace();
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -