JDBC資料庫的API對照執行個體學習

來源:互聯網
上載者:User

/*功能:      實現資料庫對資料的批處理,比如下面要輸入一千條資料,不能每次都要建立串連等操作之後插入一條再斷開再建立插入、、、、這樣的話很顯然是十分的浪費時間的。      當然了,批處理並不一定能到達很高的效率但是這是一種解決問題的方式。時間:20131003作者:煙大陽仔*/public class PiChuLi {public static void main(String[] args) throws SQLException {// TODO Auto-generated method stubcreate();}static void create() throws SQLException{Connection conn=null;PreparedStatement ps=null;ResultSet resultset=null;try {//2.建立串連conn=JdbcUtils.getConnection();//3.建立語句String sql="insert into user(name,birthday,money) values(?,?,?)";ps=conn.prepareStatement(sql,Statement.RETURN_GENERATED_KEYS);for(int i=0;i<1000;i++){ps.setString(1, "sdmf"+i);ps.setDate(2, new java.sql.Date(System.currentTimeMillis()));ps.setFloat(3, 345+i);ps.addBatch();}ps.executeBatch();} finally{JdbcUtils.free(resultset, ps, conn);}}}----------------------------------------------------------------------------------------------------------/*功能:      拿到剛插入的資訊的主鍵,這是API中的一個用於學習該方法時間:20131003作者:煙大陽仔*/public class OtherApi {public static void main(String[] args) throws SQLException {int id=create();System.out.println(id);}//拿到剛插入的資訊的主鍵static int create() throws SQLException{Connection conn=null;PreparedStatement ps=null;ResultSet resultset=null;try {//2.建立串連conn=JdbcUtils.getConnection();//3.建立語句String sql="insert into user(name,birthday,money) values('wy','2011-09-23','2894656')";ps=conn.prepareStatement(sql,Statement.RETURN_GENERATED_KEYS);ps.executeUpdate();resultset=ps.getGeneratedKeys();int id=0;if(resultset.next()){id=resultset.getInt(1);}return id;} finally{JdbcUtils.free(resultset, ps, conn);}}}-------------------------------------------------------------------------------------------------------------/*功能:      可滾動的結果集執行個體      分頁查詢時間:20131003作者:煙大陽仔*/public class ScrollAPIDemo {public static void main(String[] args) throws SQLException {scroll();}//可滾動的結果集執行個體//分頁查詢static void scroll() throws SQLException{Connection conn=null;Statement st=null;ResultSet resultset=null;try {//2.建立串連conn=JdbcUtils.getConnection();//3.建立語句st=conn.createStatement();//4.執行語句String sql="select id,name,birthday,money from user";resultset=st.executeQuery(sql);resultset.absolute(8);System.out.println();if(resultset.previous()){System.out.println(resultset.getObject("id")+"\t"+resultset.getObject("name")+"\t"+resultset.getObject("birthday")+"\t"+resultset.getObject("money")+"\t");}//分頁的一種方式不過效率比較低MySQL本身支援分頁//對MySQL來說設定分頁的話直接在sql語句中寫為://select id,name,birthday,money from user limit 100,10//也就是定位到第一百條資料顯示十條資料resultset.absolute(100);int i=0;while(resultset.next()&&i<10){i++;System.out.println(resultset.getObject("id")+"\t"+resultset.getObject("name")+"\t"+resultset.getObject("birthday")+"\t"+resultset.getObject("money")+"\t");}} finally{JdbcUtils.free(resultset, st, conn);}}}-------------------------------------------------------------------------------------------------------------/*功能:      可更新的結果集時間:20131003作者:煙大陽仔*/public class UpdateKeGengXin {/** * @param args * @throws SQLException  */public static void main(String[] args) throws SQLException {// TODO Auto-generated method stubread();}//可更新的結果集,查詢出來之後進行更新,不過這個瞭解就可以了static void read() throws SQLException{Connection conn=null;Statement st=null;ResultSet resultset=null;try {//2.建立串連conn=JdbcUtils.getConnection();//3.建立語句st=conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_UPDATABLE);//4.執行語句resultset=st.executeQuery("select id,name,birthday,money from user where id<10");//5.處理結果while(resultset.next()){System.out.println(resultset.getObject("id"));System.out.println(resultset.getObject("name"));System.out.println(resultset.getObject("birthday"));System.out.println(resultset.getObject("money"));String name=resultset.getString("name");if("wangwu".equals(name)){resultset.updateFloat("money", 123);resultset.updateRow();}}} finally{JdbcUtils.free(resultset, st, conn);}}}-----------------------------------------------------------------------------------------------------------/*功能:      資料庫的中繼資料資訊時間:20131003作者:煙大陽仔*/public class DBMD {//資料庫的原資訊public static void main(String[] args) throws SQLException {Connection conn=JdbcUtils.getConnection();DatabaseMetaData dbmd=conn.getMetaData();System.out.println(dbmd.getDatabaseMajorVersion());//資料庫的名稱System.out.println(dbmd.getDatabaseProductName());//資料庫的版本號碼System.out.println(dbmd.getDatabaseProductVersion());//是不是支援事務型System.out.println(dbmd.supportsTransactions());}}

相關文章

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.