http://www.roseindia.net/jdbc/jdbc-mysql/index.shtml
以下はemployeeテーブルの内容を表示するJavaサンプルプログラムです。
import java.sql.*; public class GetAllRows{ public static void main(String[] args) { System.out.println("Getting All Rows from a table!"); Connection con = null; String url = "jdbc:mysql://localhost:3306/"; String db = "employee_db"; String driver = "com.mysql.jdbc.Driver"; String user = "root"; String pass = "root"; try{ Class.forName(driver).newInstance(); con = DriverManager.getConnection(url+db, user, pass); try{ Statement st = con.createStatement(); ResultSet res = st.executeQuery("SELECT * FROM employee"); System.out.println("code: " + "\t" + "employee: "); while (res.next()) { int i = res.getInt("id"); String s = res.getString("employee"); System.out.println(i + "\t\t" + s); } con.close(); } catch (SQLException s){ System.out.println("SQL code does not execute."); } } catch (Exception e){ e.printStackTrace(); } } }
0 件のコメント:
コメントを投稿