(JDBC)01_JDBC基本使用------随堂练习

01_JDBC基本使用------随堂练习

练习题1:从控制台向数据库的表customers中插入一条数据,表结构如下:
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
练习题2:创立数据库表 examstudent,表结构如下:
在这里插入图片描述
向数据表中添加如下数据:
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
代码实现1:插入一个新的student 信息

请输入考生的详细信息
Type: 
IDCard:
ExamCard:
StudentName:
Location:
Grade:
信息录入成功!
@Test
	//获取连接的方法
	public   Connection getConnection() throws IOException, ClassNotFoundException, SQLException{
		//加载配置文件
		InputStream in = this.getClass().getClassLoader().getResourceAsStream("jdbc.properties");
		Properties pro = new Properties();
		pro.load(in);
		//定义四要素
		String url = pro.getProperty("url");
		String driver = pro.getProperty("driver");
		String user = pro.getProperty("user");
		String password = pro.getProperty("password");
		//加载mysql的driver
		Class.forName(driver);
		//获取连接
		Connection conn = DriverManager.getConnection(url, user, password);
		return conn;
	}
	@Test
	public void testpstmtInsert() throws ClassNotFoundException, IOException, SQLException {
		Scanner sc = new Scanner(System.in);
		System.out.println("请输入考生的详细信息:");
		System.out.println("Type:");
		String Type = sc.nextLine();
		System.out.println("IDCard:");
		String IDCard = sc.nextLine();
		System.out.println("ExamCard:");
		String ExamCard = sc.nextLine();
		System.out.println("StudentName:");
		String StudentName = sc.nextLine();
		System.out.println("Location");
		String Location = sc.nextLine();
		System.out.println("Grade:");
		String Grade = sc.nextLine();
		Connection conn = getConnection();
		String sql = "insert into examstudent(Type,IDCard,ExamCard,StudentName,Location,Grade) "
				+ "values('"+Type+"','"+IDCard+"','"+ExamCard+"','"+StudentName+"','"+Location+"','"+Grade+"')";
		PreparedStatement pstmt = conn.prepareStatement(sql);
		int i  = pstmt.executeUpdate();
		if(i != 0) {
			System.out.println("信息录入成功!");
		}else {
			System.out.println("信息录入失败!");
		}
		
		
	}

在这里插入图片描述
在这里插入图片描述

代码实现2:在 eclipse中建立 java 程序:输入身份证号或准考证号可以查询到学生的基本信息。结果如下:
在这里插入图片描述

@Test
    public void testpstmtQuery() throws ClassNotFoundException, IOException, SQLException, InstantiationException, IllegalAccessException, NoSuchFieldException, SecurityException {
		Scanner sc = new Scanner(System.in);
		System.out.println("请选择您要输入的类型");
		System.out.println("a:准考证号");
		System.out.println("b:身份证号");
		String aorb = sc.nextLine();
		
		examstudent exam = null;
		if(aorb.equals("a")) {
			System.out.println("请输入准考证号:");
			String ExamCard =sc.nextLine();
			Connection conn = getConnection();
			String sql = "select * from examstudent where ExamCard = '"+ExamCard+"'";
			
			PreparedStatement pstmt = conn.prepareStatement(sql);
			ResultSet res = pstmt.executeQuery();
			ResultSetMetaData rsmd = res.getMetaData();
			 while(res.next()) {
			    	exam = examstudent.class.newInstance(); 
			    	for(int i = 1 ; i <= rsmd.getColumnCount();i++) {
				    	String label = rsmd.getColumnLabel(i);
				    	Field field = exam.getClass().getDeclaredField(label);//使用别名来构建对象
				    	field.setAccessible(true);
				    	field.set(exam, res.getObject(i));  	
				    }
			    }
			if(exam !=null) {
				System.out.println(exam);
			}else {
				System.out.println("查无此人,请重新进入程序!");
			}
			 
			
			
		}else if(aorb.equals("b")) {
			System.out.println("请输入身份证号:");
			String IDCard =sc.nextLine();
			Connection conn = getConnection();
			String sql = "select * from examstudent where IDCard = '"+IDCard+"'";
			
			PreparedStatement pstmt = conn.prepareStatement(sql);
			ResultSet res = pstmt.executeQuery();
			ResultSetMetaData rsmd = res.getMetaData();
			 while(res.next()) {
			    	exam = examstudent.class.newInstance(); 
			    	for(int i = 1 ; i <= rsmd.getColumnCount();i++) {
				    	String label = rsmd.getColumnLabel(i);
				    	Field field = exam.getClass().getDeclaredField(label);//使用别名来构建对象
				    	field.setAccessible(true);
				    	field.set(exam, res.getObject(i));  	
				    }
			    }
			if(exam !=null) {
				System.out.println(exam);
			}else {
				System.out.println("查无此人,请重新进入程序!");
			}
		}else {
			System.out.println("您的输入有误!请重新进入程序。");
		}
		
	}

在这里插入图片描述
代码实现3:完成学生信息的删除功能
在这里插入图片描述

@Test
	public void testpstmtDelete() throws ClassNotFoundException, IOException, SQLException {
		System.out.println("请输入学生的考号:");
		Scanner sc = new Scanner(System.in);
		String ExamCard = sc.nextLine();
		Connection conn = getConnection();
		String sql= "delete from examstudent where ExamCard = '"+ExamCard+"'";
		PreparedStatement pstmt = conn.prepareStatement(sql);
		int i = pstmt.executeUpdate();
		if(i != 0) {
			System.out.println("删除成功!");
		}else {
			System.out.println("查无此人,请重新输入!");

		}
	}

在这里插入图片描述

发布了67 篇原创文章 · 获赞 6 · 访问量 1914

猜你喜欢

转载自blog.csdn.net/weixin_45801537/article/details/104909396