JDBC:Statement语句

7 篇文章 0 订阅

1、Statement语句是SQL语句的描述,使用它可以操作各种SQL语句,包括DDL(数据定义语句,如创建表)、DML(数据操作语言,如insert,update,delete)、DSL(数据控制语言)等。
2、使用Statement创建表

//创建表
	static void createTable() {
		Connection conn = DBUtil.open();
		String sql = "create table UserTbl(id int primary key auto_increment,name varchar(20))";
		try {
			Statement stmt = conn.createStatement();
			stmt.execute(sql);
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			DBUtil.close(conn);
		}
	}

3、插入数据

//添加数据
	static void insert() {
		Connection conn = DBUtil.open();
		String sql = "insert into UserTbl(name) values('guyang')";
		try {
			Statement stmt = conn.createStatement();
			stmt.executeUpdate(sql);
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			DBUtil.close(conn);
		}
	}

4、更新数据

static void update() {
		Connection conn = DBUtil.open();
		String sql = "update UserTbl set name = 'big guyang' where id >3";
		try {
			Statement stmt = conn.createStatement();
			stmt.executeUpdate(sql);
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			DBUtil.close(conn);
		}
	}

5、删除数据

static void delete() {
		Connection conn = DBUtil.open();
		String sql = "delete from UserTbl where id = 1";
		try {
			Statement stmt = conn.createStatement();
			stmt.executeUpdate(sql);
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			DBUtil.close(conn);
		}
	}

6、查询数据

//查询
	static void query() {
		Connection conn = DBUtil.open();
		String sql = "select id,name from UserTbl";
		try {
			Statement stmt = conn.createStatement();
			ResultSet rs = stmt.executeQuery(sql);
			//遍历结果集
			while(rs.next()) {
				int id = rs.getInt(1);
				String name = rs.getString(2);
				System.out.println(id+","+name);
			}
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			DBUtil.close(conn);
		}
	}

将数据封装成对象方式查询

public class User {
	private int id;
	private String name;
	public int getId() {
		return id;
	}
	public void setId(int id) {
		this.id = id;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public String toString() {
		return id+":"+name;
	}
}
//查询
	static List<User> query2() {
		Connection conn = DBUtil.open();
		String sql = "select id,name from UserTbl";
		try {
			Statement stmt = conn.createStatement();
			ResultSet rs = stmt.executeQuery(sql);
			List<User> list = new ArrayList<User>();
			//遍历结果集
			while(rs.next()) {
				int id = rs.getInt(1);
				String name = rs.getString(2);
				User u = new User();
				u.setId(id);
				u.setName(name);
				list.add(u);
//				System.out.println(id+","+name);
			}
			System.out.println(list);
			return list;
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			DBUtil.close(conn);
		}
		return null;
	}

项目路径:E:\eclipse-workspace\JDBCdemo

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值