Java学生管理系统 数据库版
CREATE DATABASE schooljava;
USE schooljava;
CREATE TABLE student (
id INT(11),
name VARCHAR(25),
tel INT(11),
sex VARCHAR(6)
);
DESC student;
INSERT INTO student VALUES
(1,'张三',1234567890,'男'),
(2,'张四',1234567890,'男'),
(3,'张五',1234567890,'男'),
(4,'张六',1234567890,'男'),
(5,'张期',1234567890,'男'),
(6,'张八',1234567890,'女'),
(7,'张久',1234567890,'女'),
(8,'张氏',1234567890,'女'),
(9,'张一',1234567890,'男'),
(10,'张二',1234567890,'男');
package Seventeenth;
import com.mysql.cj.jdbc.Driver;
import java.sql.*;
public class schooljava {
Connection connection;
public static void main(String[] args) throws SQLException {
schooljava schooljava=new schooljava();
schooljava.getConnection();
schooljava.selectAll();
schooljava.add(11,"张尤",1824563490,"男");
schooljava.add(12,"张代",1394237890,"女");
schooljava.add(13,"张伊",1893153435,"男");
schooljava.select182();
schooljava.xiugai();
schooljava.chanan();
schooljava.dsg();
schooljava.select139();
schooljava.sc();
schooljava.chanv();
schooljava.selectAll();
}
public void add(int id, String name,int tel,String sex) throws SQLException {
PreparedStatement preparedStatement=connection.prepareStatement("insert into student values(?,?,?,?);");
preparedStatement.setInt(1, id);
preparedStatement.setString(2, name);
preparedStatement.setInt(3,tel);
preparedStatement.setString(4,sex);
preparedStatement.executeUpdate();
}
public void getConnection()throws SQLException {
DriverManager.registerDriver(new Driver());//第一步:注册驱动
connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/schooljava?useUnicode=true&characterEncoding=UTF-8", "root", "1234");
//第二步:获取连接
}
public void selectAll() throws SQLException { //查询课程表所有数据
PreparedStatement preparedStatement=connection.prepareStatement("select*from student");//第三步:获取statement对象
ResultSet resultSet=preparedStatement.executeQuery();//第四步:执行SQL语句返回结果集
while(resultSet.next()){
System.out.print(resultSet.getInt("id"));
System.out.print(resultSet.getString("name"));
System.out.print(resultSet.getInt("tel"));
System.out.println(resultSet.getString("sex"));//第五步:遍历结果集
}resultSet.close();
preparedStatement.close();}
public void select182() throws SQLException {
PreparedStatement preparedStatement=connection.prepareStatement("select*from student where tel like '182%'");
ResultSet resultSet=preparedStatement.executeQuery();
while(resultSet.next()){
System.out.print(resultSet.getInt("id"));
System.out.print(resultSet.getString("name"));
System.out.print(resultSet.getInt("tel"));
System.out.println(resultSet.getString("sex"));}
}
public void xiugai() throws SQLException {
PreparedStatement preparedStatement=connection.prepareStatement("update student set tel=1312423530 where id=5");
preparedStatement.executeUpdate( );
}
public void chanan() throws SQLException {
PreparedStatement preparedStatement=connection.prepareStatement("select*from student where sex='男'");
ResultSet resultSet=preparedStatement.executeQuery();
while(resultSet.next()){
System.out.print(resultSet.getInt("id"));
System.out.print(resultSet.getString("name"));
System.out.print(resultSet.getInt("tel"));
System.out.println(resultSet.getString("sex"));}
}
public void dsg() throws SQLException {
PreparedStatement preparedStatement=connection.prepareStatement("update student set name='大帅哥' where id=10");
preparedStatement.executeUpdate( );
}
public void select139() throws SQLException {
PreparedStatement preparedStatement=connection.prepareStatement("select*from student where tel like '139%'");
ResultSet resultSet=preparedStatement.executeQuery();
while(resultSet.next()){
System.out.print(resultSet.getInt("id"));
System.out.print(resultSet.getString("name"));
System.out.print(resultSet.getInt("tel"));
System.out.println(resultSet.getString("sex"));}
}
public void sc() throws SQLException {
PreparedStatement preparedStatement=connection.prepareStatement("delete from student where id=3");
preparedStatement.executeUpdate( );
}
public void chanv() throws SQLException {
PreparedStatement preparedStatement=connection.prepareStatement("select*from student where sex='女'");
ResultSet resultSet=preparedStatement.executeQuery();
while(resultSet.next()){
System.out.print(resultSet.getInt("id"));
System.out.print(resultSet.getString("name"));
System.out.print(resultSet.getInt("tel"));
System.out.println(resultSet.getString("sex"));}}
}