第十六周 数据库课堂实践 20162305
有关本次实践
- 之前的课程实践当中,我们已经接触过数据库,本次课堂实践的内容,是把数据库和Java中的代码结合起来,并在IDEA中表现出来。由于课上我的电脑临时出了一点状况,有些实践没有很好地完成,所以写一篇博客弥补一下,也在重新实践一下这部分的内容。
实验一
- 参考教材相关代码,提交能连接到world的截图。
- 实现这个功能还算简单,只需在我们之前写过的DatabaseConnector这个程序中,将getConnection后的代码修改成
conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/world" ,"root", "");
即可。
(实验一截图)
实验二
- 查询world数据库,获得人口超过500万的所有城市的列表。
- 我们已经将java程序和数据库连接上了,在数据库界面中,我们可以应用SQL进行查询相关内容,在java代码中,我们可以将相应的SQL语句加在java代码中。
Statement stmt = conn.createStatement();
ResultSet rset = stmt.executeQuery("SELECT * FROM `city` WHERE Population > 5000000");
运行这个代码,就可以实现要求的功能。
(实验二截图)
实验三
- 查询world数据库,获得New Jersey州所有城市的总人口数。
- 之前的代码没有问题,要想实现这一功能,只需要修改相关的SQL语句就可以了。修改为
SELECT sum(Population) FROM `city` WHERE District = 'New Jersey'
运行即可。
(实验三截图)
实验四
- 查询world数据库,查询哪个国家的平均寿命最长。
- 同理修改SQL语句,要想知道最长的平均寿命,使用语句
SELECT max(LifeExpectancy),Name FROM `country`
运行即可。
(实验四截图)
实验五
- 研究学习如何实现两个表的连接(Join),查询world数据库,列出亚洲所有国家首都的人口数。
- 我不是很清楚Join这个功能,所以在网上找了一篇介绍相关内容的博客学习
- Join语句
- 学习以后修改了SQL代码,得到实验结果。
(实验五截图)
实验成果代码
- World
import static java.lang.System.out;
import java.sql.*;
public class World {
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 = "";
try(Connection conn =
DriverManager.getConnection(jdbcUrl, user, passwd)) {
out.printf("已%s数据库连接了%n",
conn.isClosed() ? "关闭" : "打开");
}
}
}
- DatabaseConnector
import java.sql.*;
//*******************************************************************
// DatabaseConnector.java Java Foundations
//
// Demonstrates the establishment of a JDBC connector.
//*******************************************************************
public class DatabaseConnector
{
//-----------------------------------------------------------------
// Establishes the connection to the database and prints an
// appropriate confirmation message.
//-----------------------------------------------------------------
public static void main (String args[])
{
try
{
Connection conn = null;
// 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:3306/world" ,"root", "");
if (conn != null)
{
System.out.println("We have connected to our 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();
}
}
}
- DatabaseModfication
import java.sql.*;
public class DatabaseModfication {
//----------------------------------------------------------------
// 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:3306/world","root",
"");
// Check the connection
if (conn != null) {
System.out.println("We have connected to our database!");
DatabaseModfication.showValues(conn);
}
} 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 country.Name,city.Name,city.Population FROM `city` JOIN `country` on country.Capital = city.ID and country.Continent = 'Asia'");
DatabaseModfication.showResults("city", rset);
} catch (SQLException ex) {
System.out.println("SQLException: " + ex.getMessage());
ex.printStackTrace();
}
}
//----------------------------------------------------------------
// Displays the structure of the Student table.
//----------------------------------------------------------------
public static void showColumns(Connection conn) {
try {
Statement stmt = conn.createStatement();
ResultSet rset = stmt.executeQuery("SHOW COLUMNS FROM Student");
DatabaseModfication.showResults("Student", rset);
} catch (SQLException ex) {
System.out.println("SQLException: " + ex.getMessage());
ex.printStackTrace();
}
}
//----------------------------------------------------------------
// 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 = "
Table: " + tableName + "
" +
"=======================================================
";
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 + '
' +
"------------------------------------------------------------");
}
} catch (SQLException ex) {
System.out.println("SQLException: " + ex.getMessage());
ex.printStackTrace();
}
}
}
总结
- 本次实验以之前的数据库内容为基础,将Java和数据库结合起来,不仅需要了解Java的知识,也需要学习SQL语句来完善功能。需要学习的还有很多,需要我们一起努力。