package test;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
public class Test {
public static void main(String[] args) {
try {//1.try
//2.forName
Class.forName("com.mysql.jdbc.Driver");
//3.获取cs
Connection cs = DriverManager.getConnection("jdbc:mysql://localhost:3306/myschool", "root", "root");
//4.创建sql 哪怕" "里 空着 也得写
String sql="select g.gradeName,count(*) from Student s,grade g where s.gradeId =g.gradeId group by g.gradeId";
//5.获取 ps
PreparedStatement ps =cs.prepareStatement(sql);
//6.根据情况是否 设置值
//ps.setObject(1, ""); 要不要这个 ,要几个 取决于上面sql语句中有没有问号,有几个问号 也就是查询条件
//7.获取rs
ResultSet rs = ps.executeQuery();
//8.遍历结果集
while (rs.next()) {
System.out.println(rs.getString(1)+"班级总人数为"+rs.getInt(2)+"人。");
}
}
catch (Exception e) {//9.处理异常
e.printStackTrace();
}finally{ //10.释放资源 如果想做这步 必须将cs ps rs 的声明 放到try外面
//释放资源
}
}
}
#1.查出工资最终的前5个员工 (没准备那么多数据,改成前5)
SELECT * FROM person ORDER BY `Wages` DESC LIMIT 0,5
#2.查询出男生与女生各自的总人数
SELECT COUNT(*) FROM person GROUP BY `Gender`;
#3.本题有误
#4.将所有员工中小于3500的工资提高10%
UPDATE person SET `Wages`=`Wages`+`Wages`*0.1 WHERE `Wages`<3500;
package accp;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
public class PersonTest {
public static void main(String[] args) {
Connection cs = null;
PreparedStatement ps =null;
ResultSet rs =null;
try {
//1.连接 20
Class.forName("com.mysql.jdbc.Driver");
cs = DriverManager.getConnection("jdbc:mysql://localhost:3306/p?characterEncoding=utf-8", "root", "root");
//2.sql语句 10
String sql="SELECT * FROM person WHERE NAME LIKE ?";
ps = cs.prepareStatement(sql);
String n ="王";
ps.setObject(1,"%"+n+"%");
//3.获取结果 10
rs = ps.executeQuery();
//4.处理结果
while (rs.next()) {
System.out.println(rs.getInt("id")+"\t"+rs.getString("name")+"\t"+rs.getFloat("wages")+"\t"+rs.getString("birthday")+"\t"+rs.getString("gender"));
}
} catch (Exception e) {
e.printStackTrace();
}finally{
try {
rs.close();
ps.close();
cs.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
}
执行脚本如下:
/*
SQLyog Ultimate v12.08 (64 bit)
MySQL - 5.7.22-log : Database - p
*********************************************************************
*/
/*!40101 SET NAMES utf8 */;
/*!40101 SET SQL_MODE=''*/;
/*!40014 SET @OLD_UNIQUE_CHECKS=@@UNIQUE_CHECKS, UNIQUE_CHECKS=0 */;
/*!40014 SET @OLD_FOREIGN_KEY_CHECKS=@@FOREIGN_KEY_CHECKS, FOREIGN_KEY_CHECKS=0 */;
/*!40101 SET @OLD_SQL_MODE=@@SQL_MODE, SQL_MODE='NO_AUTO_VALUE_ON_ZERO' */;
/*!40111 SET @OLD_SQL_NOTES=@@SQL_NOTES, SQL_NOTES=0 */;
CREATE DATABASE /*!32312 IF NOT EXISTS*/`p` /*!40100 DEFAULT CHARACTER SET utf8 */;
USE `p`;
/*Table structure for table `person` */
DROP TABLE IF EXISTS `person`;
CREATE TABLE `person` (
`Id` int(4) NOT NULL,
`Name` varchar(30) DEFAULT NULL,
`Wages` float DEFAULT NULL,
`Birthday` datetime DEFAULT NULL,
`Gender` varchar(10) DEFAULT NULL,
PRIMARY KEY (`Id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
/*Data for the table `person` */
insert into `person`(`Id`,`Name`,`Wages`,`Birthday`,`Gender`) values (111,'张三',2750,'2019-05-11 00:00:00','男'),(112,'李思',5000,'2019-02-22 00:00:00','女'),(113,'王姗姗',3500,'2019-04-16 00:00:00','女'),(114,'赵晓刚',6000,'2019-03-21 00:00:00','男'),(115,'田琪',8000,'2019-05-01 00:00:00','女'),(116,'张风光',5000,'2019-05-11 00:00:00','男'),(117,'李天启',3500,'2019-02-22 00:00:00','女'),(118,'王晓虎',6000,'2019-04-16 00:00:00','女'),(119,'赵梓龙',7000,'2019-03-21 00:00:00','男');
/*!40101 SET SQL_MODE=@OLD_SQL_MODE */;
/*!40014 SET FOREIGN_KEY_CHECKS=@OLD_FOREIGN_KEY_CHECKS */;
/*!40014 SET UNIQUE_CHECKS=@OLD_UNIQUE_CHECKS */;
/*!40111 SET SQL_NOTES=@OLD_SQL_NOTES */;
#创建数据库
CREATE DATABASE dbst;
#使用数据表
USE dbst;
#创建学生表
CREATE TABLE student(
sid INT(4),
sname VARCHAR(30),
tid INT(4)
);
#创建教师表
CREATE TABLE teacher(
tid INT (4),
tname VARCHAR(20),
tage INT(3)
);
# 更改表字段的字符编码
ALTER TABLE `teacher` CONVERT TO CHARSET utf8;
#添加教师信息
INSERT teacher VALUES(1,'李思米',7);
INSERT teacher VALUES(2,'张雪',6);
INSERT teacher VALUES(3,'李小光',3);
INSERT teacher VALUES(4,'王思聪',2);
#添加学生信息
INSERT `student` VALUES(101,'WSF',2);
INSERT `student` VALUES(102,'GDW',3);
INSERT `student` VALUES(103,'HDW',1);
INSERT `student` VALUES(104,'HTE',4);
INSERT `student` VALUES(105,'WRT',1);
INSERT `student` VALUES(106,'QGD',3);
INSERT `student` VALUES(105,'WRT',2);
INSERT `student` VALUES(106,'QGD',4);
#1.查询教师工龄超过5年的姓李的教师信息
SELECT * FROM `teacher` WHERE `tname` LIKE '李%' AND `tage`>5;
#2.查询张雪老师管理的学生总人数
SELECT COUNT(*) FROM `student`,`teacher` WHERE `student`.`tid` =`teacher`.`tid` AND `teacher`.`tname` ='张雪';
#3.查询所有的学生信息并显示教师的姓名
SELECT `student`.`sid`,`student`.`sname`,`teacher`.`tname` FROM `student`,`teacher` WHERE `student`.`tid`=`teacher`.`tid` ;
4.完成添加 : 如果报字符编码错误 : mysql中 添加 这条代码 ALTER TABLE `student` CONVERT TO CHARSET utf8;
package accp;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
public class StuTest {
public static void main(String[] args) {
Connection cs = null;
PreparedStatement ps =null;
ResultSet rs =null;
try {
//1.连接 20
Class.forName("com.mysql.jdbc.Driver");
cs = DriverManager.getConnection("jdbc:mysql://localhost:3306/dbst?characterEncoding=utf-8", "root", "root");
//2.sql语句 10
String sql="insert student values(?,?,?)";
ps = cs.prepareStatement(sql);
ps.setObject(1,101);
ps.setObject(2,"小强");
ps.setObject(3,3);
//3.获取结果 10
int row = ps.executeUpdate();
//4.处理结果
if(row>0){
System.out.println("添加成功!");
}
} catch (Exception e) {
e.printStackTrace();
}finally{
try {
ps.close();
cs.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
}