一、实验内容:
首先建立一个数据库,通过编程实现:
- 建立一个表,表名为职工,结构为:编号,姓名、性别、年龄、工资、职称;
2.向职工表中插入三条记录,如上表所示;并查询所有职工信息,输出到控制台。 3.将年龄在45岁以上的员工工资增加15%,其他人增加10%,存入原表中,并查询更新后的所有员工信息,输出到控制台。 4.删除工资超过1500的员工记录,并将每条记录按照工资由大到小的顺序排序显示输出到控制台。
二、实验代码:
【注】:这几部分还是分开写比较合适,获得结果更容易 (1)创建表:
package Test4;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Collection;
public class Test4_111 {
public static void main(String[] args) {
Statement stmt=null;
ResultSet rs=null;
Connection conn=null;
try {
Class.forName("com.mysql.cj.jdbc.Driver");
String url="jdbc:mysql://localhost:3306/jdbc";
String username="root";
String password="root";
conn =DriverManager.getConnection(url,username,password);
stmt=conn.createStatement();
String create="create table users("
+ "id int(40),"
+ "name varchar(40), "
+ "sex varchar(40), "
+ "age varchar(40), "
+ "wage varchar(40), "
+ "work varchar(40))";
stmt.executeLargeUpdate(create);
}catch(ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
}finally {
if(stmt!=null) {
try {
stmt.close();
}catch(SQLException e) {
e.printStackTrace();
}
stmt=null;
}
if(conn!=null) {
try {
conn.close();
}catch(SQLException e) {
e.printStackTrace();
}
conn=null;
}
}
}
private static Object age(int i) {
return null;
}
}
(2)插入表:
package Test4;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Collection;
public class Test4_222 {
public static void main(String[] args) {
Statement stmt=null;
ResultSet rs=null;
Connection conn=null;
try {
Class.forName("com.mysql.cj.jdbc.Driver");
String url="jdbc:mysql://localhost:3306/jdbc";
String username="root";
String password="root";
conn =DriverManager.getConnection(url,username,password);
stmt=conn.createStatement();
String select="select *from users";
String create="create table users("
+ "id int(40),"
+ "name varchar(40), "
+ "sex varchar(40), "
+ "age varchar(40), "
+ "wage varchar(40), "
+ "work varchar(40))";
String insert = "insert into users(id,name,sex,age,wage,work)\r\n"
+ " Values(1001,'丁卫国','男',25,2500,'助工'),\r\n"
+ " (1002,'张小华','女',30,1000,'工程师'),\r\n"
+ " (1003,'宁涛','男',50,600,'高工')";
rs=stmt.executeQuery(select);
System.out.println("id | name | sex | "+
"age | wage | work | ");
while(rs.next()) {
int id=rs.getInt("id");
String name=rs.getString("name");
String sex=rs.getString("sex");
double wage=rs.getDouble("wage");
int age=rs.getInt("age");
String work=rs.getString("work");
System.out.println(id+" | "+name+" | "+sex+" | "
+age+" | "+wage+" | "+work+" | ");
}
}catch(ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
}finally {
if(stmt!=null) {
try {
stmt.close();
}catch(SQLException e) {
e.printStackTrace();
}
stmt=null;
}
if(conn!=null) {
try {
conn.close();
}catch(SQLException e) {
e.printStackTrace();
}
conn=null;
}
}
}
private static Object age(int i) {
return null;
}
}
(3)增加工资,修改:
package Test4;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Collection;
public class Test4_333 {
public static void main(String[] args) {
Statement stmt=null;
ResultSet rs=null;
Connection conn=null;
try {
Class.forName("com.mysql.cj.jdbc.Driver");
String url="jdbc:mysql://localhost:3306/jdbc";
String username="root";
String password="root";
conn =DriverManager.getConnection(url,username,password);
stmt=conn.createStatement();
String select="select *from users";
String create="create table users("
+ "id int(40),"
+ "name varchar(40), "
+ "sex varchar(40), "
+ "age varchar(40), "
+ "wage varchar(40), "
+ "work varchar(40))";
String insert = "insert into users(id,name,sex,age,wage,work)\r\n"
+ " Values(1001,'丁卫国','男',25,2500,'助工'),\r\n"
+ " (1002,'张小华','女',30,1000,'工程师'),\r\n"
+ " (1003,'宁涛','男',50,600,'高工')";
stmt.executeLargeUpdate(create);
stmt.executeLargeUpdate(insert);
String update = "update users set wage = wage + wage * 0.1 where age < 45";
stmt.executeUpdate(update); String update1 =
"update users set wage = wage + wage * 0.15 where age > 45";
stmt.executeUpdate(update1);
}catch(ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
}finally {
if(stmt!=null) {
try {
stmt.close();
}catch(SQLException e) {
e.printStackTrace();
}
stmt=null;
}
if(conn!=null) {
try {
conn.close();
}catch(SQLException e) {
e.printStackTrace();
}
conn=null;
}
}
}
private static Object age(int i) {
return null;
}
}
(4)删除记录,并排序:
package Test4;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Collection;
public class Test4_444 {
public static void main(String[] args) {
Statement stmt=null;
ResultSet rs=null;
Connection conn=null;
try {
Class.forName("com.mysql.cj.jdbc.Driver");
String url="jdbc:mysql://localhost:3306/jdbc";
String username="root";
String password="root";
conn =DriverManager.getConnection(url,username,password);
stmt=conn.createStatement();
String select="select *from users";
String create="create table users("
+ "id int(40),"
+ "name varchar(40), "
+ "sex varchar(40), "
+ "age varchar(40), "
+ "wage varchar(40), "
+ "work varchar(40))";
String insert = "insert into users(id,name,sex,age,wage,work)\r\n"
+ " Values(1001,'丁卫国','男',25,2500,'助工'),\r\n"
+ " (1002,'张小华','女',30,1000,'工程师'),\r\n"
+ " (1003,'宁涛','男',50,600,'高工')";
String delete = "delete FROM users where wage > 1500";
String select1 = "select * from users ORDER BY wage ASC";
stmt.executeUpdate(delete);
rs=stmt.executeQuery(select1);
System.out.println("id | name | sex | "+
"age | wage | work | ");
while(rs.next()) {
int id=rs.getInt("id");
String name=rs.getString("name");
String sex=rs.getString("sex");
double wage=rs.getDouble("wage");
int age=rs.getInt("age");
String work=rs.getString("work");
System.out.println(id+" | "+name+" | "+sex+" | "
+age+" | "+wage+" | "+work+" | ");
}
}catch(ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
}finally {
if(stmt!=null) {
try {
stmt.close();
}catch(SQLException e) {
e.printStackTrace();
}
stmt=null;
}
if(conn!=null) {
try {
conn.close();
}catch(SQLException e) {
e.printStackTrace();
}
conn=null;
}
}
}
private static Object age(int i) {
return null;
}
}
三、运行结果:
(1)创建数据库:
(2)创建表:
(3)插入数据表: 控制台:
(4)根据条件,修改工资: (5)删除工资超过1500的记录: 按工资从大到小排序:
|