JDBC增删改查操作

学期末233333

(1)java.sql.Connection: 负责连接数据库。

(2)java.sql.Statement: 负责执行数据库SQL语句。

(3)java.sql.ResultSet: 负责存放查询结果。


添加数据:

public class Insert {
	public static void main(String args[]) throws Exception {
		//数据库数据乱码处理
		String url = "jdbc:mysql://localhost:3306/library?useUnicode=true&characterEncoding=GBK";
		String user = "root";
		String password = "123456";
		Class.forName("com.mysql.jdbc.Driver");
		Connection conn = DriverManager.getConnection(url, user, password);
		
		//添加数据(1)
		Statement stat = conn.createStatement();
		String sql = "insert into t_book(bookid, bookname, bookprice) values('0015','数据库原理',32)";
		int i = stat.executeUpdate(sql);
		System.out.println("添加"+i+"行数据");
		stat.close();
		
		//添加数据(2)
		sql = "insert into t_book(bookid,bookname,bookprice) values(?,?,?)";
		PreparedStatement ps = conn.prepareStatement(sql);
		ps.setString(1, "0015");
		ps.setString(2, "JavaEE");
		ps.setInt(3, 233);
		i = ps.executeUpdate();
		System.out.println("添加"+i+"行数据");
		ps.close();
		
		conn.close();
	}
}
删除数据:

public class Delete {
	public static void main(String args[]) throws Exception {
		String url = "jdbc:mysql://localhost:3306/library?useUnicode=true&characterEncoding=GBK";
		String user = "root";
		String password = "123456";
		Class.forName("com.mysql.jdbc.Driver");
		Connection conn = DriverManager.getConnection(url,user,password);
		
		//删除数据(1)
		Statement stat = conn.createStatement();
		String sql = "delete from t_book where bookid = '0015'";
		int i = stat.executeUpdate(sql);
		System.out.println("删除"+i+"行数据");
		stat.close();
		
		//删除数据(2)
		sql = "delete from t_book where bookid = ?";
		PreparedStatement ps = conn.prepareStatement(sql);
		ps.setString(1, "0015");
		i = ps.executeUpdate();
		System.out.println("删除"+i+"行数据");
		ps.close();
		
		conn.close();
	}
}
更新数据:

public class Update {
	public static void main(String args[]) throws Exception {
		String url = "jdbc:mysql://localhost:3306/library?useUnicode=true&characterEncoding=GBK";
		String user = "root";
		String password = "123456";
		Class.forName("com.mysql.jdbc.Driver");
		Connection conn = DriverManager.getConnection(url,user,password);
		
		//更新数据(1)
		Statement stat = conn.createStatement();
		String sql = "update t_book set bookprice = 233 where bookid = 0002";
		int i = stat.executeUpdate(sql);
		System.out.println("成功更新"+i+"行数据");
		stat.close();
		
		//更新数据(2)
		sql = "update t_book set bookprice = ? where bookid = ?";
		PreparedStatement ps = conn.prepareStatement(sql);
		ps.setInt(1, 332);
		ps.setString(2, "0002");
		i = ps.executeUpdate();
		System.out.println("成功更新"+i+"行数据");
		ps.close();
		
		conn.close();
	}
}
查询数据:

public class Select {
	public static void main(String args[]) throws Exception{
		String url = "jdbc:mysql://localhost:3306/library?useUnicode=true&characterEncoding=GBK";
		String user = "root";
		String password = "123456";
		Class.forName("com.mysql.jdbc.Driver");
		Connection conn = DriverManager.getConnection(url,user,password);
		
		//查询数据(1)
		Statement stat = conn.createStatement();
		String sql = "select * from t_book";
		ResultSet rs = stat.executeQuery(sql);
		stat.close();
		
		//查询数据(2)
		sql = "select * from t_book where bookid = ?";
		PreparedStatement ps = conn.prepareStatement(sql);
		ps.setString(1, "0002");
		rs = ps.executeQuery();
		
		while(rs.next()){
			String bookid = rs.getString("bookid");
			String bookname = rs.getString("bookname");
			int bookprice = rs.getInt("bookprice");
			System.out.println(bookid+" "+bookname+" "+bookprice);
		}
		ps.close();
		
		rs.close();
		conn.close();
	}
}





评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值