首页 > 代码库 > 【java】用javaSE来实现对mysql数据库的增删改查
【java】用javaSE来实现对mysql数据库的增删改查
主程序:
import Bean.StudentBean;
import Impl.StudentImpl;
public class T7 {
public static void main(String[] args) {
StudentImpl stdimp = new StudentImpl();
// StudentBean student = new StudentBean("halala", "woman", "china");
// stdimp.add(student);
stdimp.select("libin");
// stdimp.delete("halala");
// stdimp.add("halazi", "woman", "computer");
// stdimp.update("halazi", "libin");
}
}
DAO层:
package Bean;
public class StudentBean {
private int number;
private String name;
private String sex;
private String major;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getNumber() {
return number;
}
public void setNumber(int number) {
this.number = number;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
public String getMajor() {
return major;
}
public void setMajor(String major) {
this.major = major;
}
@Override
public String toString() {
return "StudentBean [number=" + number + ", name=" + name + ", sex="
+ sex + ", major=" + major + "]";
}
public StudentBean() {
super();
// TODO Auto-generated constructor stub
}
public StudentBean(String name, String sex, String major) {
super();
this.name = name;
this.sex = sex;
this.major = major;
}
}
实现层:
package Impl;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import Bean.StudentBean;
import Conn.BD;
public class StudentImpl {
BD bd = new BD();
public int add(StudentBean std) {
int row = 0;
Statement stmt = null;
Connection conn = null;
// String sql = "insert into info (name,sex,major)values("
// + "‘std.getName()‘" +","+ "‘std.getSex()‘" +","+ "‘std.getMajor()‘"
// + ");";
String sql = "insert into info (name,sex,major)values( ‘"
+ std.getName() + "‘,‘" + std.getSex() + "‘,‘" + std.getMajor()
+ "‘);";
System.out.println(sql);
try {
stmt = bd.conn(conn).createStatement();
row = stmt.executeUpdate(sql);
stmt.close();
return row;
} catch (SQLException e) {
e.printStackTrace();
try {
stmt.close();
conn.close();
} catch (SQLException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
return -1;
}
}
public int add(String name, String sex, String major) {
int row = 0;
Connection conn = null;
Statement stmt = null;
StudentBean std = new StudentBean();
std.setName(name);
std.setSex(sex);
std.setMajor(major);
// String sql = "insert into info (name,sex,major)values("
// + "‘std.getName()‘" +","+ "‘std.getSex()‘" +","+ "‘std.getMajor()‘"
// + ");";
String sql = "insert into info (name,sex,major)values( ‘"
+ std.getName() + "‘,‘" + std.getSex() + "‘,‘" + std.getMajor()
+ "‘);";
System.out.println(sql);
try {
stmt = bd.conn(conn).createStatement();
row = stmt.executeUpdate(sql);
stmt.close();
return row;
} catch (SQLException e) {
e.printStackTrace();
try {
stmt.close();
conn.close();
} catch (SQLException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
return -1;
}
}
public int delete(String name) {
StudentBean std = new StudentBean();
Statement stmt = null;
Connection conn = null;
int dem = 0;
std.setName(name);
String sql = "delete from info where name=‘" + std.getName() + "‘;";
System.out.println(sql);
try {
stmt = bd.conn(conn).createStatement();
dem = stmt.executeUpdate(sql);
System.out.println("删除成功!");
return dem;
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
return -1;
}
}
public void select(String name) {
StudentBean std = new StudentBean();
Statement stmt = null;
Connection conn = null;
ResultSet rs = null;
std.setName(name);
String sql = "select * from info where name=‘" + std.getName() + "‘;";
System.out.println(sql);
try {
stmt = bd.conn(conn).createStatement();
rs = stmt.executeQuery(sql);
System.out.println("查询成功!");
while (rs.next()) {
System.out.println();
System.out.println(+rs.getInt("number") + "\t"
+ rs.getString("name") + "\t" + rs.getString("sex")
+ "\t" + rs.getString("major") + "\t");
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
System.out.println("查询失败!");
}
}
//
public int update(String name,String upinfo) {
Statement stmt = null;
Connection conn = null;
StudentBean std=new StudentBean();
std.setName(name);
int upd = 0;
String sql = "update info set name= ‘"+upinfo+"‘ where name=‘"+std.getName()+"‘ ;";
System.out.println(sql);
try {
stmt = bd.conn(conn).createStatement();
upd = stmt.executeUpdate(sql);
stmt.close();
return upd;
} catch (SQLException e) {
e.printStackTrace();
return -1;
}
}
}
和数据库连接层:
package Conn;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;
public class BD {
public Connection conn(Connection conn) {
// 驱动程序名
String driver = "com.mysql.jdbc.Driver";
// URL指向所访问的数据库的驱动名
String url = "jdbc:mysql://127.0.0.1:3306/student";
// 配置mysql用的用户名
String username = "root";
// java连接mysql用的密码
String password = "root123";
try {
Class.forName(driver);
conn = DriverManager.getConnection(url, username, password);
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return conn;
}
}
它们之间的关系是:
BD连接数据库,DAO层是数据库的元素映射,实现层调用DAO层实现数据的增删改查,主函数调用实现层进行具体操作。
新手纯手打,还请各位多多指教!!
【java】用javaSE来实现对mysql数据库的增删改查