/**************************************/ /* テーブルの作成,選択,および,削除 */ /* coded by Y.Suganuma */ /**************************************/ import java.io.*; import java.sql.*; public class Test { public static void main(String args[]) { int i1; boolean s; ResultSet result; Connection Con; // JDBCドライバの登録 try { Class.forName("com.mysql.jdbc.Driver").newInstance(); } catch (Exception e1) { System.out.println("Driver Error: " + e1.toString()); } try { // データベースへの接続 Con = DriverManager.getConnection("jdbc:mysql://cs-www/mysql?" + "useUnicode=true&characterEncoding=sjis", "xxxxx", "*****"); try { // SQL の実行 Statement SQL = Con.createStatement(); // データベースの選択 SQL.execute("USE base"); // テーブルの作成 SQL.execute("CREATE TABLE gakuseki (No INT(9) NOT NULL PRIMARY KEY," + "name VARCHAR(50) NOT NULL, math INT(3))"); // テーブルリストの表示 s = SQL.execute("SHOW TABLES"); if (s) { // 結果が ResultSet の場合 result = SQL.getResultSet(); System.out.println("Tables_in_base"); while (result.next()) System.out.println(" " + result.getString(1)); } // テーブルの定義内容の表示 s = SQL.execute("DESCRIBE gakuseki"); if (s) { // 結果が ResultSet の場合 result = SQL.getResultSet(); System.out.println("Field, Type, Null, Key, Default, Extra"); while (result.next()) { System.out.print(" "); for (i1 = 1; i1 <= 6; i1++) { if (i1 < 6) System.out.print(result.getString(i1) + ", "); else System.out.print(result.getString(i1)); } System.out.println(); } } // SQLを閉じる SQL.close(); // 接続を閉じる Con.close(); } catch (SQLException e2) { System.out.println("SQL Error: " + e2.toString()); } } catch (SQLException e3) { System.out.println("SQLException: " + e3.getMessage()); System.out.println("SQLState: " + e3.getSQLState()); System.out.println("VendorError: " + e3.getErrorCode()); } } }