博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
java se加mysql_【java】用javaSE来实现对mysql数据库的增删改查
阅读量:4964 次
发布时间:2019-06-12

本文共 5283 字,大约阅读时间需要 17 分钟。

标签:

主程序:

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层实现数据的增删改查,主函数调用实现层进行具体操作。

新手纯手打,还请各位多多指教!!

标签:

转载地址:http://ahqhp.baihongyu.com/

你可能感兴趣的文章
flask简单的注册功能
查看>>
JSP常用标签
查看>>
dashucoding记录2019.6.7
查看>>
IOS FMDB
查看>>
编码总结,以及对BOM的理解
查看>>
九涯的第一次
查看>>
PHP5.3的VC9、VC6、Thread Safe、Non Thread Safe的区别
查看>>
Android中全屏或者取消标题栏
查看>>
处理器管理与进程调度
查看>>
页面懒加载
查看>>
向量非零元素个数_向量范数详解+代码实现
查看>>
java zip 中文文件名乱码_java使用zip压缩中文文件名乱码的解决办法
查看>>
java if 用法详解_Java编程中的条件判断之if语句的用法详解
查看>>
kafka的java客户端_KAFKA Producer java客户端示例
查看>>
java -f_java学习笔记(一)
查看>>
java 什么题目好做_用java做这些题目
查看>>
java中的合同打印_比较方法违反了Java 7中的一般合同
查看>>
php 位运算与权限,怎么在PHP中使用位运算对网站的权限进行管理
查看>>
php include效率,php include类文件超时
查看>>
matlab sin函数 fft,matlab的fft函数的使用教程
查看>>