`
djun100
  • 浏览: 181288 次
  • 性别: 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();

	}

}

分享到:
评论

相关推荐

    JDBC中PreparedStatement接口提供的execute、executeQuery和executeUpdate之间的区别及用法

    JDBC 中的 PreparedStatement 接口提供了三种执行 SQL 语句的方法:executeQuery、executeUpdate 和 execute。这些方法的使用取决于 SQL 语句所产生的内容。 _executeQuery 方法_ executeQuery 方法用于产生单个...

    execute、executeQuery和executeUpdate之间的区别

    在Java开发中,尤其涉及到数据库操作时,`execute`, `executeQuery` 和 `executeUpdate` 这三个方法是JDBC编程中非常重要的一部分。它们分别适用于不同类型的SQL语句,理解这些方法的区别对于正确高效地执行数据库...

    execute、executeQuery和executeUpdate之间的区别.doc

    ### JDBC中Statement接口提供的execute、executeQuery和executeUpdate的区别 #### 一、概述 在Java开发过程中,尤其是在处理数据库操作时,我们经常会遇到`execute`、`executeQuery`和`executeUpdate`这三个方法。...

    MySQL execute、executeUpdate、executeQuery三者的区别

    总之,`execute()`, `executeUpdate()` 和 `executeQuery()` 是在JDBC中处理SQL语句的关键方法。它们根据SQL语句的性质(查询、更新或定义)返回不同的结果类型,帮助开发者更有效地与数据库进行交互。理解这些方法...

    excute,excuteUpdate,excuteQuery的区别跟用法

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

    Oracle相关接口使用方法

    而`Stream`接口提供了数据流读取功能,如`readBuffer`方法用于读取数据缓冲区中的内容。 ```cpp ResultSet *rs = stmt-&gt;executeQuery("SELECT * FROM employees"); Stream *stream = rs-&gt;getStream(3); // 假设第三...

    JDBC数据库访问(七步).ppt

    可以通过调用 Statement 的 executeQuery、executeUpdate 或 execute 方法来执行 SQL 语句。 第六步:处理结果集 在执行 SQL 语句后,需要处理结果集。可以通过调用 Statement 的 getResultSet 方法来获取结果集,...

    excute、excuteQuery和excuteUpdate的使用方法以及区别

    在Java的数据库编程中,`Statement`接口提供了`execute()`, `executeQuery()`和`executeUpdate()`这三个方法,它们各自负责处理不同的SQL语句类型,理解它们的使用方法和区别至关重要。 1. **execute() 用途及格式*...

    java数据库连接Statement

    `Statement`提供了多种执行SQL语句的方法,包括`executeQuery()`, `executeUpdate()` 和 `execute()`等。这些方法分别适用于不同类型的操作: - **`executeQuery()`**:用于执行返回结果集(`ResultSet`)的SQL语句...

    第10章 JDBC-课后习题1

    3. `Statement`接口的`executeUpdate(String sql)`方法用于执行SQL中的`insert`、`update`和`delete`语句。 4. `PreparedStatement`是`Statement`的子接口,用于执行预编译的SQL语句。 5. `ResultSet`接口中定义了...

    java简答题

    Statement 接口提供了 executeQuery、executeUpdate 和 execute 三种执行 SQL 语句的方法。 四、多线程 1. 进程和线程的区别是什么?进程是操作系统分配资源的基本单位,线程是操作系统调度的基本单位。 2. Java ...

    JDBC常考知识点,常用api

    `Connection`接口提供了创建Statement、PreparedStatement和CallableStatement对象的方法,以执行SQL语句。 - `createStatement()`:返回一个Statement对象,用于执行非参数化的SQL语句。 - `prepareStatement...

    java数据库连接PrepareStatement

    设置了参数后,可以通过 `execute`、`executeQuery` 或 `executeUpdate` 方法来执行 SQL 语句: - `execute`:执行任意类型的 SQL 语句,返回一个布尔值,表示结果是否为 `ResultSet`。 - `executeQuery`:执行查询...

    JDBC连接数据库总结

    它提供了三种执行SQL语句的方法:`executeUpdate()`、`executeQuery()`和`execute()`。其中,`executeUpdate()`方法用于执行增、删、改操作,`executeQuery()`方法用于执行查询操作。 在上面的代码中,`...

    JDBCWeb试题(卷)答案解析.doc

    本篇试题主要涵盖了JDBC的基础知识,包括JDBC程序的执行顺序、驱动程序的种类、SQL命令的执行方式、Statement接口的方法以及异常处理等。 1. JDBC程序通常按照以下顺序编写: - 注册JDBC Driver - 获得与数据库的...

    对JSP数据库连接类使用方法的详细讲解

    Statement类是执行SQL语句的主要接口。我们可以通过Connection创建Statement对象,然后使用它来执行SQL。例如,`stmt=cn.createStatement();`创建Statement对象后,我们可以执行查询语句,如`rs=stmt.executeQuery(...

    JAVA学习使用JDBC的高级特征创建应用程序PPT教案学习.pptx

    在JDBC中,PreparedStatement接口是Statement接口的一个子接口,它提供了预编译的SQL语句,这对于处理动态参数和提高性能非常关键。下面将详细讨论PreparedStatement接口的高级特性以及如何在应用程序中使用它们。 ...

    如何获得PreparedStatement最终执行的sql语句

    在实际应用中,如果需要获取动态构建的SQL,可能需要自定义一个`PreparedStatement`的代理类,覆盖`execute`或`executeQuery`方法,然后在这个代理类中拼接和打印出最终的SQL。但这需要对JDBC有深入的理解,并且需要...

    Java软件开发实战 Java基础与案例开发详解 18-2 JDBC类和接口 共16页.pdf

    `Statement`接口用于执行简单的SQL语句,如查询或更新数据库中的数据。 - **作用**: 发送SQL语句到数据库。 - **关键方法**: - `execute(String sql)`:执行给定的SQL语句。 - `executeQuery(String sql)`:执行...

Global site tag (gtag.js) - Google Analytics