`
djun100
  • 浏览: 167534 次
  • 性别: Icon_minigender_1
  • 来自: 大连
文章分类
社区版块
存档分类
最新评论

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

	}

}

分享到:
评论

相关推荐

    MySQL execute、executeUpdate、executeQuery三者的区别

    execute、executeUpdate、executeQuery三者的区别(及返回值) 一、boolean execute(String sql) 允许执行查询语句、更新语句、DDL语句。 返回值为true时,表示执行的是查询语句,可以通过getResultSet方法获取结果...

    excute,excuteUpdate,excuteQuery的区别跟用法

    Statement 接口提供了三种执行 SQL 语句的方法:executeQuery、executeUpdate 和 execute。使用哪一个方法由 SQL 语句所产生的内容决定。 方法executeQuery 用于产生单个结果集的语句,例如 SELECT 语句。 被...

    踩踩踩JDBC六大步骤

    1、JDBC编程六大步: ...1.class.forName(driver) 2.new oracle.jdbc.driver.OracleDriver(); 3.java -Djdbc.drivers=oracle.jdbc.driver.OracleDriver jdbc.drivers=System.gerProperties();...5)获得并处理结果集

    JAVA的JDBC详解,面试必问

    * Statement是一个接口,它表示语句对象。 * executeQuery():只能执行查询语句(DQL),返回ResultSet对象,即结果集对象。 * executeUpdate():执行DML语句(增删改语句)和DDL语句(数据定义语言,Create、...

    JDBC访问数据库的步骤

    6. (1)应用Statement接口 获取Statement对象,通过Statement对象执行SQL语句: Statement stmt=con.createStatement(); 执行SQL查询,返回给结果集对象: ResultSet rs=stmt. executeQuery(“select * from 表名...

    JDBC详解HTML-JDBC.pp

    1、JDBC(Java Database Connection):java连接数据库统一接口API,底层主要通过直接的JDBC驱动和 JDBC-ODBC桥驱动实现与数据库的连接。 1&gt;.JDBC驱动程序类型: &lt;1&gt;.JDBC-ODBC桥加ODBC驱动程序:需要ODBC驱动,适合...

    JAVA连接ORACLE数据库方法及测试

    rs = st.executeQuery(sql);// 执行操作 } catch (Exception e) { System.out.println("查询出错"); } return rs; } // 更新方法 public void execute(String sql) { Connection c = create();//...

    jdbc基础和参考

    1.DriverManager(中的getConnection其实也是调用的Driver.connect方法) getConnection(url);//没有用户名密码 //将用户名密码存放在java.util.Properties对象中 getConnection(url,properties); get...

    Java面试宝典2020修订版V1.0.1.doc

    35、Statement 中execute、executeUpdate、executeQuery这三者的区别 78 36、jdbc中怎么做批量处理的? 80 37、什么是json 83 38、json与xml的区别 83 39、XML和HTML的区别? 84 40、XML文档定义有几种形式?它们...

    学生信息系统

    ResultSet re=stmt.executeQuery(sql); return re; } catch(Exception e){ System.out.println("getResult------"+e.toString()); return null; } } public boolean executeSql(String sql){ ...

Global site tag (gtag.js) - Google Analytics