【java基础】JDBC实现增删改查


梳理知识:JDBC的使用


准备工作:

  1. jdbc连接的jar包,可到mysql官网下载或者百度
  2. 安装好mysql数据库,并创建数据库和表
  3. java编写操作数据库的代码

代码实现增删改查如下:

package jdbc;
/**
 * Java 在jdbc中的增删改查操作
 * 1.PrepareStatement 是Statement的子接口,拥有其所有功能
 * 2.PrepareStatement在大部分情况下性能更优
 * 3.PrepareStatement能阻断大部分的sql注入攻击
 * 综上所述,优先使用PrepareStatement
 * @author Solitude
 *
 */


import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

public class MyJDBC {
	private static final String url = "jdbc:mysql://localhost:3306/zsdx";
	private static final String username = "root";
	private static final String password = "root";
	
	static{
		try {
			Class.forName("com.mysql.jdbc.Driver");
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		}
	}

	// main方法,主函数入口
	public static void main(String[] args) {
		System.out.println("start");
		MyJDBC.addUser();
		MyJDBC.deleteUser(4);
		User user = MyJDBC.selectUser(5);
		System.out.println(user.getName() +" " + user.getPassword());
		MyJDBC.alterUser(6);
		System.out.println("stop");

	}
	
	//取得数据库链接
	public static Connection getConn(){
		Connection conn = null;
		try {
			conn = DriverManager.getConnection(url, username, password);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return conn;
	}
	
	//增加一个user
	public static void addUser(){
		String sql = "insert into user(name,password) values(?,?)";
		Connection conn = MyJDBC.getConn();
		try {
			PreparedStatement ptst = conn.prepareStatement(sql);
			ptst.setString(1, "hongda");
			ptst.setString(2, "lilong123");
			ptst.executeUpdate();
			ptst.close();
		} catch (SQLException e) {	
			e.printStackTrace();
		}finally {
			try {
				conn.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
	}
	
	// delete a user
	public static void deleteUser(int id){
		String sql = "delete from user where id = ?";
		Connection conn = MyJDBC.getConn();
		try {
			PreparedStatement ptst = conn.prepareStatement(sql);
			ptst.setInt(1, id);
			ptst.executeUpdate();
			ptst.close();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally {
			try {
				conn.close();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}

	//Alter a user
	public static void alterUser(int id){
		String sql = "update user set name = ? where id = ?";
		try {
			PreparedStatement ptst = MyJDBC.getConn().prepareStatement(sql);
			ptst.setString(1, "李龙");
			ptst.setInt(2, id);
			ptst.executeUpdate();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	public static User selectUser(int id) {
        String sql = "select * from user where id = ?";
        User user = new User();
        try {
            PreparedStatement ptst = MyJDBC.getConn().prepareStatement(sql);
            ptst.setInt(1, id);
            ResultSet rs = ptst.executeQuery();
            if(rs!= null){
                rs.next(); //把指针指向第一条数据
                user.setName(rs.getString("name"));
                user.setPassword(rs.getString("password"));
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return user;
    }
}

class User{
	private int id;
	private String name;
	private String password;
	
	public User() {
		
	}

	public User(String name, String password) {
		super();
		this.name = name;
		this.password = password;
	}

	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 getPassword() {
		return password;
	}

	public void setPassword(String password) {
		this.password = password;
	}
	
	
}





你可能感兴趣的:(java基础)