jdbc的CURD——增删改查
以一个关于学生的表格举例。
准备工作!
1.工具类
先建立一个工具类,起名DBUtil,留着方便使用
“取连接”关连接“”。
package jdbc;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
public class DBUtil {
private DBUtil() {
}
private static String str = "jdbc:mysql://localhost:3306/test";
private static String name = "root";
private static String pword = "root";
private static String classname = "com.mysql.jdbc.Driver";
static Connection conn = null;
static {
try {
Class.forName(classname);
} catch (Exception e) {
e.printStackTrace();
}
}
// 取连接
public static Connection getConnection() {
try {
conn = DriverManager.getConnection(str, name, pword);
} catch (SQLException e) {
e.printStackTrace();
}
return conn;
}
// 关连接
public static void sTop(Connection conn, Statement stm, ResultSet rss) {
if (rss != null) {
try {
rss.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
if (stm != null) {
try {
rss.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
if (conn != null) {
try {
rss.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
}
2.对象属性的类(userInfo)
存放各种属性。比如学号,姓名,专业,成绩等等。添加get-set方法。
package com.bean;
public class userInfo {
private String userName;
private float score;
private String schoolName;
private int id;
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
public float getScore() {
return score;
}
public void setScore(float score) {
this.score = score;
}
public String getSchoolName() {
return schoolName;
}
public void setSchoolName(String schoolName) {
this.schoolName = schoolName;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
}
具体操作!
1.增加
返回值设定成result
.
注意executeUpdate的使用
方法如下
public static int addUser(userInfo user) {
int result = 0;
Connection conn = null;
Statement stm = null;
ResultSet rss = null;
try {
conn = DBUtil.getConnection();
stm = conn.createStatement();
String sql = "inser into userInfo(id,userName,password,school)values('" + user.getUserName() + "','"
+ user.getScore() + "','" + user.getSchoolName() + "')";
/* int count = stm.executeUpdate(sql);
*执行添加的操作。count 代表对数据库影响的行数。
*/
result = stm.executeUpdate(sql);
} catch (Exception e) {
e.printStackTrace();
} finally {
DBUtil.sTop(conn, stm, rss);
}
return result;
}
具体实现的操作: userInfo user = new userInfo(); user.setSchoolName("linda");
user.setScore(99); user.setUserName("打蜡级"); int result =
addUser(user); if (result == 1) { System.out.println("添加成功"); } else
{ System.out.println("添加失败"); }
userInfo user = new userInfo(); user.setSchoolName("linda");
user.setScore(99); user.setUserName("打蜡级"); int result =
addUser(user); if (result == 1) { System.out.println("添加成功"); } else
{ System.out.println("添加失败"); }
需要注意的问题:
1、在实际操作要注意id问题:
不要主动在表单里添加id(如果用id作为主键使用),以免造成不必要的id冲突。
2、ezecute与executeUpdate两个方法的区别?
execute()没有返回值,“干完活就是干完活了,不说做了多少。”
executeUpdate()具有返回值,“干完活了会把事情说出来,说出做了多少。”
2.删除
相比之下比较容易。
所谓删除,就是根据一个属性进行查找,将所有相关的信息进行删除。
public static int delUser(int userId) {
int result = 0;
try {
conn = DBUtil.getConnection();
stm = conn.createStatement();
result = stm.executeUpdate("delete from userInfo where id=" + userId);
} catch (SQLException e) {
e.printStackTrace();
} finally {
DBUtil.sTop(conn, stm, null);
}
return result;
}
具体实现的语句:
int result=delUser(90); if(result>0){ System.out.println("成功");
}else{ System.out.println("失败"); }
int result=delUser(90); if(result>0){ System.out.println("成功");
}else{ System.out.println("失败"); }
3.修改
通常是根据主键进行更新;
个人学习经验。
public static int upDate(userInfo user) {
int result = 0;
try {
conn = DBUtil.getConnection();
stm = conn.createStatement();
String sql = "update userInfo set userName='" + user.getUserName() + "',password='" + user.getScore()
+ "',school='" + user.getSchoolName() + "'";
result = stm.executeUpdate(sql);
} catch (SQLException e) {
e.printStackTrace();
} finally {
DBUtil.sTop(conn, stm, null);
}
return result;
}
具体的实现语句:
userInfo user1=new userInfo(); user1.setId(91);
user1.setSchoolName("456"); user1.setScore(99); int
result=upDate(user1); if(result>0){ System.out.println("成功"); }else{
System.out.println("失败"); }
userInfo user1=new userInfo(); user1.setId(91);
user1.setSchoolName("456"); user1.setScore(99); int
result=upDate(user1); if(result>0){ System.out.println("成功"); }else{
System.out.println("失败"); }
4.查询
千万不要在if判断语句中进行new userInfo
public static userInfo getuserById(int id) {
userInfo user = null;
conn = null;
stm = null;
rss = null;
try {
conn = DBUtil.getConnection();
stm = conn.createStatement();
String sql = "update userInfo set userName='" + user.getUserName() + "',password='" + user.getScore()
+ "',school='" + user.getSchoolName() + "'";
rss = stm.executeQuery(sql);
if (rss.next()) {
user = new userInfo();
user.setId(rss.getInt("id"));
user.setSchoolName(rss.getString("schoolName"));
}
} catch (SQLException e) {
e.printStackTrace();
} finally {
DBUtil.sTop(conn, stm, null);
}
return user;
}
具体的实现语句:
userInfo user = getuserById(91);
System.out.println(user);
个人学习经验。