好难过..........................
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
public class DBUtil {
private static final String url ="jdbc:mysql://localhost:3306/test1";
private static final String user= "root";
private static final String password = "root";
private static final String driver= "com.mysql.jdbc.Driver";
static {
try {
Class.forName(driver);
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
}
public static Connection getConnection(){
try {
return DriverManager.getConnection(url,user,password);
} catch (SQLException e) {
e.printStackTrace();
}
return null;
}
public static void close(Connection conn){
if (conn!=null){
try {
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
}
package cn.itcast.service;
import cn.itcast.do_main.emp;
import cn.itcast.util.DBUtil;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
public class empservice {
private final String ADD= "inster into emp(id,ename)values (null,?)";
private final String EDIT= "update emp set ename = ? where id =?";
private final String DELETE= "delete from emp where id =?";
private final String LEST= "select * from emp";
public void add (emp entity){
Connection coon = DBUtil.getConnection();
try {
PreparedStatement patmt = coon.prepareStatement(ADD);
patmt.setString(1,entity.getName());
patmt.executeUpdate();
} catch (SQLException e) {
e.printStackTrace();
} finally {
DBUtil.close(coon);
}
}
}
private int id;
private String name;
private String gender;
private double salary;
private Date join_date;
private int dept_id;
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 getGender() {
return gender;
}
public void setGender(String gender) {
this.gender = gender;
}
public double getSalary() {
return salary;
}
public void setSalary(double salary) {
this.salary = salary;
}
public Date getJoin_date() {
return join_date;
}
public void setJoin_date(Date join_date) {
this.join_date = join_date;
}
public int getDept_id() {
return dept_id;
}
public void setDept_id(int dept_id) {
this.dept_id = dept_id;
}
public emp() {
}
public emp(int id, String name) {
this.id = id;
this.name = name;
}
public emp(int id, String name, String gender, double salary, Date join_date, int dept_id) {
this.id = id;
this.name = name;
this.gender = gender;
this.salary = salary;
this.join_date = join_date;
this.dept_id = dept_id;
}
}这不是我想要的那种结果....++
|
|