import java.sql.SQLException;
/**
* 第一個 JDBC 的 HelloWorld 程序, 數(shù)據(jù)庫訪問 MySQL.
* @author BeanSoft@126.com
* @version 0.3 2007-12-12
*/
public class JDBCHelloWorld {
public static void main(String[] args) {
// 1. 注冊驅動
try {
Class.forName("org.apache.derby.jdbc.ClientDriver");
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
......
try {
// 2. 獲取數(shù)據(jù)庫的連接
conn = java.sql.DriverManager.getConnection(
"jdbc:derby://localhost:1527/myeclipse",
"classiccars", "classiccars");
// 3. 獲取表達式
stmt = conn.createStatement();
// 執(zhí)行插入數(shù)據(jù)的 SQL
stmt.executeUpdate("insert into Student(username, password,age) values('張三', '1234', 20)");
// 4. 執(zhí)行 SQL
rs = stmt.executeQuery("select * from Student");
// 5. 顯示結果集里面的數(shù)據(jù)
while(rs.next()) {
System.out.println("編號=" + rs.getInt(1));
System.out.println("學生姓名=" +
rs.getString("username"));
System.out.println("密碼=" + rs.getString("password"));
System.out.println("年齡=" + rs.getString("age"));
}
// 執(zhí)行刪除數(shù)據(jù)的 SQL
// stmt.executeUpdate("delete from Student");
} catch (SQLException e) {
e.printStackTrace();
} finally {
// 6. 釋放資源,建議放在finally語句中確保都被關閉掉了
try {
rs.close();
} catch (SQLException e) {
}
try {
stmt.close();
} catch (SQLException e) {
}
try {
conn.close();
} catch (SQLException e) {
}
}
}
}
/**
* 第一個 JDBC 的 HelloWorld 程序, 數(shù)據(jù)庫訪問 MySQL.
* @author BeanSoft@126.com
* @version 0.3 2007-12-12
*/
public class JDBCHelloWorld {
public static void main(String[] args) {
// 1. 注冊驅動
try {
Class.forName("org.apache.derby.jdbc.ClientDriver");
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
......
try {
// 2. 獲取數(shù)據(jù)庫的連接
conn = java.sql.DriverManager.getConnection(
"jdbc:derby://localhost:1527/myeclipse",
"classiccars", "classiccars");
// 3. 獲取表達式
stmt = conn.createStatement();
// 執(zhí)行插入數(shù)據(jù)的 SQL
stmt.executeUpdate("insert into Student(username, password,age) values('張三', '1234', 20)");
// 4. 執(zhí)行 SQL
rs = stmt.executeQuery("select * from Student");
// 5. 顯示結果集里面的數(shù)據(jù)
while(rs.next()) {
System.out.println("編號=" + rs.getInt(1));
System.out.println("學生姓名=" +
rs.getString("username"));
System.out.println("密碼=" + rs.getString("password"));
System.out.println("年齡=" + rs.getString("age"));
}
// 執(zhí)行刪除數(shù)據(jù)的 SQL
// stmt.executeUpdate("delete from Student");
} catch (SQLException e) {
e.printStackTrace();
} finally {
// 6. 釋放資源,建議放在finally語句中確保都被關閉掉了
try {
rs.close();
} catch (SQLException e) {
}
try {
stmt.close();
} catch (SQLException e) {
}
try {
conn.close();
} catch (SQLException e) {
}
}
}
}