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();
	}
}

你可能感兴趣的:(jdbc连接数据库)