【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層實現數據的增刪改查,主函數調用實現層進行具體操作。
?
新手純手打,還請各位多多指教!!
轉載于:https://www.cnblogs.com/zhizhuniuniu/p/4211291.html
總結
以上是生活随笔為你收集整理的【java】用javaSE来实现对mysql数据库的增删改查的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 手机百度地图软件的小度智能语音如何使用
- 下一篇: 快手口外是什么意思