1.写SQL语句a)创建phone_book数据库b)创建info数据表包含 id、name姓名、tel电话、sex性别字段c)往info表中加10条数据2.写Java代码

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

public class Zy3 {
    static Connection con;  
    static PreparedStatement sql; 
    static ResultSet res;
    public Connection getConnection() {
        
        try{
        Class.forName("com.mysql.jdbc.Driver");
        con  =DriverManager.getConnection("jdbc:mysql:"+"//127.0.0.1:3306/phone_book", "root", "123456");
        }catch (Exception e){
            e.printStackTrace();
        }
        return con;
    }
    public static void main(String[] args) {
        
        Zy3 c= new Zy3();  
        con= c.getConnection(); 
        try{
            sql=con.prepareStatement("select*from info");
            res=sql.executeQuery();
            System.out.println("查询通讯录信息:");
            while (res.next()){
                String  id=res.getString("id");
                String name =res.getString("name");
                String tel =res.getString("tel");
                String sex =res.getString("sex");
                System.out.print("学号:"+id);
                System.out.print(" 姓名:"+name);
                System.out.print(" 电话号码:"+tel);
                System.out.println(" 性别:"+sex);
            }
            System.out.println("------------------------------------------");
            sql=con.prepareStatement("insert into info "+ "values(?,?,?,?)");
            sql.setString(1, "111");
            sql.setString(2, "萧丽");
            sql.setString(3, "15047890635");
            sql.setString(4, "女");
            sql.executeUpdate();
            sql.setString(1, "112");
            sql.setString(2, "小刘");
            sql.setString(3, "13889048394");
            sql.setString(4, "男");
            sql.executeUpdate();
            sql.setString(1, "113");
            sql.setString(2, "小胡");
            sql.setString(3, "15047856925");
            sql.setString(4, "女");
            sql.executeUpdate();
            System.out.println("增加后的数据");
            sql=con.prepareStatement("select*from info");
            res=sql.executeQuery();
            while (res.next()){
                String  id=res.getString("id");
                String name =res.getString("name");
                String tel =res.getString("tel");
                String sex =res.getString("sex");
                System.out.print("学号:"+id);
                System.out.print(" 姓名:"+name);
                System.out.print(" 电话号码:"+tel);
                System.out.println(" 性别:"+sex);
            }
            System.out.println("------------------------------------------");
            System.out.println("查询182开头的信息");
            sql=con.prepareStatement("select * from info where "+"tel like '182%'");
            res=sql.executeQuery();
            while (res.next()){
                String  id=res.getString("id");
                String name =res.getString("name");
                String tel =res.getString("tel");
                String sex =res.getString("sex");
                System.out.print("学号:"+id);
                System.out.print(" 姓名:"+name);
                System.out.print(" 电话号码:"+tel);
                System.out.println(" 性别:"+sex);
            }
            System.out.println("------------------------------------------");
            System.out.println("更改第五的电话数据");
            sql=con.prepareStatement("update info set tel"+"=? where id =105");
            sql.setString(1, "12345678901");
            sql.executeUpdate();
            System.out.println("------------------------------------------");
            System.out.println("查询男性的信息");
            sql=con.prepareStatement("select * from info where "+"sex = '男'");
            res=sql.executeQuery();
            while (res.next()){
                String  id=res.getString("id");
                String name =res.getString("name");
                String tel =res.getString("tel");
                String sex =res.getString("sex");
                System.out.print("学号:"+id);
                System.out.print(" 姓名:"+name);
                System.out.print(" 电话号码:"+tel);
                System.out.println(" 性别:"+sex);
            }
            System.out.println("------------------------------------------");
            System.out.println("更改第10的姓名");
            sql=con.prepareStatement("update info set name"+"=? where id =110");
            sql.setString(1, "大帅哥");
            sql.executeUpdate();
            System.out.println("------------------------------------------");
            System.out.println("查询182开头的信息");
            sql=con.prepareStatement("select * from info where "+"tel like '182%'");
            res=sql.executeQuery();
            while (res.next()){
                String  id=res.getString("id");
                String name =res.getString("name");
                String tel =res.getString("tel");
                String sex =res.getString("sex");
                System.out.print("学号:"+id);
                System.out.print(" 姓名:"+name);
                System.out.print(" 电话号码:"+tel);
                System.out.println(" 性别:"+sex);
            }
            System.out.println("------------------------------------------");
            System.out.println("删除第3个数据");
            sql.executeUpdate("delete from info where id =103");
            
            
            System.out.println("------------------------------------------");
            System.out.println("查询女性的信息");
            sql=con.prepareStatement("select * from info where "+"sex = '女'");
            res=sql.executeQuery();
            while (res.next()){
                String  id=res.getString("id");
                String name =res.getString("name");
                String tel =res.getString("tel");
                String sex =res.getString("sex");
                System.out.print("学号:"+id);
                System.out.print(" 姓名:"+name);
                System.out.print(" 电话号码:"+tel);
                System.out.println(" 性别:"+sex);
            }
            System.out.println("------------------------------------------");
            sql=con.prepareStatement("select*from info");
            res=sql.executeQuery();
            System.out.println("查询修改后的数据:");
            while (res.next()){
                String  id=res.getString("id");
                String name =res.getString("name");
                String tel =res.getString("tel");
                String sex =res.getString("sex");
                System.out.print("学号:"+id);
                System.out.print(" 姓名:"+name);
                System.out.print(" 电话号码:"+tel);
                System.out.println(" 性别:"+sex);
            }
            
        }catch (Exception e){
            e.printStackTrace();    
        }finally{
            if(res !=null){
                try{
                    res.close();
                }catch (SQLException e){
                    e.printStackTrace();
                }
            }
 
            if(con !=null){
                try{
                    con.close();
                }catch (SQLException e){
                    e.printStackTrace();
                }
            }
            if(sql !=null){
                try{
                     sql.close();
                }catch (SQLException e){
                    e.printStackTrace();
                }
            }
  
        }        
    }         
    }

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值