日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 运维知识 > 数据库 >内容正文

数据库

【java】用javaSE来实现对mysql数据库的增删改查

發(fā)布時間:2023/12/19 数据库 38 豆豆
生活随笔 收集整理的這篇文章主要介紹了 【java】用javaSE来实现对mysql数据库的增删改查 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

主程序:

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;
?? ?}

}

實現(xiàn)層:

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;
?? ??? ?}
?? ??? ?
?? ?}
}

和數(shù)據(jù)庫連接層:

?

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) {
?? ??? ?// 驅(qū)動程序名
?? ??? ?String driver = "com.mysql.jdbc.Driver";
?? ??? ?// URL指向所訪問的數(shù)據(jù)庫的驅(qū)動名
?? ??? ?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;
?? ?}

}

它們之間的關(guān)系是:

BD連接數(shù)據(jù)庫,DAO層是數(shù)據(jù)庫的元素映射,實現(xiàn)層調(diào)用DAO層實現(xiàn)數(shù)據(jù)的增刪改查,主函數(shù)調(diào)用實現(xiàn)層進行具體操作。

?

新手純手打,還請各位多多指教!!

轉(zhuǎn)載于:https://www.cnblogs.com/zhizhuniuniu/p/4211291.html

總結(jié)

以上是生活随笔為你收集整理的【java】用javaSE来实现对mysql数据库的增删改查的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。