一竿开、jdbc連接mysql數(shù)據(jù)庫(kù)
Public static void main(String [] args) throws ClassNotFoundException,SQLException{
String url=”jdbc:mysql://localhost:3306/dataBaseName?useUnicode=true&characterEncoding=ut ?????????????????????????????f-8”;
String userName=”root”;
String passWord=”root”;
//1.加載數(shù)據(jù)庫(kù)驅(qū)動(dòng)
Class.forName(“com.mysql.jdbc.Driver”);
//2.獲得數(shù)據(jù)庫(kù)連接
Connection conn=DriverManager.getConnection(url,userName,passWord);
//3.1通過(guò)數(shù)據(jù)庫(kù)的連接捎琐,使用Statement(無(wú)法防止sql注入)對(duì)數(shù)據(jù)庫(kù)進(jìn)行 insert、 //update叉庐、delete舒帮、select 操作
//3.1 select操作使用Statement 的 st.executeQuery(sql);
//3.1 insert、update陡叠、delete操作使用st.executeUpdate(sql);
Statement st=conn.createStatement();
ResultSet rs=st.executeQuery(“select * from user”);
//4.1處理返回結(jié)果
While(rs.next()){
System.out.println(rs.getString(“name”));
}
//5.1關(guān)閉資源
rs.close();
st.close();
conn.close();
//3.2通過(guò)數(shù)據(jù)庫(kù)的連接玩郊, PreparedStatement (可以防止sql注入)對(duì)數(shù)據(jù)庫(kù)進(jìn)行 //insert、update枉阵、delete译红、select 操作
String sql=”insert into user(id,name) values(?,?)”;
PreparedStatement pst=conn.preparedStatement(sql);
pst.setString(1,”1”);
pst.setString(2,”lty”);
pst.execute();
//5.2關(guān)閉資源
pst.close();
conn.close();
}
二、jdbc連接oracle
/**
* 一個(gè)非常標(biāo)準(zhǔn)的連接Oracle數(shù)據(jù)庫(kù)的示例代碼
*/
public void testOracle()
{
? ? Connection con = null;// 創(chuàng)建一個(gè)數(shù)據(jù)庫(kù)連接
? ? PreparedStatement pre = null;// 創(chuàng)建預(yù)編譯語(yǔ)句對(duì)象兴溜,一般都是用這個(gè)而不用Statement
? ? ResultSet result = null;// 創(chuàng)建一個(gè)結(jié)果集對(duì)象
? ? try
? ? {
? ? ? ? Class.forName("oracle.jdbc.driver.OracleDriver");// 加載Oracle驅(qū)動(dòng)程序
? ? ? ? System.out.println("開(kāi)始嘗試連接數(shù)據(jù)庫(kù)侦厚!");
? ? ? ? String url = "jdbc:oracle:" + "thin:@127.0.0.1:1521:XE";// 127.0.0.1是本機(jī)地址耻陕,XE是精簡(jiǎn)版Oracle的默認(rèn)數(shù)據(jù)庫(kù)名
? ? ? ? String user = "system";// 用戶(hù)名,系統(tǒng)默認(rèn)的賬戶(hù)名
? ? ? ? String password = "147";// 你安裝時(shí)選設(shè)置的密碼
? ? ? ? con = DriverManager.getConnection(url, user, password);// 獲取連接
? ? ? ? System.out.println("連接成功!");
? ? ? ? String sql = "select * from student where name=?";// 預(yù)編譯語(yǔ)句刨沦,“诗宣?”代表參數(shù)
? ? ? ? pre = con.prepareStatement(sql);// 實(shí)例化預(yù)編譯語(yǔ)句
? ? ? ? pre.setString(1, "劉顯安");// 設(shè)置參數(shù),前面的1表示參數(shù)的索引想诅,而不是表中列名的索引
? ? ? ? result = pre.executeQuery();// 執(zhí)行查詢(xún)召庞,注意括號(hào)中不需要再加參數(shù)
? ? ? ? while (result.next())
? ? ? ? ? ? // 當(dāng)結(jié)果集不為空時(shí)
? ? ? ? ? ? System.out.println("學(xué)號(hào):" + result.getInt("id") + "姓名:"
? ? ? ? ? ? ? ? ? ? + result.getString("name"));
? ? }
? ? catch (Exception e)
? ? {
? ? ? ? e.printStackTrace();
? ? }
? ? finally
? ? {
? ? ? ? try
? ? ? ? {
? ? ? ? ? ? // 逐一將上面的幾個(gè)對(duì)象關(guān)閉,因?yàn)椴魂P(guān)閉的話會(huì)影響性能来破、并且占用資源
? ? ? ? ? ? // 注意關(guān)閉的順序篮灼,最后使用的最先關(guān)閉
? ? ? ? ? ? if (result != null)
? ? ? ? ? ? ? ? result.close();
? ? ? ? ? ? if (pre != null)
? ? ? ? ? ? ? ? pre.close();
? ? ? ? ? ? if (con != null)
? ? ? ? ? ? ? ? con.close();
? ? ? ? ? ? System.out.println("數(shù)據(jù)庫(kù)連接已關(guān)閉!");
? ? ? ? }
? ? ? ? catch (Exception e)
? ? ? ? {
? ? ? ? ? ? e.printStackTrace();
? ? ? ? }
? ? }
}