package com.wj.demo01;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import org.junit.Test;
/**
* 增删改查操作
* @author 开心
*
*/
public class Demo02 {
@Test
public void query() {
//注册驱动
//获取连接
//创建发送sql语句的对象
//执行sql语句
//处理结果集
//关闭资源
Connection conn=null;
Statement stmt=null;
ResultSet rs=null;
try {
Class.forName("com.mysql.jdbc.Driver");
String url="jdbc:mysql://localhost:3306/mydb3";
String user="root";
String password="1234";
conn=DriverManager.getConnection(url, user, password);
stmt=conn.createStatement();
rs=stmt.executeQuery("select * from student");
while(rs.next()) {
int id=rs.getInt("id");
String name=rs.getString("name");
String city=rs.getString("city");
int age=rs.getInt("age");
System.out.println(id+":"+name+":"+city+":"+age);
}
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
}finally {
try {
if(null!=rs) {
rs.close();
}
} catch (SQLException e) {
e.printStackTrace();
}
try {
if(null!=stmt) {
stmt.close();
}
} catch (SQLException e) {
e.printStackTrace();
}
try {
if(null!=conn) {
conn.close();
}
} catch (SQLException e) {
e.printStackTrace();
}
}
}
@Test
public void delete() {
//注册驱动
//获得连接
//获取发送sql语句的对象
//执行sql语句
//如果有结果集就处理结果集
//关闭资源
Connection conn=null;
Statement stmt=null;
try {
Class.forName("com.mysql.jdbc.Driver");
conn=DriverManager.getConnection("jdbc:mysql://localhost:3306/mydb3","root", "1234");
stmt=conn.createStatement();
String sql="delete from student where id=11";
int count=stmt.executeUpdate(sql);
System.out.println("删除的行数是"+count);
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
}finally {
try {
if(null!=stmt) {
stmt.close();
}
} catch (SQLException e) {
e.printStackTrace();
}
try {
if(conn!=null) {
conn.close();
}
} catch (SQLException e) {
e.printStackTrace();
}
}
}
//测试JDBC的修改操作
@Test
public void update() {
//注册驱动
//获得连接
//创建发送sql语句的对象
//执行sql语句,
//如果有结果集就处理结果集
//关闭资源
Connection conn=null;
Statement stmt=null;
try {
Class.forName("com.mysql.jdbc.Driver");
String url="jdbc:mysql://localhost:3306/mydb3";
String user="root";
String password="1234";
conn=DriverManager.getConnection(url, user, password);
stmt=conn.createStatement();
String sql="update student set name='开心',city='广州' where id=11 ";
int count=stmt.executeUpdate(sql);
System.out.println("受影响的行数"+count);
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
}finally {
try {
if(stmt!=null) {
stmt.close();
}
} catch (SQLException e) {
e.printStackTrace();
}
try {
if(conn!=null) {
conn.close();
}
} catch (SQLException e) {
e.printStackTrace();
}
}
}
//添加操作
@Test
public void insert() {
Connection conn=null;
Statement stmt=null;
try {
//注册驱动
Class.forName("com.mysql.jdbc.Driver");
String url="jdbc:mysql://localhost:3306/mydb3";
String user="root";
String password="1234";
conn=DriverManager.getConnection(url,user,password);
//获取发送sql语句的对象
stmt=conn.createStatement();
String sql="insert into student values(null,'开心','广州',18)";
//执行sql语句
int count=stmt.executeUpdate(sql);
System.out.println("受影响的行数是"+count);
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
}finally {
try {
if(stmt!=null) {
stmt.close();
}
} catch (SQLException e) {
e.printStackTrace();
}
try {
if(conn!=null) {
conn.close();
}
} catch (SQLException e) {
e.printStackTrace();
}
}
}
}
Java 小白 jabc增删改查(未优化)
最新推荐文章于 2021-09-06 16:29:24 发布
本文介绍了如何使用Java进行JDBC的增删改查操作,包括查询学生信息、删除记录、更新数据和插入新数据。通过实例演示了连接数据库、执行SQL语句及错误处理,并展示了如何使用DriverManager、Connection和Statement等关键组件。
3911

被折叠的 条评论
为什么被折叠?



