首先建立一个数据库,通过编程实现:
- 建立一个表,表名为职工,结构为:编号,姓名、性别、年龄、工资、职称;
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; //202056228杨丽花 public class Test4_111 { public static void main(String[] args) { // TODO Auto-generated method stub Statement stmt=null; ResultSet rs=null; Connection conn=null; try { //1.注册数据库的驱动 Class.forName("com.mysql.cj.jdbc.Driver");//版本为6.02之后 //2.通过DriverManager获得数据库的连接 String url="jdbc:mysql://localhost:3306/jdbc"; String username="root"; String password="root"; conn =DriverManager.getConnection(url,username,password); //3.通过 Connection对象获取Statement对象 stmt=conn.createStatement(); //4.使用Statement执行SQL语句 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) { // TODO Auto-generated catch block e.printStackTrace(); }finally { //6.回收数据库资源 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) { // TODO Auto-generated method stub 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; //202056228杨丽花 public class Test4_222 { public static void main(String[] args) { // TODO Auto-generated method stub Statement stmt=null; ResultSet rs=null; Connection conn=null; try { //1.注册数据库的驱动 Class.forName("com.mysql.cj.jdbc.Driver");//版本为6.02之后 //2.通过DriverManager获得数据库的连接 String url="jdbc:mysql://localhost:3306/jdbc"; String username="root"; String password="root"; conn =DriverManager.getConnection(url,username,password); //3.通过 Connection对象获取Statement对象 stmt=conn.createStatement(); //4.使用Statement执行SQL语句 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)rn" + " Values(1001,'丁卫国','男',25,2500,'助工'),rn" + " (1002,'张小华','女',30,1000,'工程师'),rn" + " (1003,'宁涛','男',50,600,'高工')"; //创建表 ///stmt.executeLargeUpdate(create); //插入表 //stmt.executeLargeUpdate(insert); rs=stmt.executeQuery(select); //5. *** 作ResultSet结果集 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) { // TODO Auto-generated catch block e.printStackTrace(); }finally { //6.回收数据库资源 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) { // TODO Auto-generated method stub 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; //202056228杨丽花 public class Test4_333 { public static void main(String[] args) { // TODO Auto-generated method stub Statement stmt=null; ResultSet rs=null; Connection conn=null; try { //1.注册数据库的驱动 Class.forName("com.mysql.cj.jdbc.Driver");//版本为6.02之后 //2.通过DriverManager获得数据库的连接 String url="jdbc:mysql://localhost:3306/jdbc"; String username="root"; String password="root"; conn =DriverManager.getConnection(url,username,password); //3.通过 Connection对象获取Statement对象 stmt=conn.createStatement(); //4.使用Statement执行SQL语句 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)rn" + " Values(1001,'丁卫国','男',25,2500,'助工'),rn" + " (1002,'张小华','女',30,1000,'工程师'),rn" + " (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) { // TODO Auto-generated catch block e.printStackTrace(); }finally { //6.回收数据库资源 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) { // TODO Auto-generated method stub 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; //202056228杨丽花 public class Test4_444 { public static void main(String[] args) { // TODO Auto-generated method stub Statement stmt=null; ResultSet rs=null; Connection conn=null; try { //1.注册数据库的驱动 Class.forName("com.mysql.cj.jdbc.Driver");//版本为6.02之后 //2.通过DriverManager获得数据库的连接 String url="jdbc:mysql://localhost:3306/jdbc"; String username="root"; String password="root"; conn =DriverManager.getConnection(url,username,password); //3.通过 Connection对象获取Statement对象 stmt=conn.createStatement(); //4.使用Statement执行SQL语句 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)rn" + " Values(1001,'丁卫国','男',25,2500,'助工'),rn" + " (1002,'张小华','女',30,1000,'工程师'),rn" + " (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); //5. *** 作ResultSet结果集 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) { // TODO Auto-generated catch block e.printStackTrace(); }finally { //6.回收数据库资源 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) { // TODO Auto-generated method stub return null; } }三、运行结果:
(1)创建数据库:
(2)创建表:
(3)插入数据表:
控制台:
(4)根据条件,修改工资:
(5)删除工资超过1500的记录:
按工资从大到小排序:
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)