jdbc鏈接數據庫

public class TestConnection {
	public static void main(String[] args) throws Exception {
		//兩種方法加載驅動,一種是forname,另外一種是經過設置系統屬性
//		Class.forName("org.gjt.mm.mysql.Driver");
		System.setProperty("jdbc.drivers","org.gjt.mm.mysql.Driver");
		Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/auction", "root", "19890511");
		//建立statement或者preparedstatement來進行數據庫操做,一般鼓勵使用preparedstatement,由於這個能夠對屢次執行的sql語句進行優化
		//能夠相對提升速度,並能夠防止部分sql注入攻擊
		Statement stat = conn.createStatement();		
		ResultSet set = stat.executeQuery("select * from kind ");
		while(set.next()){//resultset和iterator不一樣之處是,一開始resultset的指針是在結果集第一個的前面,使用next才能指向
			//第一個結果集,iterator是直接在第一個,因此使用hasnext來判斷是否有下一個
			System.out.print(set.getString(1));
			System.out.print(set.getString(2));
			System.out.println(set.getString(3));
		}
		set.close();
		
		ResultSet set2 = stat.executeQuery("select * from kind where kind_id = " + 6);
		while (set2.next()) {
			System.out.println(set2.getString(2));
		}
		set2.close();
		
		if(stat.execute("select * from kind")){
			ResultSet set3 = stat.getResultSet();
			while (set3.next()) {
				System.out.println("set3:" + set3.getString(1));
			}
			
			System.out.println("set3.previout:" + set3.previous());
			while(set3.previous()){
				System.out.println("set3:" + set3.getString(1));
			}
			set3.close();
		}
		
		stat.executeUpdate("update kind set kind_id = 7 where kind_id = 6");
		System.out.println(stat.getUpdateCount());
		
		stat.execute("delete from kind where kind_id = 7 ");
		System.out.println(stat.getUpdateCount());
		stat.close();
		
		
		PreparedStatement prest = conn.prepareStatement("insert into kind (kind_id,kind_name,kind_desc) values (?,?,?)");
		prest.setInt(1, 7);
		prest.setString(2, "gg");
		prest.setString(3, "test");
		prest.executeUpdate();
		System.out.println(prest.getUpdateCount() + ":prest");
		prest.close();
		
		PreparedStatement prest2 = conn.prepareStatement("update kind set kind_name = 'delete' where kind_id = 7 ");
		prest2.execute();
		System.out.println("prest2:" + prest2.getUpdateCount());
		prest2.close();
		
		// 自動生成主鍵
		PreparedStatement prest8 = conn.prepareStatement("insert into kind (kind_name,kind_desc) values (?,?)",PreparedStatement.RETURN_GENERATED_KEYS);
		prest8.setString(1, "gg8");
		prest8.setString(2, "test8");
		prest8.executeUpdate();
		prest8.close();
		
		// 可更新可滾動的結果集
		PreparedStatement prest9 = conn.prepareCall("select * from kind ", ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);
		prest9.execute();
		
		//得到上次執行影響的結果集
		ResultSet rs = prest9.getResultSet();
		// next爲下一個
		while(rs.next()){
			System.out.println("next:" + rs.getString(1));
		}
		
		//absolute
		if(rs.absolute(2)){
			System.out.println("absolute:" + rs.getString(1));
		}
		
		// previous 爲上一個
		while(rs.previous()){
			System.out.println("previout:" + rs.getString(1));
		}
		
		
		// 可更新可滾動的結果集
		Statement prest92 = conn.createStatement(ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_READ_ONLY);
		
		//得到上次執行影響的結果集
		ResultSet rs2 = prest92.executeQuery("select * from kind");
		// next爲下一個
		while(rs2.next()){
			System.out.println("next:" + rs2.getString(1));
		}
		
		//absolute
		if(rs2.absolute(2)){
			System.out.println("absolute:" + rs2.getString(1));
		}
		
		// previous 爲上一個
		while(rs2.previous()){
			System.out.println("previout:" + rs2.getString(1));
		}	
		
		conn.close();
	}
}
相關文章
相關標籤/搜索