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();
}
}
}