Java 实现对象插入、删除、更新、集合返回、数据库中数据总数获取

package jdbc;

import java.sql.Driver;
import java.sql.DriverManager;
import java.sql.Connection;
import java.sql.Statement;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import file.Hero;

public class TestJDBC{
	public static void main(String[] args){
		TestJDBC a = new TestJDBC();
//		System.out.println(a.getTotal());
		Hero hero = new Hero();
		hero.name = "A";
		hero.hp = 20;
		hero.damage = 15;
//		a.add(hero);
//		Hero hero = a.get(55);
//		System.out.println(hero.damage);
		a.update(hero,49);
//		a.delete(43);
//		 System.out.println(a.get(44).name);
//		List<Hero> he = new ArrayList<>(); 
//		he = a.list(35,45);
//		System.out.println(he);
		
		
	}
	public int getTotal(){
//		查询数据库数据量
		try {
			Class.forName("com.mysql.jdbc.Driver");
		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		String sql = "select count(*) from hero";
		int total = 0;
		try (Connection c = DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/how2java?characterEncoding=UTF8","root","admin");
			 PreparedStatement ps = c.prepareStatement(sql)	){
			 
			 ResultSet rs = ps.executeQuery();
			 if(rs.next()){
				 total = rs.getInt(1);
			 }
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return total;
	}
	public void add(Hero hero){
		String name = hero.name;
		float hp = hero.hp;
		int damage = hero.damage;
		try {
			Class.forName("com.mysql.jdbc.Driver");
		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		String sql = "insert into hero values(null,?,?,?)";
		try (Connection c = DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/how2java?characterEncoding=UTF8","root","admin");
			 PreparedStatement ps = c.prepareStatement(sql)	){
			 ps.setString(1, name);
			 ps.setFloat(2, hp);
			 ps.setInt(3, damage);
			 ps.execute();
		
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	public void update(Hero h,int id){
		
		try {
			Class.forName("com.mysql.jdbc.Driver");
		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		String sql = "update hero set name=? , hp=? , damage=? where id=?";
		try (Connection c = DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/how2java?characterEncoding=UTF8","root","admin");
			 PreparedStatement ps = c.prepareStatement(sql)	){
			    ps.setString(1, h.name);
	            ps.setFloat(2, h.hp);
	            ps.setInt(3, h.damage);
	            ps.setInt(4, id);
	            ps.execute();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	public void delete(int id){
		try {
			Class.forName("com.mysql.jdbc.Driver");
		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		String sql = "delete from hero where id=?";
		try (Connection c = DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/how2java?characterEncoding=UTF8","root","admin");
			 PreparedStatement ps = c.prepareStatement(sql)	){
			
			 ps.setInt(1, id);
			 ps.execute();
		
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	public Hero get(int id){
		Hero hero = null;
		try {
			Class.forName("com.mysql.jdbc.Driver");
		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		String sql = "select * from hero where id=?";
		try (Connection c = DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/how2java?characterEncoding=UTF8","root","admin");
			 PreparedStatement ps = c.prepareStatement(sql)	){
			
			 ps.setInt(1, id);
			 ResultSet rs = ps.executeQuery();
			 if(rs.next()){
				 hero = new Hero();
				 hero.name = rs.getString("name");
				 hero.hp = rs.getFloat("hp");
				 hero.damage = rs.getInt("damage");
			 }
		
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return hero;
	}
	public List<Hero> list(int start,int end){
		List<Hero> heros = new ArrayList<>();
		try {
			Class.forName("com.mysql.jdbc.Driver");
		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		String sql = "select * from hero order by id desc limit ?,?";
		try (Connection c = DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/how2java?characterEncoding=UTF8","root","admin");
			 PreparedStatement ps = c.prepareStatement(sql)	){
			ps.setInt(1, start);
			ps.setInt(2, end);
			ResultSet rs = ps.executeQuery();
			while(rs.next()){
				Hero hero = new Hero();
				hero.name = rs.getString("name");
				hero.hp = rs.getFloat("hp");
				hero.damage = rs.getInt("damage");
				heros.add(hero);
			}
			
		
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return heros;
	}
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值