📄 essentialjdbc.java
字号:
import java.sql.*;
public class EssentialJDBC {
public static void main(String[] args) {
EssentialJDBC SQLExample =
new EssentialJDBC(); // Create application object
SQLExample.getResultsByColumnName();
SQLExample.getResultsByColumnPosition();
SQLExample.getAllColumns();
SQLExample.closeConnection();
}
public EssentialJDBC() {
try {
Class.forName("COM.cloudscape.core.JDBCDriver");
connection = DriverManager.getConnection(sourceURL);
statement = connection.createStatement();
} catch (SQLException sqle) {
System.err.println("Error creating connection");
} catch (ClassNotFoundException cnfe) {
System.err.println(cnfe.toString());
}
}
void getResultsByColumnName() {
// Execute wildcard query and output selected columns...
try {
ResultSet recordingResults = statement.executeQuery(queryWildcard);
int row = 0;
while (recordingResults.next()) {
System.out.println("Row " + (++row) + ") "
+ recordingResults.getString("recordingid")
+ " "
+ recordingResults.getString("recordingtitle")
+ " , "
+ recordingResults.getString("catalognumber"));
}
recordingResults.close();
} catch (SQLException sqle) {
System.err.println("\nSQLException-------------------\n");
System.err.println("SQLState: " + sqle.getSQLState());
System.err.println("Message : " + sqle.getMessage());
}
}
void getResultsByColumnPosition() {
// Execute ID and name query and output results...
try {
ResultSet recordingResults = statement.executeQuery(queryIDAndName);
int row = 0;
while (recordingResults.next()) {
System.out.print("\nRow " + (++row) + ") ");
for (int i = 1; i <= 3; i++) {
System.out.print((i > 1 ? ", " : " ")
+ recordingResults.getString(i));
}
}
recordingResults.close(); // Close the result set
} catch (SQLException ex) {
System.err.println("\nSQLException-------------------\n");
System.err.println("SQLState: " + ex.getSQLState());
System.err.println("Message : " + ex.getMessage());
}
}
void getAllColumns() {
// Execute wildcard query and output all columns...
try {
ResultSet recordingResults = statement.executeQuery(queryWildcard);
ResultSetMetaData metadata = recordingResults.getMetaData();
int columns = metadata.getColumnCount(); // Column count
int row = 0;
while (recordingResults.next()) {
System.out.print("\nRow " + (++row) + ") ");
for (int i = 1; i <= columns; i++) {
System.out.print((i > 1 ? ", " : " ")
+ recordingResults.getString(i));
}
}
recordingResults.close(); // Close the result set
} catch (SQLException ex) {
System.err.println("\nSQLException-------------------\n");
System.err.println("SQLState: " + ex.getSQLState());
System.err.println("Message : " + ex.getMessage());
}
}
// Close the connection
void closeConnection() {
if (connection != null) {
try {
connection.close();
connection = null;
} catch (SQLException ex) {
System.out.println("\nSQLException-------------------\n");
System.out.println("SQLState: " + ex.getSQLState());
System.out.println("Message : " + ex.getMessage());
}
}
}
Connection connection;
Statement statement;
String sourceURL = "jdbc:cloudscape:Wrox4370.db";
String queryIDAndName =
"SELECT recordingid, recordingtitle, catalognumber FROM recordings";
String queryWildcard = "SELECT * FROM recordings"; // Select all columns
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -