Java语言实现对MySql数据库中数据的增删改查操作的代码

2025-05-29 0 107

简单说操作的步骤:

1.连接数据库

2.将SQL语句发送到数据库

3.执行SQL语句

这里举个例子:

在一个数据库中有个students表,表中有学号(Id),姓名(Name),性别(Sex),地址(Address),电话(Phone),专业(Dept)。

这里把这个表写成一个学生信息类(Info_student)

(请先确保看了例子说明,不然代码有的地方可能看不明白)

要实现操纵我们首先得连接数据库,因为每个操作都要进行连接操作,所以我们直接把连接的操作封装在一个类中,需要连接的时候直接调用可。

数据库连接类:

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27
import java.sql.Connection;

import java.sql.DriverManager;

public class DB_Helper {

public static Connection connect = null;

static {

try {

Class.forName("com.mysql.jdbc.Driver"); // 加载MYSQL JDBC驱动程序

// 观察以下2个语句的差别,

// connect =

// DriverManager.getConnection("jdbc:mysql://localhost:3306/students", "root", "");

connect = DriverManager.getConnection(

"jdbc:mysql://localhost:3306/students?useUnicode=true&characterEncoding=utf-8", "root", "");

System.out.println("Success loading Mysql Driver!");

} catch (Exception e) {

System.out.print("Error loading Mysql Driver!");

e.printStackTrace();

}

}

public static Connection getConnection() {

return connect;

}

}

数据库已经连接了,那么接下来就是要发送SQL语句和执行语句。

发送语句用到了PreparedStatement对象和Connection对象的操作prepareStatement()

执行语句用到PreparedStatement对象的操作execute()

提示:以下是一些对象的说明,可以先看代码,遇到的时候再回来看。

************************

PreparedStatement

表示预编译的 SQL 语句的对象。

SQL 语句被预编译并存储在 PreparedStatement 对象中。然后可以使用此对象多次高效地执行该语句。

*************************

Connection

与特定数据库的连接(会话)。在连接上下文中执行 SQL 语句并返回结果。

Connection 对象的数据库能够提供描述其表、所支持的 SQL 语法、存储过程、此连接功能等等的信息。

**********************

以下代码是要实现在数据库中实现学生信息的增删改查操作。

一、增

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23
public void add(Info_student student) throws SQLException{

// 与特定数据库的连接(会话)。

Connection conn = (Connection) DB_Helper.getConnection();

String sql = "insert into student(Sno,Sname,Ssex,Saddress,Sphone,Sdept) values(?,?,?,?,?,?)";

// 创建一个 PreparedStatement 对象来将参数化的 SQL 语句发送到数据库。

PreparedStatement ptmt = (PreparedStatement) conn.prepareStatement(sql);

/*

* void setBigDecimal(int parameterIndex,BigDecimal x)throws SQLException

* 将指定参数设置为给定 Java String 值。在将此值发送给数据库时,驱动程序将它转换成一个 SQL VARCHAR

* 或 LONGVARCHAR 值(取决于该参数相对于驱动程序在 VARCHAR 值上的限制的大小)。

*/

ptmt.setString(1, student.getId());

ptmt.setString(2, student.getName());

ptmt.setString(3, student.getSex());

ptmt.setString(4, student.getAddress());

ptmt.setString(5, student.getPhone());

ptmt.setString(6, student.getDept());

// 在此 PreparedStatement 对象中执行 SQL 语句

ptmt.execute();

}

二、删

?

1

2

3

4

5

6

7

8

9
public void delete(String id) throws SQLException{

Connection conn = (Connection) DB_Helper.getConnection();

String sql = "delete from student where Sno=?";

PreparedStatement ptmt = (PreparedStatement) conn.prepareStatement(sql);

ptmt.setString(1, id);

ptmt.execute();

}

三、改

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14
public void update(Info_student student) throws SQLException{

Connection conn = (Connection) DB_Helper.getConnection();

String sql = "update student set Sname=?,Ssex=?,Saddress=?,Sphone=?,Sdept=? where Sno=?";

PreparedStatement ptmt = (PreparedStatement) conn.prepareStatement(sql);

ptmt.setString(1, student.getName());

ptmt.setString(2, student.getSex());

ptmt.setString(3, student.getAddress());

ptmt.setString(4, student.getPhone());

ptmt.setString(5, student.getDept());

ptmt.setString(6, student.getId());

ptmt.execute();

}

四、查

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44
public Info_student search(String id) throws SQLException{

Info_student student = null;

Connection conn = (Connection) DB_Helper.getConnection();

String sql = "select * from student where Sno=?";

PreparedStatement ptmt = (PreparedStatement) conn.prepareStatement(sql);

ptmt.setString(1, id);

/*

* ResultSet executeQuery()throws SQLException

* 在此 PreparedStatement 对象中执行 SQL 查询,并返回该查询生成的 ResultSet 对象。

*/

/*

* public interface ResultSet extends Wrapper

* 表示数据库结果集的数据表,通常通过执行查询数据库的语句生成。 ResultSet 对象具有指向其当前数据行的光标。

* 最初,光标被置于第一行之前。next 方法将光标移动到下一行;因为该方法在 ResultSet 对象没有下一行时

* 返回 false,所以可以在 while 循环中使用它来迭代结果集。

*

*/

ResultSet rs = ptmt.executeQuery();

/*

* boolean next()throws SQLException

* 将光标从当前位置向前移一行。

* ResultSet 光标最初位于第一行之前;

* 第一次调用 next 方法使第一行成为当前行;

* 第二次调用使第二行成为当前行,依此类推。

*/

while(rs.next()){

student = new Info_student();

student.setId(rs.getString("Sno"));

student.setName(rs.getString("Sname"));

student.setSex(rs.getString("Ssex"));

student.setAddress(rs.getString("Saddress"));

student.setPhone(rs.getString("Sphone"));

student.setDept(rs.getString("Sdept"));

}

return student;

}

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持快网idc。

原文链接:http://blog.csdn.net/qq_34594236/article/details/53894905

收藏 (0) 打赏

感谢您的支持,我会继续努力的!

打开微信/支付宝扫一扫,即可进行扫码打赏哦,分享从这里开始,精彩与您同在
点赞 (0)

声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。

快网idc优惠网 建站教程 Java语言实现对MySql数据库中数据的增删改查操作的代码 https://www.kuaiidc.com/119603.html

相关文章

发表评论
暂无评论