今からはじめるプログラミング75(SQLを発行する・・・その3)
データを追加するSQLを発行する場合。。。
あまり行けてないのですが、違うメソッドを呼びます。
preparedstatementのexecuteQuery()というメソッドを使ってSQLを実行しました。
select文はquery=問い合わせなので、データを追加するinsert やデータを更新するupdateはこのメソッドに該当しません。
というか、自分の作ったexecSQLというメソッド自体が、名前とちょっと齟齬がある形になっていますw
まぁそれはおいておいて、というわけで、executeUpdateというメソッドを利用します。
別のメソッドを追加したログアウト画面ですw
queryを実行するメソッドもちょっと修正しましたw
そして、id列がidになっていないというwww
以下ソースです。
--------------------------------------------------------------------------
package sample39;
import java.awt.Container;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class Logout extends JFrame implements ActionListener {
@Override
public void actionPerformed(ActionEvent e) {
// ボタンなどが押された時の処理を描く(イベントドリブンという?)
String eventName = e.getActionCommand();
// 確認
System.out.println(eventName);
if ("cancel".equals(eventName)) {
this.setVisible(false);
// 特に意味はないですが、終了時に数値を通知できます。
System.exit(99);
}
}
public Logout() {
init();
}
private void init() {
// コンポーネントの入れ物
Container container = this.getContentPane();
JButton btnLogout = new JButton("ログアウト");
JButton btnCancel = new JButton("キャンセル");
// コンポーネントのおおきさ
btnLogout.setSize(100, 30);
btnCancel.setSize(100, 30);
// コンポーネントの場所
btnLogout.setLocation(100, 100);
btnCancel.setLocation(250, 100);
// イベント実行時のきりわけに
btnLogout.setActionCommand("logout");
btnCancel.setActionCommand("cancel");
// <b>ログアウトボタンを使用不能に</b>
btnLogout.setEnabled(false);
// イベントの登録
btnLogout.addActionListener(this);
btnCancel.addActionListener(this);
JPanel pnl = new JPanel();
pnl.add(btnLogout);
pnl.add(btnCancel);
container.add(pnl);
this.setTitle("ろぐあうと?");
this.setSize(600, 400);
this.setLocation(200, 200);
this.setVisible(true);
}
public void exec(String sql, String params) {
final String URL = "jdbc:sqlite:E:\\resource\\sqlite_win\\test.db";
// final String USER = "";
// final String PASS = ""; Connection conn = null;
Connection conn = null;
PreparedStatement ps = null;
try {
conn = DriverManager.getConnection(URL);
ps = conn.prepareStatement(sql);
// パラメータのセット
for (int i = 0; i < params.length; i++) {
ps.setString(i + 1, params[i]);
}
// SQL実行
ResultSet rs = ps.executeQuery();
// /MetaDataから列名とかを取得する
ResultSetMetaData mDat = rs.getMetaData();
int colCnt = mDat.getColumnCount();
// 列名をいれる配列
String names = new String[colCnt];
for (int i = 0; i < colCnt; i++) {
// 列名をmetadataから配列に入れて表示(System.out)
names[i] = mDat.getColumnName(i + 1);
System.out.print(names[i] + " | ");
}
// 見ずらいので改行
System.out.println(" ");
while (rs.next()) {
for (int i = 0; i < colCnt; i++) {
System.out.print(rs.getString(names[i]) + " | ");
}
System.out.println(" ");
}
rs.close();
ps.close();
} catch (SQLException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if (conn != null) {
conn.close();
}
} catch (SQLException e) {
// TODO 自動生成された catch ブロック
e.printStackTrace();
}
System.out.println("処理が完了しました");
}
}
public void execUpd(String sql, String params) {
final String URL = "jdbc:sqlite:E:\\resource\\sqlite_win\\test.db";
// final String USER = "";
// final String PASS = ""; Connection conn = null;
Connection conn = null;
PreparedStatement ps = null;
try {
conn = DriverManager.getConnection(URL);
ps = conn.prepareStatement(sql);
if (params != null) {
// パラメータのセット
for (int i = 0; i < params.length; i++) {
ps.setString(i + 1, params[i]);
}
}
// SQL実行
int result = ps.executeUpdate();
System.out.println("処理が完了しました(" + result + "件)");
} catch (SQLException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if (conn != null) {
conn.close();
}
} catch (SQLException e) {
// TODO 自動生成された catch ブロック
e.printStackTrace();
}
System.out.println("処理が完了しました");
}
}
/**
* SQLを実行するメソッドを追加して、ポインタから実行できるようにした
* */
public void execSQL() {
final String URL = "jdbc:sqlite:E:\\resource\\sqlite_win\\test.db";
// final String USER = "";
// final String PASS = "";
final String SQL = "select * from area where id =? ;";
Connection conn = null;
PreparedStatement ps = null;
try {
conn = DriverManager.getConnection(URL);
ps = conn.prepareStatement(SQL);
ps.setInt(1, 1);
ResultSet rs = ps.executeQuery();
// /MetaDataから列名とかを取得する
ResultSetMetaData mDat = rs.getMetaData();
int colCnt = mDat.getColumnCount();
// 列名をいれる配列
String names = new String[colCnt];
for (int i = 0; i < colCnt; i++) {
// 列名をmetadataから配列に入れて表示(System.out)
names[i] = mDat.getColumnName(i + 1);
System.out.print(names[i] + " | ");
}
// 見ずらいので改行
System.out.println(" ");
while (rs.next()) {
for (int i = 0; i < colCnt; i++) {
System.out.print(rs.getString(names[i]) + " | ");
}
System.out.println(" ");
}
rs.close();
ps.close();
} catch (SQLException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if (conn != null) {
conn.close();
}
} catch (SQLException e) {
// TODO 自動生成された catch ブロック
e.printStackTrace();
}
System.out.println("処理が完了しました");
}
}
public static void main(String args) {
Logout frame = new Logout();
// //SQLを実行する
frame.execSQL();
frame.exec("select * from area where id =? ;", new String { "1" });
frame.execUpd("insert into area values (1,'name',2,1,'cm');", null);
}
}
-----------------------------------------------------------------------
実行結果は以下になります。
(何回実行したかがわかるという?)
-------------------------------------------------------------------------
id | name | sort | res_flg | comment |
1 | minamino-huchuu | 1 | 0 | test data |
1 | name | 2 | 1 | cm |
1 | name | 2 | 1 | cm |
処理が完了しました
id | name | sort | res_flg | comment |
1 | minamino-huchuu | 1 | 0 | test data |
1 | name | 2 | 1 | cm |
1 | name | 2 | 1 | cm |
処理が完了しました
処理が完了しました(1件)
処理が完了しました
cancel
----------------------------------------------------------------------------
あれだ、コミットについて忘れてますw
実行結果を見るとauto commitがされているようですがwww