zoukankan      html  css  js  c++  java
  • 课堂实践6-7

    实践1
    image

    实践2

    public class MessageDAO {
        public static String url;
        private String user;
        private String passwd;
    
        public MessageDAO(String url, String user, String passwd) {
            this.url = url;
            this.user = user;
            this.passwd = passwd;
        }
    
        public void add(week12.Message message) {
            try(Connection conn = DriverManager.getConnection(url, user, passwd);
                Statement statement = conn.createStatement()) {
                String sql = String.format(
                        "INSERT INTO t_message(name, CountryCode, Distridt,Population) VALUES ('%s', '%s', '%s', '%s')",
                        message.getName(), message.getCountryCode(), message.getDistridt(),message.getPopulation());
                statement.executeUpdate(sql);
            } catch(SQLException ex) {
                throw new RuntimeException(ex);
            }
        }
        public  static week12.Message toMessage(ResultSet result) throws SQLException {
            week12.Message message = new week12.Message();
            message.setId(result.getLong(1));
            message.setName(result.getString(2));
            message.setCountryCode(result.getString(3));
            message.setDistridt(result.getString(4));
            message.setPopulation(result.getString(4));
            return message;
        }
    
        public static void main(String[] args)    throws ClassNotFoundException, SQLException {
            Class.forName("com.mysql.jdbc.Driver");
            String jdbcUrl = "jdbc:mysql://localhost:3306/world";
            String user = "root";
            String passwd = "root";
            try (Connection conn =
                         DriverManager.getConnection(jdbcUrl, user, passwd)) {
                System.out.printf("已%s数据库连接%n", conn.isClosed() ? "关闭" : "打开");
                try (Statement statement = conn.createStatement()) {
                    ResultSet result =
                            statement.executeQuery("SELECT * FROM `city` WHERE Population>5000000");
                    System.out.println("ID" + "	" + "	" + "Name" + "	" + "	" + "CountryCode" + "	" + "	" + "Distridt" + "	" + "	" + "Population");
                    int count = 0;
                    while (result.next()) {
                        System.out.print(result.getInt(1) + "	"+"	");
                        System.out.print(result.getString(2) + "	"+"	");
                        System.out.print(result.getString(3) + "	"+"	");
                        System.out.print(result.getString(4) + "	"+"	");
                        System.out.print(result.getString(5) + "	"+"	");
                        System.out.println();
                    }
                } catch (SQLException ex) {
                    throw new RuntimeException(ex);
                }
            }
        }}
    
    

    image
    实践三

    import java.sql.*;
    import java.util.*;
    
    public class MessageDAO {
        public static String url;
        private String user;
        private String passwd;
    
        public MessageDAO(String url, String user, String passwd) {
            this.url = url;
            this.user = user;
            this.passwd = passwd;
        }
    
        public void add(Message message) {
            try(Connection conn = DriverManager.getConnection(url, user, passwd);
                Statement statement = conn.createStatement()) {
                String sql = String.format(
                        "INSERT INTO t_message(name, CountryCode, Distridt,Population) VALUES ('%s', '%s', '%s', '%s')",
                        message.getName(), message.getCountryCode(), message.getDistridt(),message.getPopulation());
                statement.executeUpdate(sql);
            } catch(SQLException ex) {
                throw new RuntimeException(ex);
            }
        }
        public  static Message toMessage(ResultSet result) throws SQLException {
            Message message = new Message();
            message.setId(result.getLong(1));
            message.setName(result.getString(2));
            message.setCountryCode(result.getString(3));
            message.setDistridt(result.getString(4));
            message.setPopulation(result.getString(4));
            return message;
        }
    
        public static void main(String[] args)    throws ClassNotFoundException, SQLException {
            Class.forName("com.mysql.jdbc.Driver");
            String jdbcUrl = "jdbc:mysql://localhost:3306/world";
            String user = "root";
            String passwd = "root";
            try (Connection conn =
                         DriverManager.getConnection(jdbcUrl, user, passwd)) {
                System.out.printf("已%s数据库连接%n", conn.isClosed() ? "关闭" : "打开");
                try (Statement statement = conn.createStatement()) {
                    ResultSet result =
                            statement.executeQuery("SELECT SUM( Population ) FROM  `city` WHERE District =  'Tokyo'");
                    System.out.println("ID" + "	" + "	" + "Name" + "	" + "	" + "CountryCode" + "	" + "	" + "Distridt" + "	" + "	" + "Population");
                    int count = 0;
                    while (result.next()) {
                        System.out.print(result.getString(1) + "	"+"	");
                        System.out.println();
                    }
                } catch (SQLException ex) {
                    throw new RuntimeException(ex);
                }
            }
        }}
    

    image

    实践四

    import java.sql.*;
    import java.util.*;
    
    public class MessageDAO {
        public static String url;
        private String user;
        private String passwd;
    
        public MessageDAO(String url, String user, String passwd) {
            this.url = url;
            this.user = user;
            this.passwd = passwd;
        }
    
        public void add(Message message) {
            try(Connection conn = DriverManager.getConnection(url, user, passwd);
                Statement statement = conn.createStatement()) {
                String sql = String.format(
                        "INSERT INTO t_message(LifeExpectancy) VALUES ('%s')",message.getLifeExpectancy()
                        );
                statement.executeUpdate(sql);
            } catch(SQLException ex) {
                throw new RuntimeException(ex);
            }
        }
        public  static Message toMessage(ResultSet result) throws SQLException {
            Message message = new Message();
            message.setLifeExpectancy(result.getString(1));
            return message;
        }
    
        public static void main(String[] args)    throws ClassNotFoundException, SQLException {
            Class.forName("com.mysql.jdbc.Driver");
            String jdbcUrl = "jdbc:mysql://localhost:3306/world";
            String user = "root";
            String passwd = "root";
            try (Connection conn =
                         DriverManager.getConnection(jdbcUrl, user, passwd)) {
                System.out.printf("已%s数据库连接%n", conn.isClosed() ? "关闭" : "打开");
                try (Statement statement = conn.createStatement()) {
                    ResultSet result =
                            statement.executeQuery("SELECT * FROM `country` WHERE 1");
                    int count = 0;
                    Double sum=0.0;
                    while (result.next()) {
                        System.out.print( result.getDouble(8)+ "	"+"	");
                        sum=sum+ result.getDouble(8);
                        System.out.println();
                        count++;
                    }
                    System.out.println(sum/count);
                } catch (SQLException ex) {
                    throw new RuntimeException(ex);
                }
            }
        }}
    
    

    image

  • 相关阅读:
    Visual Studio 2015编译64位MySQL Connector/C++
    html.parser无法完全解析网页之BUG的修正
    Boost-Visual studio 2015环境配置
    Struts2--拦截器和常用标签库
    Struts2---OGNL表达式和值栈的运用
    Struts2---对Servlet的API的访问,结果页面的配置,数据的封装
    Struts2---入门
    spring mvc 文件上传
    ElasticSearch 基本操作
    SpringBoot 项目打包后获取不到resource下资源的解决
  • 原文地址:https://www.cnblogs.com/paypay/p/6957980.html
Copyright © 2011-2022 走看看