2013年8月14日水曜日

テーブルの作成とデータ追加:SQLite


import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;
 
public class SqliteTest {
 
 /**
  * @param args
  */
 public static void main(String[] args) {
  // TODO 自動生成されたメソッド・スタブ
 
  try {
   // JDBCドライバーの指定
   Class.forName("org.sqlite.JDBC");
 
   // データベースに接続する なければ作成される
   Connection con = DriverManager.getConnection("jdbc:sqlite:../database.db");
 
   // Statementオブジェクト作成
   Statement stmt = con.createStatement();
 
   //テーブル作成 すでにある場合は例外が発生
   stmt.executeUpdate("create table nameTable( id integer, name string )");
    
    //テーブルにデータを追加
   stmt.executeUpdate("insert into nameTable values(1, 'sakura')");
 
  } catch (ClassNotFoundException e) {
   // TODO 自動生成された catch ブロック
   e.printStackTrace();
  } catch (SQLException e) {
   // TODO 自動生成された catch ブロック
   // Connection の例外が発生した時
 
   e.printStackTrace();
  }
 }
}



0 件のコメント:

コメントを投稿