使用Statement执行sql语句

3.1 执行DDL语句

/**
	 * 执行DDL语句(创建表)
	 */
	@Test
	public void test1(){
		Statement stmt = null;
		Connection conn = null;
		try {
			//1.驱动注册程序
			Class.forName("com.mysql.jdbc.Driver");
			
			//2.获取连接对象
			conn = DriverManager.getConnection(url, user, password);
			
			//3.创建Statement
			stmt = conn.createStatement();
			
			//4.准备sql
			String sql = "CREATE TABLE student(id INT PRIMARY KEY AUTO_INCREMENT,NAME VARCHAR(20),gender VARCHAR(2))";
			
			//5.发送sql语句,执行sql语句,得到返回结果
			int count = stmt.executeUpdate(sql);
			
			//6.输出
			System.out.println("影响了"+count+"行!");
		} catch (Exception e) {
			e.printStackTrace();
			throw new RuntimeException(e);
		} finally{
			//7.关闭连接(顺序:后打开的先关闭)
			if(stmt!=null)
				try {
					stmt.close();
				} catch (SQLException e) {
					e.printStackTrace();
					throw new RuntimeException(e);
				}
			if(conn!=null)
				try {
					conn.close();
				} catch (SQLException e) {
					e.printStackTrace();
					throw new RuntimeException(e);
				}
		}
	}


3.2 执行DML语句

/**
 * 使用Statement执行DML语句
 * @author APPle
 *
 */
public class Demo2 {
private String url = "jdbc:mysql://localhost:3306/day17";
private String user = "root";
private String password = "root";
 
/**
 * 增加
 */
@Test
public void testInsert(){
Connection conn = null;
Statement stmt = null;
try {
//通过工具类获取连接对象
conn = JdbcUtil.getConnection();
//3.创建Statement对象
stmt = conn.createStatement();
//4.sql语句
String sql = "INSERT INTO student(NAME,gender) VALUES('李四','女')";
//5.执行sql
int count = stmt.executeUpdate(sql);
System.out.println("影响了"+count+"行");
} catch (Exception e) {
e.printStackTrace();
throw new RuntimeException(e);
} finally{
//关闭资源
/*if(stmt!=null)
try {
stmt.close();
} catch (SQLException e) {
e.printStackTrace();
throw new RuntimeException(e);
}
if(conn!=null)
try {
conn.close();
} catch (SQLException e) {
e.printStackTrace();
throw new RuntimeException(e);
}*/
JdbcUtil.close(conn, stmt);
}
}
/**
 * 修改
 */
@Test
public void testUpdate(){
Connection conn = null;
Statement stmt = null;
//模拟用户输入
String name = "陈六";
int id = 3;
try {
/*//1.注册驱动
Class.forName("com.mysql.jdbc.Driver");
//2.获取连接对象
conn = DriverManager.getConnection(url, user, password);*/
//通过工具类获取连接对象
conn = JdbcUtil.getConnection();
//3.创建Statement对象
stmt = conn.createStatement();
//4.sql语句
String sql = "UPDATE student SET NAME='"+name+"' WHERE id="+id+"";
System.out.println(sql);
//5.执行sql
int count = stmt.executeUpdate(sql);
System.out.println("影响了"+count+"行");
} catch (Exception e) {
e.printStackTrace();
throw new RuntimeException(e);
} finally{
//关闭资源
/*if(stmt!=null)
try {
stmt.close();
} catch (SQLException e) {
e.printStackTrace();
throw new RuntimeException(e);
}
if(conn!=null)
try {
conn.close();
} catch (SQLException e) {
e.printStackTrace();
throw new RuntimeException(e);
}*/
JdbcUtil.close(conn, stmt);
}
}
/**
 * 删除
 */
@Test
public void testDelete(){
Connection conn = null;
Statement stmt = null;
//模拟用户输入
int id = 3;
try {
/*//1.注册驱动
Class.forName("com.mysql.jdbc.Driver");
//2.获取连接对象
conn = DriverManager.getConnection(url, user, password);*/
//通过工具类获取连接对象
conn = JdbcUtil.getConnection();
//3.创建Statement对象
stmt = conn.createStatement();
//4.sql语句
String sql = "DELETE FROM student WHERE id="+id+"";
System.out.println(sql);
//5.执行sql
int count = stmt.executeUpdate(sql);
System.out.println("影响了"+count+"行");
} catch (Exception e) {
e.printStackTrace();
throw new RuntimeException(e);
} finally{
//关闭资源
/*if(stmt!=null)
try {
stmt.close();
} catch (SQLException e) {
e.printStackTrace();
throw new RuntimeException(e);
}
if(conn!=null)
try {
conn.close();
} catch (SQLException e) {
e.printStackTrace();
throw new RuntimeException(e);
}*/
JdbcUtil.close(conn, stmt);
}
}
}

3.3 执行DQL语句

/**
 * 使用Statement执行DQL语句(查询操作)
 * @author APPle
 */
public class Demo3 {

	@Test
	public void test1(){
		Connection conn = null;
		Statement stmt = null;
		try{
			//获取连接
			conn = JdbcUtil.getConnection();
			//创建Statement
			stmt = conn.createStatement();
			//准备sql
			String sql = "SELECT * FROM student";
			//执行sql
			ResultSet rs = stmt.executeQuery(sql);
			
			//移动光标
			/*boolean flag = rs.next();
			
			flag = rs.next();
			flag = rs.next();
			if(flag){
				//取出列值
				//索引
				int id = rs.getInt(1);
				String name = rs.getString(2);
				String gender = rs.getString(3);
				System.out.println(id+","+name+","+gender);
				
				//列名称
				int id = rs.getInt("id");
				String name = rs.getString("name");
				String gender = rs.getString("gender");
				System.out.println(id+","+name+","+gender);
			}*/
			
			//遍历结果
			while(rs.next()){
				int id = rs.getInt("id");
				String name = rs.getString("name");
				String gender = rs.getString("gender");
				System.out.println(id+","+name+","+gender);
			}
			
		}catch(Exception e){
			e.printStackTrace();
			throw new RuntimeException(e);
		}finally{
			JdbcUtil.close(conn, stmt);
		}
	}
}



  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: Java语言中的statement可以用来执行SQL语句。它是一个接口,可以通过Connection对象的createStatement()方法来创建。执行SQL语句的方法有execute()、executeQuery()和executeUpdate(),分别用于执行查询语句、返回结果集的查询语句和更新语句。在执行SQL语句后,可以通过ResultSet对象来获取查询结果。 ### 回答2: Java中的Statement用于执行SQL语句,并且支持包括查询、插入、更新和删除等操作。在使用Statement之前,需要先建立与数据库的连接。一旦连接建立,就可以使用该链接创建一个Statement对象。创建Statement对象的代码如下: Statement stmt = conn.createStatement(); //conn为连接对象 在创建Statement对象之后,就可以使用它来执行SQL语句执行SQL语句有两种方式:一种是使用execute()方法来执行SQL语句,该方法可以用于执行任何SQL语句,包括查询、插入等操作。另一种是使用executeQuery()方法来执行查询语句。下面分别介绍这两种方法的使用方法。 1.执行任意SQL语句执行任意SQL语句时,可以使用execute()方法。该方法返回一个Boolean值,表示执行SQL语句是否成功。如果执行成功,返回值为true,否则返回false。 假设要执行如下SQL语句: String sql = "INSERT INTO student(id, name, age) VALUES (1, 'Tom', 18)"; 则可以使用如下代码来执行: boolean result = stmt.execute(sql); 如果执行成功,result的值为true。 2.执行查询语句 在执行查询语句时,需要使用executeQuery()方法。该方法返回一个ResultSet对象,用于存放查询结果。 假设要执行如下SQL查询语句: String sql = "SELECT * FROM student WHERE age = 18"; 则可以使用如下代码来执行: ResultSet rs = stmt.executeQuery(sql); 查询结果存放在ResultSet对象中,可以使用ResultSet对象中的方法来获取查询结果。例如,可以使用rs.next()方法来将ResultSet游标指向下一行,并返回该行是否存在;使用rs.getString()方法来获取某个列的字符串值等。如果查询到的数据为空,rs.next()方法返回false。 在使用Statement对象后,需要将它关闭以释放资源。可以使用如下代码来关闭Statement对象: stmt.close(); 以上就是使用Statement执行SQL语句的方法。在使用Statement时需要注意的是,SQL语句中涉及到的参数需要使用预编译PreparedStatement来处理,以防止SQL注入等问题的发生。 ### 回答3: Java statement是一个接口,是Java程序与关系型数据库之间的桥梁,用于执行SQL语句。在Java程序中,我们通过建立数据库连接,并使用Statement对象来实现与数据库的交互。Statement是一个接口,支持执行SQL语句的方法,比如executeQuery(),executeUpdate()和execute()。 Statement的主要作用是将SQL命令传递给关系型数据库管理系统(RDBMS),并返回查询结果或更新的行数。执行SQL语句时,Statement调用JDBC驱动程序来创建和执行相应的数据库命令。执行SQL语句期间,在StatementJDBC驱动程序之间进行了实时的交互,实现了高效的数据交换。 Statement中最常用的方法是executeQuery()和executeUpdate()。executeQuery()用于执行查询语句,查询结果返回ResultSet对象,其中包含了满足查询条件的所有数据行。而executeUpdate()用于执行更新语句,比如插入、修改或删除数据,执行结果被返回为更新的行数。 Statement还可以通过设置参数来执行预定义的参数化查询。参数化查询是预防SQL注入攻击的一种方式,它通过使用特定格式的占位符(?)来代替SQL语句中的实际参数,避免了对输入参数的未经处理。参数可以从外部输入,可以是任何类型的常量或变量。 总之,Java Statement是一个非常强大的接口,允许Java程序轻松与关系型数据库进行交互。通过StatementJava程序可以执行各种类型的SQL语句,并获取查询结果或更新行数,同时还能够支持参数化查询来防止SQL注入攻击。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值