学生选课系统数据库版

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
 
import com.mysql.cj.jdbc.Driver;
 
public class homework {
	Connection connection;
	public static void main(String[] args) throws SQLException {
		homework homework =new homework ();
		homework .getConnection();
		System.out.println("查询student表中所有学生信息");
		homework .selectall();
		System.out.println("student表中新增三条数据");
		homework.add(11,"赵五",567894345,"男");
		homework.add(12,"戚六",244767345,"女");
		homework.add(13,"刘五",435894345,"男");
		homework .selectall();
		System.out.println("查询以182开头的信息");
		homework.select();
		System.out.println("修改student表中第五条数据,电话改为1312423530");
		homework.update(5,1312423530);
		homework .selectall();
		System.out.println("查询student表中所有男性学生信息");
		homework.selectmandate("男");
		System.out.println("修改student表中第十条数据,姓名改为”大帅哥");
		homework.updatedate(10,"大帅哥");
		homework .selectall();
		System.out.println("查询student表中以139开头的所有学生信息要用like");
		homework.selecttel();
		System.out.println("删除student表中第三条数据");
		homework.delete(3);
		homework .selectall();
		System.out.println("查询student表中所有女性数据");
		homework.selectwomandate("女");
		System.out.println("查询student表中所有学生数据");
		homework.selectalltwo();
		homework .close();
	}
	
	private void selectalltwo() throws SQLException {
		PreparedStatement preparedStatement=connection.prepareStatement("select*from student;");		
		//第四步:执行SQL语句返回结果集
		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")+" ");
         }
		
	}
 
	private void selectwomandate(String name) throws SQLException {
		PreparedStatement preparedStatement=connection.prepareStatement("select * from student where sex=?;");
		preparedStatement.setString(1,name);
		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")+" ");
	         }
		preparedStatement.close();
		
	}
 
	private void delete(int id) throws SQLException {
		PreparedStatement preparedStatement=connection.prepareStatement("delete from student where id=?;");	
		preparedStatement.setInt(1,id);
		preparedStatement.executeUpdate();
		preparedStatement.close();
	}
 
	private void selecttel() 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")+" ");
	         }
		preparedStatement.close();
		
	}
 
	private void updatedate(int id, String name) throws SQLException {
		PreparedStatement preparedStatement=connection.prepareStatement("update student set name=? where id=?;");
		preparedStatement.setString(1,name);
		preparedStatement.setInt(2,id);
		preparedStatement.executeUpdate();
		preparedStatement.close();
		
	}
 
	private void selectmandate(String sex) throws SQLException {
		PreparedStatement preparedStatement=connection.prepareStatement("select * from student where sex=?;");
		preparedStatement.setString(1,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")+" ");
	         }
		preparedStatement.close();
		
	}
 
	private void update(int id,  int tel) throws SQLException {
		PreparedStatement preparedStatement=connection.prepareStatement("update student set tel=? where id=?;");
		preparedStatement.setInt(1,tel);
		preparedStatement.setInt(2,id);
		preparedStatement.executeUpdate();
		preparedStatement.close();
	}
	private void select() 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")+" ");
	         }
		preparedStatement.close();
	}
	private 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();
		preparedStatement.close();
	}
	public void getConnection() throws SQLException {
		//第一步:注册驱动
				DriverManager.registerDriver(new Driver());	
				//第二步:获取连接
		     connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/schooljava","root","1234");	
	}
	public void selectall() throws SQLException {
		//第三步:获取statement对象                                //jdbc技术:什么数据库://地址(localhost本地地址):3306数据库端口/数据库的名字
				PreparedStatement preparedStatement=connection.prepareStatement("select*from student;");		
				//第四步:执行SQL语句返回结果集
				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 close() throws SQLException {
		connection.close();
	}
	}

你可能感兴趣的:(数据库,开发语言)