中软国际实习day2
作者:互联网
JDBC(Java Data Base Connectivity)
JDBC实际上起到的是一个桥梁的作用,它使得原本没有关系的Java和Mysql连接起来。
在IDEA创建Java Web Application项目后需要将包导入项目中
然后使用JDBC进行数据库的连接。
(需要注意的是,无论用什么语言去开发,做事之前都需要有数据库的支持)
下面展示的是JDBC数据库连接并对表单进行遍历
public class TestUser {
// 测试增加
public static void main(String[] args) {
Connection conn = null;
PreparedStatement pstm = null;
ResultSet rs = null;
try {
// 1. 获取驱动 ,万能键 Alt + enter
// 自动补全返回值: Ctrl + Alt + V
Class.forName("com.mysql.jdbc.Driver");
// 2. 创建连接
conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/java7?useSSL=true&characterEncoding=utf-8" +
"&user=root&password=123");
System.out.println("数据库连接成功" + conn);
// 3. 编写sql, 注释的快捷键:Ctrl + /
// String sql = "insert into tb_user(username,password) values(?,?)";
String sql = "select * from tb_user";
// 4. 获取存放sql的对象
pstm = conn.prepareStatement(sql);
// 5. 去执行SQL语句 并得到结果
// executeUpdate: 给我们返回的是int类型的值(增删改), executeQuery: 给我们返回的是结果集(查询);
// int i = pstm.executeUpdate();
rs = pstm.executeQuery();
// 6. 遍历结果
while (rs.next()){
System.out.println("用户ID:"+rs.getInt(1));
System.out.println("用户名:"+rs.getString(2));
System.out.println("用户密码:"+rs.getString(3));
}
} catch (Exception e) {
} finally {
if(rs !=null){
try {
rs.close();
} catch (SQLException e) {
e.printStackTrace();
}
} else if(pstm !=null){
try {
pstm.close();
} catch (SQLException ex) {
ex.printStackTrace();
}
}else if(conn != null){
try {
conn.close();
} catch (SQLException ex) {
ex.printStackTrace();
}
}
}
}
通过上例,会发现建立连接的代码都在方法内,这样的话相当于每进行一个类方法的操作时,都需要重新写这段连接的的代码。
Class.forName("com.mysql.jdbc.Driver");
conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/java7?useSSL=true&characterEncoding=utf-8" + "&user=root&password=123456");
因此,我们就引出了封装的概念
public class DBUtil {
private static String driver = "com.mysql.jdbc.Driver";
private static String url = "jdbc:mysql://localhost:3306/java7?useSSL=true&characterEncoding=utf-8";
private static String user = "root";
private static String password = "123";
//获取驱动
static{
try {
Class.forName(driver);
} catch (Exception e) {
e.printStackTrace();
}
}
//创建连接
public static Connection get_Conn() throws SQLException {
Connection conn = DriverManager.getConnection(url, user, password);
System.out.println("数据库连接成功"+conn);
return conn;
}
//3.关闭连接
public static void get_close(ResultSet rs , PreparedStatement pstm , Connection conn) throws SQLException {
if(rs != null){
rs.close();
}
if(pstm != null){
pstm.close();
}
if(conn != null){
conn.close();
}
}
//所有写在代码里的main测试都是白盒测试/单元测试
public static void main(String[] args) {
try {
get_Conn();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
同时我们可以将用户类的操作也进行封装
public class User {
//1.对属性的封装
private int id; //用户ID
private String username; //用户名
private String password; //用户密码
//2.对get和set的封装
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
//3.构造方法
public User(int id, String username, String password) {
this.id = id;
this.username = username;
this.password = password;
}
public User(String username, String password) {
this.username = username;
this.password = password;
}
public User() {
}
//4.toString方法的重写
@Override
public String toString() {
return "User{" +
"id=" + id +
", username='" + username + '\'' +
", password='" + password + '\'' +
'}';
}
}
运用这种方式,我们接下来就可以进行简单的增删查改操作,以及遍历表种数据的操作了
//测试增加
public void addUser(User user){
try {
//1.获取连接
conn = DBUtil.get_Conn();
//2.获取存放sql语句的对象
pstm = conn.prepareStatement("insert into tb_user(username,password)values(?,?)");
//3.填坑
pstm.setString(1,user.getUsername());
pstm.setString(2,user.getPassword());
//4.执行sql并得到结果
int i = pstm.executeUpdate();
if(i>0){
System.out.println("增加成功");
}else{
System.out.println("增加失败");
}
} catch (Exception e) {
e.printStackTrace();
}finally {
try {
DBUtil.get_close(null,pstm,conn);
} catch (SQLException e) {
e.printStackTrace();
}
}
}
//删除
public void delUser(User user){
try {
conn = DBUtil.get_Conn();
pstm = conn.prepareStatement("delete from tb_user where username=? and password=?");
pstm.setString(1,user.getUsername());
pstm.setString(2,user.getPassword());
int i = pstm.executeUpdate();
if(i>0){
System.out.println("删除成功");
}else{
System.out.println("删除失败");
}
} catch (Exception e) {
e.printStackTrace();
}finally{
try {
DBUtil.get_close(null,pstm,conn);
} catch (SQLException e) {
e.printStackTrace();
}
}
}
//查询
public void selUser(User user){
try {
User userc;
conn = DBUtil.get_Conn();
pstm = conn.prepareStatement("select * from tb_user where username=? and password=?");
pstm.setString(1,user.getUsername());
pstm.setString(2,user.getPassword());
rs = pstm.executeQuery();
if(rs != null){
System.out.println("查询成功");
while(rs.next()){
System.out.println("用户ID:"+rs.getInt(1));
System.out.println("用户名"+rs.getString(2));
System.out.println("密码"+rs.getString(3));
}
}else{
System.out.println("查询失败");
}
} catch (Exception e) {
e.printStackTrace();
}finally {
try {
DBUtil.get_close(rs,pstm,conn);
} catch (SQLException e) {
e.printStackTrace();
}
}
}
//更新
public void upUser(User user){
try {
conn = DBUtil.get_Conn();
pstm = conn.prepareStatement("update tb_user set password=? where username=?");
pstm.setString(1,user.getPassword());
pstm.setString(2,user.getUsername());
int i = pstm.executeUpdate();
if(i>0){
System.out.println("修改成功");
}else{
System.out.println("修改失败");
}
} catch (Exception e) {
e.printStackTrace();
}finally {
try {
DBUtil.get_close(null,pstm,conn);
} catch (SQLException e) {
e.printStackTrace();
}
}
}
//遍历
public void allUser(){
try {
conn = DBUtil.get_Conn();
pstm = conn.prepareStatement("select * from tb_user");
rs = pstm.executeQuery();
while(rs.next()){
System.out.println("用户ID:"+rs.getInt(1));
System.out.println("用户名"+rs.getString(2));
System.out.println("密码"+rs.getString(3));
}
} catch (Exception e) {
e.printStackTrace();
}finally {
try {
DBUtil.get_close(null,pstm,conn);
} catch (SQLException e) {
e.printStackTrace();
}
}
}
标签:rs,day2,国际,pstm,实习,password,public,conn,user 来源: https://blog.csdn.net/Yehowah_Wang/article/details/95507566