2018-12-28 作业

2018-12-28 作业

package com.neu_zuoye;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

public class zuoye {

	public static void main(String[] args) throws ClassNotFoundException, SQLException {
		Class.forName("com.mysql.jdbc.Driver");
		Connection connection = 
				DriverManager.getConnection("jdbc:mysql://localhost:3306/mydb", "root", "root");
		
		String sql = "select count(*) from student100 where sex = '女' and score >=80";
		
		Statement statement = connection.createStatement();
		
		ResultSet rs = statement.executeQuery(sql);
		
		while(rs.next()) {
			int n = rs.getInt(1);
			System.out.println(n);			
		}
		
		rs.close();
		statement.close();
		connection.close();
		
		
	}
	
	@org.junit.Test
	public void testzhang() throws ClassNotFoundException, SQLException{
		Class.forName("com.mysql.jdbc.Driver");
		Connection connection = 
				DriverManager.getConnection("jdbc:mysql://localhost:3306/mydb", "root", "root");
		
		String sql = "update student100 set score = 100 where name like('张%') and sex = '男'";
		
		Statement statement = connection.createStatement();
		
		int n = statement.executeUpdate(sql);
		
		if(n>0) {
			System.out.println("修改成功");
		}else {
			System.out.println("修改失败");
		}
		statement.close();
		connection.close();
	}
	@org.junit.Test
	public void test60() throws ClassNotFoundException, SQLException{
		
		Class.forName("com.mysql.jdbc.Driver");
		Connection connection = 
				DriverManager.getConnection("jdbc:mysql://localhost:3306/mydb", "root", "root");
		
		
		String sql = "select name,sex,score from student100 where sex ='女' and score >=60";
		
		Statement statement = connection.createStatement();
		ResultSet rs = statement.executeQuery(sql);
		
		while(rs.next()) {
			String name = rs.getString(1);
			String sex = rs.getString(2);
			int score = rs.getInt(3);
			System.out.println(name+sex+score);	
		}
		rs.close();
		statement.close();
		connection.close();
	}
	
	@org.junit.Test
	public void testAvg() throws ClassNotFoundException, SQLException{
		
		Class.forName("com.mysql.jdbc.Driver");
		Connection connection = 
				DriverManager.getConnection("jdbc:mysql://localhost:3306/mydb", "root", "root");
		
		
		String sql = "select sex,avg(score)\r\n" + 
					"from student100 m NATURAL join student100 w\r\\n" + 
					"group by sex";
		
		Statement statement = connection.createStatement();
		ResultSet rs = statement.executeQuery(sql);
		
		while(rs.next()) {
			String sex = rs.getString(1);
			double score = rs.getDouble(2);
			System.out.println(sex+","+score);	
		}
		
		rs.close();
		statement.close();
		connection.close();
	}
	
	@org.junit.Test
	public void testss() throws ClassNotFoundException, SQLException{
		
		Class.forName("com.mysql.jdbc.Driver");
		Connection connection = 
				DriverManager.getConnection("jdbc:mysql://localhost:3306/mydb", "root", "root");
		
		
		String sql = "select *\r\n" + 
				"from student100 \r\n" + 
				"where score > (select avg(score) avgs from student100)";
		
		Statement statement = connection.createStatement();
		ResultSet rs = statement.executeQuery(sql);
		
		while(rs.next()) {
			int id = rs.getInt(1);
			String name = rs.getString(2);
			String sex = rs.getString(3);
			int age = rs.getInt(4);
			int score = rs.getInt(5);
			System.out.println(id+","+name+","+sex+","+age+","+score);	
		}
		rs.close();
		statement.close();
		connection.close();
	}
	
	

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值