jdbc_mysql增删改查用户简单操作!分包进行!

package connectionTool;

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

public class connectionTool {

	public static Connection connection() {
		Connection c = null;
		try {
			c = DriverManager.getConnection("jdbc:mysql:///person", "root", "");
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return c;
	}

	static {
		try {
			Class.forName("com.mysql.jdbc.Driver");
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		}
	}

}
package test;

import user.user;
import userDaoImpl.userDaoImpl;

public class Tmain {
	public static void main(String[] args) {
		user u = new user();
		u.setId(3);
		u.setName("luxi");
		u.setSalary(6934);
		userDaoImpl udi = new userDaoImpl();
		udi.insert(u); // 新增
		// udi.select(1); //查询
		// udi.update(1, "jack", 300); //修改信息
		// udi.drop(1); //删除

	}
}
package user;

//   mysql:数据库person.person表
//		create table person (
//		 id int(90) primary key auto_increment  comment '序号' ,
//		 name varchar(90) comment '姓名',
//		 salary int(90) comment '工资'
//	  )charset gbk;

/*
 * 定义一个用户类
 * 
 */

public class user {
	private int id;
	private String name;
	private int salary;

	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 int getSalary() {
		return salary;
	}

	public void setSalary(int salary) {
		this.salary = salary;
	}

}
package userDao;

import user.user;

/*
 * 对user的增删改查
 * 
 */
public interface userDao {

	/*
	 * 新增user
	 */
	public void insert(user u);

	/*
	 * 修改信息
	 */
	public void update(int id, String name, int salary);

	/*
	 * 查询用户
	 */
	public void select(int id);

	/*
	 * 删除用户
	 */
	public void drop(int id);

}
package userDaoImpl;

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

import connectionTool.connectionTool;
import user.user;
import userDao.userDao;

public class userDaoImpl implements userDao {
	Connection c = null;
	PreparedStatement p = null;
	int i = 0;

	public userDaoImpl() {
		c = connectionTool.connection();
	}

	@Override
	public void insert(user u) {

		String sql = "insert into person values(?,?,?)";
		try {
			p = c.prepareStatement(sql);
		} catch (SQLException e1) {
			e1.printStackTrace();
		}
		try {
			p.setInt(1, u.getId());
			p.setString(2, u.getName());
			p.setInt(3, u.getSalary());
			i = p.executeUpdate();
		} catch (SQLException e) {
			e.printStackTrace();
		}

		if (i == 1)
			System.out.println("新增用户成功!");
		else
			System.out.println("新增用户失败!");
	}

	@Override
	public void update(int id, String name, int salary) {
		String sql = "update person set name=?,salary=? where id=?";
		try {
			p = c.prepareStatement(sql);
			p.setString(1, name);
			p.setInt(2, salary);
			p.setInt(3, id);
			int i = p.executeUpdate();
			if (i == 1)
				System.out.println("更新用户信息成功!");
			else
				System.out.println("更新用户信息失败!");

		} catch (SQLException e) {
			e.printStackTrace();
		}

	}

	@Override
	public void select(int id) {
		String sql = "select name,salary from person where id=?";

		ResultSet rs = null;
		try {
			p = c.prepareStatement(sql);
			p.setInt(1, id);
		} catch (SQLException e) {
			e.printStackTrace();
		}
		try {
			rs = p.executeQuery();
			while (rs.next()) {
				System.out.println("姓名是: " + rs.getString("name")
 + "----工资是: " + rs.getInt("salary"));
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}

	}

	@Override
	public void drop(int id) {

		String sql = "delete from person where id=?";
		try {
			p = c.prepareStatement(sql);
			p.setInt(1, id);
			int i = p.executeUpdate();
			if (i == 1)
				System.out.println("删除用户成功!");
			else
				System.out.println("删除用户失败!");

		} catch (SQLException e) {
			e.printStackTrace();
		}

	}

}

 

转载于:https://my.oschina.net/u/3384770/blog/902285

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值