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

	}

}

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值