首先你需要创建 数据库 和 s_students学生表,再进行下一步的 增(add) ,删(del) ,改(update),查(query)。
查询所有学生姓名:
java
@Test
void query(){
try{
Statement st = conn.createStatement();
ResultSet rs = st.executeQuery("select * from t_students");
//ResultSet rs = st.executeQuery("select * from t_students order by sid desc limit 3");
while(rs.next()){
System.out.printf("sid:%d,sname:%s%n",rs.getInt(1),rs.getString("sname"));
}
}catch (Exception e){
e.printStackTrace();
}
}
执行结果:
查询最后3名学生姓名:t_students后 + "order by sid desc limit 3" (双引号内)
增加一名学生信息:姓名,性别,年龄,得分,出生日期,专业
(红豆,男,20,90,2023-8-11,软件)
java
@Test
void add(){
//使用statmement语句 insert into 语句
try{
Statement simt = conn.createStatement();
String sql =String.format("insert into t_students values(null,'%s','%s','%d','%d','%s','%s')", "红豆","男",20,90,"2023-8-11","软件");
System.out.println(sql);
int rows = simt.executeUpdate(sql);
if(rows >0){
System.out.printf("数据库插入成功: %d条%n",rows);
}
query();
}catch (Exception e){
e.printStackTrace();
}
}
执行结果:
插入成功并输出语句内容:
以学生sid 为16,17为例进行删除信息
删除 sid为16的学生:
java
@Test
void del(){
//使用st 语句删除
try{
Statement st = conn.createStatement();
String sql = String.format("delete from t_students where sid = %d",16);
int ok = st.executeUpdate(sql);
if(ok>0){
System.out.printf("删除成功:%d条%n",ok);
}else {
System.out.printf("\33[31m删除成功:%d条\33[0m%n",ok);
}
System.out.println();
query();
}catch (Exception e){
e.printStackTrace();
}
}
执行结果:
再次删除sid为16的学生会显示:
需插入:System.out .printf("\33[31m删除成功:%d条\33[0m**%n",ok);**
修改学生姓名:sid = 17的学生姓名红豆更改为小强
java
@Test
void update(){
try{
Statement st = conn.createStatement();
String sql = String.format("update t_students set sname = '%s' where sid = %d","小强",17);
System.out.println(sql);
int i = st.executeUpdate(sql);
if(i >0){
System.out.printf("修改数据:%d条%n",i);
}else {
System.out.printf("修改数据:%d条%n",i);
}
query();
}catch (Exception e){
e.printStackTrace();
}
}
执行结果:
全部代码:
java
package cn.mingzi;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
/**
* <p>Project: jdbcdemo - StatementDemo</p>
* <p>Powered by MingZi On 2023-08-11 20:25:20</p>
* <p>描述:<p>
*
* @author MingZi [MingZi@1541521841@qq.com]
* @version 1.0
* @since 17
*/
public class StatementDemo {
@Test
void query(){
try{
Statement st = conn.createStatement();
ResultSet rs = st.executeQuery("select * from t_students");
//ResultSet rs = st.executeQuery("select * from t_students order by sid desc limit 3");
while(rs.next()){
System.out.printf("sid:%d,sname:%s%n",rs.getInt(1),rs.getString("sname"));
}
}catch (Exception e){
e.printStackTrace();
}
}
@Test
void update(){
try{
Statement st = conn.createStatement();
String sql = String.format("update t_students set sname = '%s' where sid = %d","小强",17);
System.out.println(sql);
int i = st.executeUpdate(sql);
if(i >0){
System.out.printf("修改数据:%d条%n",i);
}else {
System.out.printf("修改数据:%d条%n",i);
}
query();
}catch (Exception e){
e.printStackTrace();
}
}
@Test
void del(){
//使用st 语句删除
try{
Statement st = conn.createStatement();
String sql = String.format("delete from t_students where sid = %d",16);
int ok = st.executeUpdate(sql);
if(ok>0){
System.out.printf("删除成功:%d条%n",ok);
}else {
System.out.printf("\33[31m删除成功:%d条\33[0m%n",ok);
}
System.out.println();
query();
}catch (Exception e){
e.printStackTrace();
}
}
@Test
void add(){
//使用statmement语句 insert into 语句
try{
Statement simt = conn.createStatement();
String sql =String.format("insert into t_students values(null,'%s','%s','%d','%d','%s','%s')", "红豆","男",20,90,"2023-8-11","软件");
System.out.println(sql);
int rows = simt.executeUpdate(sql);
if(rows >0){
System.out.printf("数据库插入成功: %d条%n",rows);
}
//ddl create database drop table
//stmt.execute(String sql); 执行语句,返回 true false
//insert delete update
//int i = stmt.executeUpdate(String sql); 返回影响的行
//select show query 查询
//ResultSet rs = simt.executeQuery(String sql); 返回ResultSet
query();
}catch (Exception e){
e.printStackTrace();
}
}
Connection conn;
String url ="jdbc:mysql:/dbok";
String user = "root";
String password = "";
@BeforeEach
void init() {
try {
conn = DriverManager.getConnection(url, user, password);
} catch (Exception e) {
e.printStackTrace();
}
}
@AfterEach
void close(){
try{
conn.close();
}catch (Exception e){
e.printStackTrace();
}
}
}