MySQL的课堂的实践

MySQL的课堂的实践

基本认识

如今的数据库有几种是主流,分别是:Oracle Database、Informix、SQL Server、PostgreSQL、MySQL等,我们现在学习的MySQL就算是其中之一。MySQL的功能强大,也是我们日后专业课所要面对的重点。在课堂实践上,我们学习了数据库的基础知识,包括数据库的界面介绍以及一些基本操作。

  • 数据表

每一列是一个域,每一行是一个记录。在用数据库语言操作的时候,域和记录是很重要的两个部分。

  • SQL语句

  • SQL操作界面

若我要让数据表只查询语言(Language)和百分比(Percentage),我就可以通过修改SELECT后面的星号,改为"'Language','Percentage'"即可。

若我要看百分比(Percentage)大于5的,我可以通过修改后面WHERE的参数来达到目的

SQL语句小拓展

  • SUM

我想知道该表格中说英语的人的总数,我可以通过SUM来得到总数,再在后面的WHERE中限定语言为英语就好。

-MAX

我想知道表格中说英语最多的国家以及它的百分比,就可以用到MAX来获取最大的单位。

SELECT `CountryCode`,`Language`,MAX(`Percentage`) FROM `countrylanguage` WHERE Language = 'English'

实践项目

实际上,在代码行中同样可以把数据库给用上,课本上有一段统一的代码供我们来使用,根据要求,我们要连上名为world的数据库来进行操作,经过调整,代码大体为:

//*******************************************************************
// DatabaseModfication.java Java Foundations
//
// Demonstrates interaction between a Java program and a database.
//*******************************************************************
import java.sql.*;
public class DatabaseModification
{
    //-----------------------------------------------------------------
// Carries out various CRUD operations after establishing the
// database connection.
//-----------------------------------------------------------------
    public static void main (String args[])
    {
        Connection conn = null;
        try
        {
// Loads the class object for the mysql driver into the DriverManager.
            Class.forName("com.mysql.jdbc.Driver");
// Attempt to establish a connection to the specified database via the
// DriverManager
            conn = DriverManager.getConnection("jdbc:mysql://localhost:3307/" +
                    "world?user=root&password=");
// Check the connection
            if (conn != null)
            {
                System.out.println("We have connected to our database!");
// Create the table and show the table structure
                Statement stmt = conn.createStatement();
// Insert the data into the database and show the values in the table
                Statement stmt2 = conn.createStatement(ResultSet.TYPE_FORWARD_ONLY,
                        ResultSet.CONCUR_UPDATABLE);

                DatabaseModification.showValues(conn);
// Close the database
                conn.close();
            }
        } catch (SQLException ex) {
            System.out.println("SQLException: " + ex.getMessage());
            ex.printStackTrace();
        } catch (Exception ex) {
            System.out.println("Exception: " + ex.getMessage());
            ex.printStackTrace();
        }
    }
    //-----------------------------------------------------------------
// Obtains and displays a ResultSet from the Student table.
//-----------------------------------------------------------------
    public static void showValues(Connection conn)
    {
        try
        {
            Statement stmt = conn.createStatement();
            ResultSet rset = stmt.executeQuery("SELECT * FROM 表名 WHERE 1");
            DatabaseModification.showResults("表名", rset);
        } catch (SQLException ex) {
            System.out.println("SQLException: " + ex.getMessage());
            ex.printStackTrace();
        }
    }
    //-----------------------------------------------------------------
// Displays the structure of the Student table.
//-----------------------------------------------------------------

    //-----------------------------------------------------------------
// Displays the contents of the specified ResultSet.
//-----------------------------------------------------------------
    public static void showResults(String tableName, ResultSet rSet)
    {
        try
        {
            ResultSetMetaData rsmd = rSet.getMetaData();
            int numColumns = rsmd.getColumnCount();
            String resultString = null;
            if (numColumns > 0)
            {
                resultString = "\nTable: " + tableName + "\n" +
                        "=======================================================\n";
                for (int colNum = 1; colNum <= numColumns; colNum++)
                    resultString += rsmd.getColumnLabel(colNum) + " ";
            }
            System.out.println(resultString);
            System.out.println(
                    "=======================================================");
            while (rSet.next())
            {
                resultString = "";
                for (int colNum = 1; colNum <= numColumns; colNum++)
                {
                    String column = rSet.getString(colNum);
                    if (column != null)
                        resultString += column + " ";
                }
                System.out.println(resultString + '\n' +
                        "------------------------------------------------------------");
            }
        } catch (SQLException ex) {
            System.out.println("SQLException: " + ex.getMessage());
            ex.printStackTrace();
        }
    }
}

其中重要的部分就是:

 public static void showValues(Connection conn)
    {
        try
        {
            Statement stmt = conn.createStatement();
            ResultSet rset = stmt.executeQuery("SELECT * FROM 表名 WHERE 1");
            DatabaseModification.showResults("表名", rset);
        } catch (SQLException ex) {
            System.out.println("SQLException: " + ex.getMessage());
            ex.printStackTrace();
        }
    }

修改这一部分关系到你看到的结果。项目的操作也都在这里进行。

  • 项目1:查询world数据库,获得人口超过500万的所有城市的列表。
public static void showValues(Connection conn)
    {
        try
        {
            Statement stmt = conn.createStatement();
            ResultSet rset = stmt.executeQuery("SELECT * FROM city WHERE Population > 5000000");
            DatabaseModification.showResults("city", rset);
        } catch (SQLException ex) {
            System.out.println("SQLException: " + ex.getMessage());
            ex.printStackTrace();
        }
    }

结果截图:

  • 实践项目2:查询world数据库,获得New Jersey州所有城市的总人口数。
public static void showValues(Connection conn)
    {
        try
        {
            Statement stmt = conn.createStatement();
            ResultSet rset = stmt.executeQuery("SELECT SUM(Population) FROM city WHERE District = 'New Jersey'");
            DatabaseModification.showResults("city", rset);
        } catch (SQLException ex) {
            System.out.println("SQLException: " + ex.getMessage());
            ex.printStackTrace();
        }
    }

结果截图:

  • 实践项目3:查询world数据库,查询哪个国家的平均寿命最长
public static void showValues(Connection conn)
    {
        try
        {
            Statement stmt = conn.createStatement();
            ResultSet rset = stmt.executeQuery("SELECT MAX(LifeExpectancy) FROM country WHERE 1");
            DatabaseModification.showResults("country", rset);
        } catch (SQLException ex) {
            System.out.println("SQLException: " + ex.getMessage());
            ex.printStackTrace();
        }
    }

结果截图:

posted @ 2017-06-14 11:14  FunnyOne  阅读(392)  评论(0编辑  收藏  举报