1. 首页
  2. JAVA

JAVA连接数据库编程——java,mysql实现学生信息管理

直接上代码,需要下载mysql连接驱动的jar包

package lianxi;

import java.util.*;
import java.sql.*;

public class Mysqlfirst {
	public static void main(String[] args) {
		Scanner cin = new Scanner(System.in);
		int xuanze;
		int charu;
		int charu1;
		int ages;
		Connection con;
		PreparedStatement stmt;
		PreparedStatement stmt1;
		String driver = "com.mysql.cj.jdbc.Driver";
		String url = "jdbc:mysql://127.0.0.1:3306/mydata?useSSL=false&serverTimezone=UTC";
		String user = "root";
		String password = "mysql密码";
		try {
			Class.forName(driver);
			con = DriverManager.getConnection(url, user, password);
			if (!con.isClosed())
				System.out.println("Succeeded connecting to the Database!");
			Statement statement = con.createStatement();
			String sql = "select * from xinxi";
			String sql1 = "insert into xinxi values (?,?,?)";
			ResultSet rs = statement.executeQuery(sql);
			System.out.println("查询学生信息  输入1");
			System.out.println("查询课表信息  输入2");
			System.out.println("插入学生信息  输入3");
			System.out.println("插入课程信息  输入4");
			xuanze = cin.nextInt();
			if (xuanze == 5) {
				System.out.println("请输入年龄");
				ages = cin.nextInt();
				String sql4 = "select * from xinxi where age=";
				ResultSet re1 = statement.executeQuery(sql4);
				System.out.println("查询成功");
				System.out.println("姓名" + "\t" + "年龄");
				String name = null;
				int age = 0;
				while (re1.next()) {
					name = re1.getString("name");
					age = re1.getInt("age");
					System.out.println(name + " " + age);
				}
			}
			if (xuanze == 1) {
				System.out.println("获取数据库成功:");
				System.out.println("学号" + "\t" + "年龄" + "\t" + "姓名");
				String name = null;
				int id = 0;
				int age = 0;
				while (rs.next()) {
					id = rs.getInt("id");
					age = rs.getInt("age");
					name = rs.getString("name");
					System.out.println(id + " " + age + " " + name);
				}
			}
			rs.close();
			if (xuanze == 2) {
				stmt = (PreparedStatement) con.prepareStatement(sql1);
				System.out.println("是否选择插入学生信息 1:是  2:否");
				charu = cin.nextInt();
				System.out.println("请输入插入学生信息个数");
				int shu = cin.nextInt();
				System.out.println("请按照学号年龄姓名输入");
				for (int i = 1; i <= shu; i++) {
					int x = cin.nextInt();
					int y = cin.nextInt();
					String z = cin.next();
					stmt.setInt(1, x);
					stmt.setInt(2, y);
					stmt.setString(3, z);
					stmt.executeUpdate();
				}
				stmt.close();
			}
			if (xuanze == 3) {
				String sql2 = "select * from kebiao";
				ResultSet rs1 = statement.executeQuery(sql2);
				String km = null;
				String js = null;
				String xq = null;
				int kh = 0;
				int rl = 0;
				while (rs1.next()) {
					kh = rs1.getInt("kh");
					km = rs1.getString("km");
					js = rs1.getString("js");
					xq = rs1.getString("xq");
					rl = rs1.getInt("rl");
					System.out.println(kh + "\t  " + km + "\t  " + js + "\t  " + xq + "\t  " + rl);
				}
			}
			if (xuanze == 4) {
				int kh1;
				int rl1;
				String js1;
				String xq1;
				String km1;
				String sql3 = "insert into kebiao values (?,?,?,?,?)";
				stmt1 = (PreparedStatement) con.prepareStatement(sql3);
				System.out.println("请输入插入个数");
				int shu1 = cin.nextInt();
				for (int i = 1; i <= shu1; i++) {
					kh1 = cin.nextInt();
					km1 = cin.next();
					js1 = cin.next();
					xq1 = cin.next();
					rl1 = cin.nextInt();
					stmt1.setInt(1, kh1);
					stmt1.setString(2, km1);
					stmt1.setString(3, js1);
					stmt1.setString(4, xq1);
					stmt1.setInt(5, rl1);
					stmt1.executeUpdate();
				}
				System.out.println("插入成功");
				stmt1.close();
			}
			con.close();
		} catch (ClassNotFoundException e) {
			System.out.println("Sorry,can`t find the Driver!");
			e.printStackTrace();
		} catch (SQLException e) {
			e.printStackTrace();
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			System.out.println("数据库数据成功获取!!");
		}
	}
}

 

原创文章,作者:程序员凯小白,如若转载,请注明出处:http://www.kaixiaobai.com/archives/82

发表评论

登录后才能评论

联系我们

qq

在线咨询:点击这里给我发消息

邮件:1298646637@qq.com

工作时间:周一至周五,9:30-18:30,节假日休息