mutiltable.java
来自「本套光盘提供了本书各章实例的所需的部分源程序文件以及数据库文件。读者 需要使用」· Java 代码 · 共 107 行
JAVA
107 行
import java.sql.*;
import java.net.URL;
class mutiltable
{
public static void main (String args[])
{
String sUsr = "JavaDB";
String sPwd = "javadb";
String query = "Select s.* from firsttable AS f inner join secondtable AS s where f.id =s.sid";
try
{
//加载jdbc-odbc桥驱动程序
Class.forName( "oracle.jdbc.driver.OracleDriver" );
Connection con = DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:JAVADB", sUsr, sPwd );
//建立一个Statement对象,用这个对象向驱动程序发送SQL语句
Statement stmt = con.createStatement ();
//提交SQL语句,创建一个ResultSet对象
ResultSet rs = stmt.executeQuery (query);
// 显示结果集的所有行和列
dispResultSet (rs);
// 关闭结果集
rs.close();
// 关闭statement对象
stmt.close();
// 关闭连接
con.close();
}
catch (SQLException ex)
{
//捕获错误信息,并显示其详细信息
System.out.println ("\n*** SQLException caught ***\n");
while (ex != null)
{
System.out.println ("SQLState: " +ex.getSQLState ());
System.out.println ("Message: " + ex.getMessage ());
System.out.println ("Vendor: " +ex.getErrorCode ());
ex = ex.getNextException ();
System.out.println ("");
}
}
catch (java.lang.Exception ex)
{
// 捕获其他类型的例外
ex.printStackTrace ();
}
}
//-------------------------------------------------------------------
// dispResultSet
// 现实整个结果届中的所有的行和列
//-------------------------------------------------------------------
private static void dispResultSet (ResultSet rs)
throws SQLException
{
int i;
// Get the ResultSetMetaData. This will be used for
// the column headings
ResultSetMetaData rsmd = rs.getMetaData ();
// Get the number of columns in the result set
int numCols = rsmd.getColumnCount ();
// Display column headings
for (i=1; i<=numCols; i++) {
if (i > 1) System.out.print(",");
System.out.print(rsmd.getColumnLabel(i));
}
System.out.println("");
// Display data, fetching until end of the result set
boolean more = rs.next ();
while (more) {
// Loop through each column, getting the
// column data and displaying
for (i=1; i<=numCols; i++) {
if (i > 1) System.out.print(",");
System.out.print(rs.getString(i));
}
System.out.println("");
// Fetch the next result set row
more = rs.next ();
}
}
}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?