JDBC
JDBC六步
-
注冊驅動
-
建立連接
-
獲取預編譯的資料庫操作物件
-
執行SQL
-
處理查詢結果
-
釋放資源
Connection conn = null;
PrepareStatement ps = null;
ResultSet rs = null;
String url="jdbc:mysql://資料庫地址:埠號/資料庫名稱";
String user="用戶名";
String pwd="密碼";
try{
//注冊驅動
Class.forname("com.mysql.cj.jdbc.Driver")
//建立連接
conn = DriverManager.getConnection(url,user,pwd);
//獲取預編譯的資料庫操作物件
String SQL="select * from table";
ps = conn.PrepareStatement(SQL);
//執行SQL
rs = ps.executeQuery();
//處理查詢結果
while(rs.next()){
String name = rs.getString("name");
}
}catch(Exception e){
e.printStackTrace()
}finally{
//釋放資源
if(rs!=null){
try{
rs.close();
}catch(Exception e){
e.printStackTrace()
}
}
if(ps!=null){
try{
ps.close();
}catch(Exception e){
e.printStackTrace()
}
}
if(conn!=null){
try{
conn.close();
}catch(Exception e){
e.printStackTrace()
}
}
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/541651.html
標籤:其他
上一篇:檔案字串
