📄 webrowsetexample.java
字号:
// com/wrox/rowset/WebRowSetExample.java
package com.wrox.rowset;
import java.io.*;
import java.sql.*;
import javax.sql.*;
import sun.jdbc.rowset.*;
public class WebRowSetExample {
WebRowSet webRs;
int recordingid;
public static void main(String[] args) {
WebRowSetExample wrse = new WebRowSetExample();
try {
boolean done = false;
BufferedReader in =
new BufferedReader(new InputStreamReader(System.in));
while (!done) {
wrse.recordingid = 0;
System.out.print("Enter a RecordingId (0 to exit) : ");
String s = in.readLine();
int result = new Integer(s).intValue();
if (result == 0) {
done = true;
} else {
wrse.recordingid = result;
wrse.populateRowSet();
wrse.writeXml();
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
void populateRowSet()
throws ClassNotFoundException, SQLException
{
Connection connection = null;
try {
Class.forName("COM.cloudscape.core.JDBCDriver");
String url = "jdbc:cloudscape:c:/wrox/database/Wrox4370.db";
String username = "APP";
String password = "";
connection = DriverManager.getConnection(url, username, password);
Statement statement = connection.createStatement();
String sqlCount = "SELECT count(*) FROM tracks WHERE " +
"recordingid=" + recordingid;
ResultSet rset = statement.executeQuery(sqlCount);
int count = 0;
if (rset.next()) {
count = rset.getInt(1);
}
webRs = null;
if (count > 0) {
System.out.println("Found " + count +
" tracks for recordingid " + recordingid);
System.out.println("Querying database for track data...");
String sqlQuery = "SELECT * FROM tracks WHERE " +
"recordingid=" + recordingid;
webRs = new WebRowSet();
webRs.setCommand(sqlQuery);
webRs.execute(connection);
}
} finally {
try { connection.close(); } catch (Exception ignored) {}
}
}
void writeXml() throws SQLException, IOException
{
if (webRs == null) {
System.out.println("No track data found for recording id");
return;
}
FileWriter fw = null;
try {
File file = new File("recording" + recordingid + "Tracks.xml");
fw = new FileWriter(file);
System.out.println("Writing track data to file " +
file.getAbsolutePath());
webRs.writeXml(fw);
} finally {
fw.flush();
fw.close();
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -