Java语言实现对MySql数据库中数据的增删改查操作的代码
简单说操作的步骤:
1.连接数据库
2.将SQL语句发送到数据库
3.执行SQL语句
这里举个例子:
在一个数据库中有个students表,表中有学号(Id),姓名(Name),性别(Sex),地址(Address),电话(Phone),专业(Dept)。
这里把这个表写成一个学生信息类(Info_student)
(请先确保看了例子说明,不然代码有的地方可能看不明白)
要实现操纵我们首先得连接数据库,因为每个操作都要进行连接操作,所以我们直接把连接的操作封装在一个类中,需要连接的时候直接调用可。
数据库连接类:
importjava.sql.Connection;
importjava.sql.DriverManager;
publicclassDB_Helper{
publicstaticConnectionconnect=null;
static{
try{
Class.forName("com.mysql.jdbc.Driver");//加载MYSQLJDBC驱动程序
//观察以下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("SuccessloadingMysqlDriver!");
}catch(Exceptione){
System.out.print("ErrorloadingMysqlDriver!");
e.printStackTrace();
}
}
publicstaticConnectiongetConnection(){
returnconnect;
}
}
数据库已经连接了,那么接下来就是要发送SQL语句和执行语句。
发送语句用到了PreparedStatement对象和Connection对象的操作prepareStatement()
执行语句用到PreparedStatement对象的操作execute()
提示:以下是一些对象的说明,可以先看代码,遇到的时候再回来看。
************************
PreparedStatement
表示预编译的SQL语句的对象。
SQL语句被预编译并存储在PreparedStatement对象中。然后可以使用此对象多次高效地执行该语句。
*************************
Connection
与特定数据库的连接(会话)。在连接上下文中执行SQL语句并返回结果。
Connection对象的数据库能够提供描述其表、所支持的SQL语法、存储过程、此连接功能等等的信息。
**********************
以下代码是要实现在数据库中实现学生信息的增删改查操作。
一、增
publicvoidadd(Info_studentstudent)throwsSQLException{
//与特定数据库的连接(会话)。
Connectionconn=(Connection)DB_Helper.getConnection();
Stringsql="insertintostudent(Sno,Sname,Ssex,Saddress,Sphone,Sdept)values(?,?,?,?,?,?)";
//创建一个PreparedStatement对象来将参数化的SQL语句发送到数据库。
PreparedStatementptmt=(PreparedStatement)conn.prepareStatement(sql);
/*
*voidsetBigDecimal(intparameterIndex,BigDecimalx)throwsSQLException
*将指定参数设置为给定JavaString值。在将此值发送给数据库时,驱动程序将它转换成一个SQLVARCHAR
*或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();
}
二、删
publicvoiddelete(Stringid)throwsSQLException{
Connectionconn=(Connection)DB_Helper.getConnection();
Stringsql="deletefromstudentwhereSno=?";
PreparedStatementptmt=(PreparedStatement)conn.prepareStatement(sql);
ptmt.setString(1,id);
ptmt.execute();
}
三、改
publicvoidupdate(Info_studentstudent)throwsSQLException{
Connectionconn=(Connection)DB_Helper.getConnection();
Stringsql="updatestudentsetSname=?,Ssex=?,Saddress=?,Sphone=?,Sdept=?whereSno=?";
PreparedStatementptmt=(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();
}
四、查
publicInfo_studentsearch(Stringid)throwsSQLException{
Info_studentstudent=null;
Connectionconn=(Connection)DB_Helper.getConnection();
Stringsql="select*fromstudentwhereSno=?";
PreparedStatementptmt=(PreparedStatement)conn.prepareStatement(sql);
ptmt.setString(1,id);
/*
*ResultSetexecuteQuery()throwsSQLException
*在此PreparedStatement对象中执行SQL查询,并返回该查询生成的ResultSet对象。
*/
/*
*publicinterfaceResultSetextendsWrapper
*表示数据库结果集的数据表,通常通过执行查询数据库的语句生成。ResultSet对象具有指向其当前数据行的光标。
*最初,光标被置于第一行之前。next方法将光标移动到下一行;因为该方法在ResultSet对象没有下一行时
*返回false,所以可以在while循环中使用它来迭代结果集。
*
*/
ResultSetrs=ptmt.executeQuery();
/*
*booleannext()throwsSQLException
*将光标从当前位置向前移一行。
*ResultSet光标最初位于第一行之前;
*第一次调用next方法使第一行成为当前行;
*第二次调用使第二行成为当前行,依此类推。
*/
while(rs.next()){
student=newInfo_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"));
}
returnstudent;
}
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持毛票票。