包數據庫編程;
導入Java . SQL . *;
公共類數據庫連接{
公共靜態void main(String[] args)引發SQLException{
//1.加載驅動程序
嘗試{
class . forname(" com . MySQL . JDBC . driver ");
} catch(ClassNotFoundException e){
// TODO自動生成的catch塊
e . printstacktrace();
}
//2.創建與數據庫的連接
//連接字符串
string URL = " JDBC:MySQL://192.168.1.101:3306/Java數據庫?用戶= root & amp密碼=胡敏”;
connection con = driver manager . get connection(URL);
//讀取和寫入
如果(!con.isClosed()){
System.out.print("歡迎使用數據庫!\ n您想做什麽.........................\ n ");
語句ST = con . create statement();
ResultSet RS = ST . execute query(" select * from stu ");
while(rs.next()){
system . out . print(RS . getstring(" stuid ")+","+rs.getString("name")+","+rs.getString("sex")+","+rs.getString("age")+","+rs.getString("address")+","+RS . getstring(" tel ")+" \ n ");
}
}
//關閉數據庫
con . close();
}
}