我想要实现的是:我在多个数据库中有救护车数据。救护车的GPS在一个数据库中跟踪,在另一个数据库中发送记录,在另一个数据库中发送医疗记录。他们都有共同的一个独特的关键导致火灾/ EMS的事件。在这种情况下,我想使用JDBC来使用参数查询两个数据库。我的参数是紧急事件的唯一ID。我想根据该唯一ID完成来自多个数据库源的事件的完整报告。正如你从我下面显示的代码中可以看到的,我传递了两次参数 - 一次在每个try catch块(preStatement.setString(1,“MLC140701045015”);)中。jdbc和使用单个参数查询多个数据库
我的目标是拥有一个下拉列表中的gui。用户选择一个事件并生成报告。该参数只能从gui的下拉列表中传递一次。
我的问题是:该参数只能传递一次吗? 我拥有文科学位,而不是计算机科学学位。你对我来说不能太简单。请在你的回复中明确表示。
package javaDatabase;
import java.sql.Connection;
import java.sql.Date;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.sql.PreparedStatement;
public class PreparedStatements
{
public static void main(String[] args)
{
// obtain information from dispatch
Connection conn = null;
Statement stmt = null;
try
{
conn = DriverManager
.getConnection
("jdbc:sqlserver://999.99.99.99;database=Incidents;integratedsecurity=false;user=myUser;password=myPassw
ord");
PreparedStatement preStatement = conn
.prepareStatement("SELECT I_EventNumber, I_Address,
I_tTimeDispatch FROM IIncident WHERE I_EventNumber = ?");
// pass a parameter
preStatement.setString(1, "MLC140701045015");
ResultSet rset = preStatement.executeQuery();
// output the results of the dispatch
while (rset.next())
{
String IncidentID = rset.getString("I_EventNumber");
String Address = rset.getString("I_Address");
String dtmIncident = rset.getString("I_tTimeDispatch");
System.out.println(IncidentID + ", " + Address + ", "
+ dtmIncident);
}
// clean up the program and close the connections
} catch (SQLException ex)
{
ex.printStackTrace();
} finally
{
try
{
if (stmt != null)
stmt.close();
if (conn != null)
conn.close();
} catch (SQLException ex)
{
ex.printStackTrace();
}
}
// obtain information from gps database.
Connection avlconn = null;
Statement avlstmt = null;
try
{
avlconn = DriverManager
.getConnection
("jdbc:sqlserver://777.77.77.77;database=gps_tracks;integratedsecurity=false;user=myUser;password=myPass
");
PreparedStatement preStatement = avlconn
.prepareStatement("SELECT RTRIM(AssignedIncident) AS [Mission],
RTRIM(CallSign) AS [Ambulance], RTRIM(UnitStatus) AS [Disposition], DateTimeStamp, Longitude, Latitude
FROM HISTORY_201407 WHERE AssignedIncident = ?");
// pass the same parameter previously. Why must this be done twice?
preStatement.setString(1, "MLC140701045015");
// return a result of the gps tracks
ResultSet rset = preStatement.executeQuery();
while (rset.next())
{
String mission = rset.getString("Mission");
String truck = rset.getString("Ambulance");
String disposition = rset.getString("Disposition");
Date dateTime = rset.getDate("DateTimeStamp");
Double longitude = rset.getDouble("Longitude");
Double latitude = rset.getDouble("Latitude");
System.out.println(mission + ", " + truck + ", " + disposition
+ ", " + dateTime + ", " + longitude + ", " + latitude);
}
// clean up the program and close out the connections
} catch (SQLException ex)
{
ex.printStackTrace();
} finally
{
try
{
if (avlstmt != null)
avlstmt.close();
if (avlconn != null)
avlconn.close();
} catch (SQLException ex)
{
ex.printStackTrace();
}
}
}
}