@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 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();
}
}
@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();
}
}
需插入:System.out .printf("\33[31m删除成功:%d条\33[0m%n",ok);
@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();
}
}
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;
/**
* Project: jdbcdemo - StatementDemo
* Powered by MingZi On 2023-08-11 20:25:20
* 描述:
*
* @author MingZi [MingZi@[email protected]]
* @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();
}
}
}