创建数据库
create database school_javas;
use school_javas;
create table student(
id INT(11),
name VARCHAR(4),
tel VARCHAR(11),
sex VARCHAR(1)
);
insert into student values(1,"李盼盼",13928376253,"男"),(2,"李萍萍",18276373453,"女"),
(3,"王明",12734647499,"男"),(4,"小明",13963547382,"男"),(5,"李华",19283746573,"女"),
(6,"李四",18236455748,"男"),(7,"王五",17364537288,"男"),(8,"张三",18236473838,"男"),
(9,"黎丽",13928376436,"女"),(10,"吴垒",17363283743,"男");
java代码:
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Connection;
import com.mysql.cj.jdbc.Driver;
public class MysqlDemoTest {
Connection connection;
public static void main(String[] args) throws SQLException {
// TODO Auto-generated method stub
MysqlDemoTest mysqlDemoTest = new MysqlDemoTest();
mysqlDemoTest.getConnection();
System.out.println("查询学生表数据,结果为:");
mysqlDemoTest.selectAll();
//System.out.println("新增学生表数据,结果为:");
//mysqlDemoTest.add(11,"刘备","18273643837","男");
//System.out.println("新增学生表数据,结果为:");
//mysqlDemoTest.add(12,"曹操","12873247638","男");
//System.out.println("新增学生表数据,结果为:");
//mysqlDemoTest.add(13,"小巧","13928327828","女");
//System.out.println("查询学生表数据,结果为:");
//mysqlDemoTest.selectAll();
System.out.println("查询电话号码是182开头的所有学生信息,结果为:");
mysqlDemoTest.query();
System.out.println("修改student表中第10条数据,电话改为13124235300,结果为:");
mysqlDemoTest.modify(5,"13124235300");
mysqlDemoTest.selectAll();
System.out.println("查询student表中所有男性的学生信息,结果为:");