English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
This chapter provides examples of how to sort records in a table using JDBC applications. This will useasc和descKeywords are used to sort records in ascending or descending order. Before executing the following examples, please ensure that you have the following conditions met:
要执行以下示例,您可以将用户名和密码替换为实际的用户名和密码。
您的MySQL或您正在使用的任何数据库均已启动并正在运行。
使用JDBC应用程序创建新数据库需要执行以下步骤-
导入包:要求您包含包含数据库编程所需的JDBC类的软件包。通常,使用 import java.sql.* 就足够了。
注册 JDBC 驱动程序: 要求您初始化驱动程序,以便您可以打开与数据库的通信通道。
建立连接:需要使用 DriverManager.getConnection() 方法来创建一个Connection对象,该对象表示与数据库服务器的物理连接。
执行查询:需要使用Statement类型的对象来构建和提交SQL语句以对表中的记录进行排序。这些查询使用asc和desc子句对数据进行升序和降序排序。
清理环境: 需要显式关闭所有数据库资源,而不是依赖JVM的垃圾回收。
将以下示例复制并粘贴到JDBCExample.java中,编译并运行如下-
//Step1.导入所需的软件包 import java.sql.*; public class JDBCExample { // JDBC驱动程序名称和数据库URL static final String JDBC_DRIVER = "com.mysql.jdbc.Driver"; static final String DB_URL = "jdbc:mysql://localhost/STUDENTS"; // 数据库凭证 static final String USER = "username"; static final String PASS = "password"; public static void main(String[] args) { Connection conn = null; Statement stmt = null; try{ //Step2注册JDBC驱动程序 Class.forName("com.mysql.jdbc.Driver"); //Step3建立连接 System.out.println("连接到所选数据库..."); conn = DriverManager.getConnection(DB_URL, USER, PASS); System.out.println("Database connected successfully..."); //Step4Execute query System.out.println("Creating statement..."); stmt = conn.createStatement(); // Extract records in ascending order by name. System.out.println("Fetching records in ascending order..."); String sql = "SELECT id, first, last, age FROM Registration" + " ORDER BY first ASC"; ResultSet rs = stmt.executeQuery(sql); while(rs.next()){ //Search by column name int id = rs.getInt("id"); int age = rs.getInt("age"); String first = rs.getString("first"); String last = rs.getString("last"); //Display value System.out.print("ID: " + id); System.out.print(", Age: " + age); System.out.print(", First: " + first); System.out.println(", Last: " + last); } // Extract records in descending order by name. System.out.println("Fetching records in descending order..."); sql = "SELECT id, first, last, age FROM Registration" + " ORDER BY first DESC"; rs = stmt.executeQuery(sql); while(rs.next()){ //Search by column name int id = rs.getInt("id"); int age = rs.getInt("age"); String first = rs.getString("first"); String last = rs.getString("last"); //Display value System.out.print("ID: " + id); System.out.print(", Age: " + age); System.out.print(", First: " + first); System.out.println(", Last: " + last); } rs.close(); }catch(SQLException se){ //Handle JDBC errors se.printStackTrace(); catch(Exception e){ //Handle the error of Class.forName e.printStackTrace(); } //Finally block to stop resources from closing try{ if(stmt!=null) conn.close(); }catch(SQLException se){ }// Nothing to do try{ if(conn!=null) conn.close(); }catch(SQLException se){ se.printStackTrace(); } } System.out.println("Goodbye!"); }//End main }//End JDBCExample
Now, let's compile the above example as follows:
C:\>javac JDBCExample.java C:\>
at runtimeJDBCExampleIt will produce the following result-
C:\>java JDBCExample Connecting to a selected database... Database connected successfully... Creating statement... Fetching records in ascending order... ID: 103, Age: 28, First: Sumit, Last: Mittal ID: 102, Age: 30, First: Zaid, Last: Khan ID: 100, Age: 30, First: Zara, Last: Ali Fetching records in descending order... ID: 100, Age: 30, First: Zara, Last: Ali ID: 102, Age: 30, First: Zaid, Last: Khan ID: 103, Age: 28, First: Sumit, Last: Mittal Goodbye! C:\>