statement接口中executeUpdate()、executeQuery()、execute()方法处理

清单如下:

 

 

import java.sql.Connection; import java.sql.DriverManager; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.ResultSetMetaData; import java.sql.SQLException; public class MyMeta { /** * statement接口中executeUpdate()、executeQuery()、execute()方法处理 * * @author caodehua * @exception * @version 2.0 */ public static Connection conn=null; public static PreparedStatement pst=null; public static ResultSet rst=null; // 加载jdbc驱动程序 static { try { Class.forName("org.gjt.mm.mysql.Driver").newInstance(); } catch (ClassNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (InstantiationException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IllegalAccessException e) { // TODO Auto-generated catch block e.printStackTrace(); } } // 建立数据库连接 public static Connection getConnection() { try { conn = DriverManager.getConnection("jdbc:mysql://localhost/test?user=root&password=root&useUnicode=true&characterEncoding=UTF-8"); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } return conn; } public static void QueryMode1() throws Exception{ conn=getConnection(); // pst=conn.prepareStatement("update student set name=? where id=?"); // pst=conn.prepareStatement("update student set name=?", 1); // pst=conn.prepareStatement("update student set name=?", new int[]{1,2,3}); // pst=conn.prepareStatement("update student set name=?",new String[]{"name","age","sex"}); // pst.setString(1, "zhangmei"); // // conn.setAutoCommit(false); // int rows =pst.executeUpdate(); // if(rows>0){ // conn.commit(); // conn.setAutoCommit(true); // System.out.println("update data successful"); // }else // { // conn.rollback(); // System.out.println("update data failureful"); // } pst=conn.prepareStatement("select * from student", rst.TYPE_FORWARD_ONLY, rst.CONCUR_UPDATABLE); rst=pst.executeQuery(); while(rst.next()){ if(rst.first()){ // rst.moveToCurrentRow(); rst.deleteRow(); } System.out.println(rst.getInt("id")+"/t"+rst.getString("name")+"/t"+ rst.getInt("age")+"/t"+rst.getString("sex")+"/t"+ rst.getString("phone")+"/t"+rst.getString("mail")); } closeConnection(rst, pst, conn); } public static void QueryMode2()throws Exception{ conn=getConnection(); pst=conn.prepareStatement("select * from student"); // rst=pst.executeQuery(); // System.out.println(conn.isClosed()); // while(rst.next()){ // // System.out.println(rst.getString("name")+"/t"+rst.getInt("age")+"/t"+rst.getString("sex")); // } boolean flag=pst.execute(); if(flag){ rst=pst.getResultSet(); ResultSetMetaData rdata=rst.getMetaData(); int count=rdata.getColumnCount(); while(rst.next()){ for(int i=0;i<count;i++){ System.out.print(rst.getString(i+1)+"/t"); } System.out.println(); } }else { System.out.println("it is SQL statement reflect"+pst.getUpdateCount()+"codes"); } closeConnection(rst, pst, conn) ; } public static void QueryMode3()throws Exception{ conn=getConnection(); pst=conn.prepareStatement("update student set phone=?"); pst.setString(1, "13469595923"); // int rows =pst.executeUpdate(); // if(rows>0){ // System.out.println("update data successful"); // // }else // { // System.out.println("update data failureful"); // } closeConnection(null, pst, conn); } // 关闭数据库连接 public static void closeConnection(ResultSet rst, PreparedStatement pst, Connection conn) throws Exception { try { if (rst != null) { rst.close(); } if (pst != null) { pst.close(); } if (conn != null) { conn.close(); } } catch (Exception e) { // TODO: handle exception e.printStackTrace(); } } public static void main(String[] args)throws Exception { // TODO Auto-generated method stub // QueryMode1(); // QueryMode2(); // QueryMode3(); } }

你可能感兴趣的:(exception,数据库,jdbc,String,null,import)