資源簡介
JDBC入門匯總及范例講解 ,具體過程與效果看博文
http://blog.csdn.net/evankaka/article/details/45370609

代碼片段和文件信息
package?com.mucfc;
import?java.sql.*;
public?class?JdbcTest?{
//定義數據庫驅動程序
private?static?final?String?DBDRIVER=“com.mysql.jdbc.Driver“;
//數據庫連接地址
private?static?final?String?DBURL=“jdbc:mysql://localhost:3306/school“;//school表示數據庫
//數據庫用戶名
private?static?final?String?DBUSER=“root“;
//電腦上的數據庫密碼
private?static?final?String?DBPASS=“christmas258@“;
????public?void?testDDL(){
???? try?{
????????//1.注冊驅動
????????Class.forName(DBDRIVER);
????????//2.獲取連接
????????Connection?conn?=?DriverManager.getConnection(DBURLDBUSERDBPASS);??
????????//3.創建Statement對象
????????Statement?stmt?=?conn.createStatement();??????
????????//4.準備sql語句
????????String?sql??=“CREATE?TABLE?student(sid?INT?PRIMARY?KEYsname?VARCHAR(20)age?INT)“;?????
????????//5.通過statement對象發送sql語句返回執行結果
????????int?count?=?stmt.executeUpdate(sql);?
????????System.out.println(“CREATE?TABLE?student......“);
????????//6.打印執行結果
????????System.out.println(“影響了“+count+“條記錄“);
????????
????????//執行插入操作
????????System.out.println(“Inserting?records?into?the?table...“);
????????sql?=?“INSERT?INTO?student?“?+
?????????????????????“VALUES?(100?‘小文‘?18)“;
????????stmt.executeUpdate(sql);
????????sql?=?“INSERT?INTO?student?“?+
?????????????????????“VALUES?(101?‘大林‘?25)“;
????????stmt.executeUpdate(sql);
????????sql?=?“INSERT?INTO?student?“?+
?????????????????????“VALUES?(102?‘阿白‘??30)“;
????????stmt.executeUpdate(sql);
????????sql?=?“INSERT?INTO?student?“?+
?????????????????????“VALUES(103?‘小小‘?28)“;
????????stmt.executeUpdate(sql);
????????System.out.println(“Inserted?records?into?the?table...“);
????????
????????//執行查找操作
????????sql?=?“SELECT*?FROM?student“;
????????System.out.println(“SELECT?records?FROM?the?table...“);
????????ResultSet?rs?=?stmt.executeQuery(sql);
???????//輸出查找結果
????????while(rs.next()){
???????????//先獲取數據
???????????int?sid??=?rs.getInt(“sid“);???????
???????????String?sname?=?rs.getString(“sname“);
???????????int?age?=?rs.getInt(“age“);
???????????//打印結果
???????????System.out.print(“sid:?“?+?sid);
???????????System.out.print(“??sname:?“?+sname);
???????????System.out.println(“??age:?“?+?age);
????????}
????????rs.close();
????????//7.關閉資源
????????try?{
????????if(stmt!=null)
????????{
???????? stmt.close();
????????}
????????}?catch?(Exception?e)?{
???e.printStackTrace();
}
????????try?{
????????if(conn!=null)
????????{????
???????? conn.close();
????????}
????????}?catch?(Exception?e)?{
????????
}
???? }?catch?(Exception?e)?{
???? ?e.printStackTrace();
}
????????
}
public?static?void?main(String[]?args)?{
/* JdbcTest?jdbcTest=new?JdbcTest();
jdbcTest.CreateTableTest();
jdbcTest.InsertTest();
jdbcTest.SelectTest();*/
SqlDB?sqlDB=new?SqlDB();
sqlDB.CreatTable();
sqlDB.InsertData(309?“小紅“12);
sqlDB.InsertData(33?“小灰“34);
sqlDB.InsertData(23?“阿大“145);
????sqlDB.SelectDataWithId(33);
}
public?void?CreateTableTest(){
//獲取連接
Conne
?屬性????????????大小?????日期????時間???名稱
-----------?---------??----------?-----??----
?????目錄???????????0??2015-04-29?19:58??JDBCLearning1\
?????文件?????????413??2015-04-29?19:59??JDBCLearning1\.classpath
?????文件?????????389??2015-04-29?19:58??JDBCLearning1\.project
?????目錄???????????0??2015-04-29?19:58??JDBCLearning1\.settings\
?????文件?????????598??2015-04-29?19:58??JDBCLearning1\.settings\org.eclipse.jdt.core.prefs
?????目錄???????????0??2015-05-06?08:59??JDBCLearning1\bin\
?????目錄???????????0??2015-05-06?08:59??JDBCLearning1\bin\com\
?????目錄???????????0??2015-05-06?08:59??JDBCLearning1\bin\com\mucfc\
?????文件????????4946??2015-05-06?08:59??JDBCLearning1\bin\com\mucfc\JdbcTest.class
?????文件????????3996??2015-05-06?08:59??JDBCLearning1\bin\com\mucfc\SqlDB.class
?????目錄???????????0??2015-04-29?19:59??JDBCLearning1\src\
?????目錄???????????0??2015-04-29?19:59??JDBCLearning1\src\com\
?????目錄???????????0??2015-04-29?20:31??JDBCLearning1\src\com\mucfc\
?????文件????????6261??2015-04-30?08:20??JDBCLearning1\src\com\mucfc\JdbcTest.java
?????文件????????4247??2015-04-30?08:33??JDBCLearning1\src\com\mucfc\SqlDB.java
評論
共有 條評論