CREATE DATABASE mydatabase; USE mydatabase; CREATE TABLE customers ( customerID varchar(8) primary key, name varchar(40) default NULL, phone varchar(16) default NULL ); INSERT INTO customers VALUES ('ADDIFK01','Frank Addinsell', '(718) 555-3911'); INSERT INTO customers VALUES ('ALBIBB01','Bob Albinoni', '(213) 555-7566');在这段代码中package homework7_二_1_20221548; import java.sql.*; import javax.swing.JOptionPane; public class Demo { public static void main(String args[]) { String url = "jdbc:mysql://127.0.0.1:3306/student"; String user = "root", pwd = ""; Connection con; Statement sql; ResultSet rs; try { Class.forName("com.mysql.jdbc.Driver"); } catch (ClassNotFoundException e) { System.out.println("test" + e); } try { con = DriverManager.getConnection(url, user, pwd); sql = con.createStatement(); String sqlStatement; sqlStatement = "SELECT * From message order by height desc"; rs = sql.executeQuery(sqlStatement); while (rs.next()) { double customerID = rs.getDouble("customerID"); String name = rs.getString("name"); double phone = rs.getDouble("phone"); System.out.println(customerID + " " + name + " " + phone); } con.close(); } catch (SQLException e) { System.out.println("数据库操作失败" + e); } } }为什么输不出结果?

时间: 2024-02-14 13:10:56 浏览: 19
这段代码中的 SQL 语句是查询 message 表,但是在数据库中并没有创建名为 message 的表,而是创建了一个名为 customers 的表。因此,需要将 SQL 语句修改为查询 customers 表,同时查询的列名也需要修改为该表中的列名。另外,由于 phone 列的数据类型是 varchar,因此无法直接使用 getDouble() 方法获取,需要改为使用 getString() 方法获取,并且在输出时需要将字符串转换为 double 类型。修改后的代码如下: ``` package homework7_二_1_20221548; import java.sql.*; import javax.swing.JOptionPane; public class Demo { public static void main(String args[]) { String url = "jdbc:mysql://127.0.0.1:3306/mydatabase"; String user = "root", pwd = ""; Connection con; Statement sql; ResultSet rs; try { Class.forName("com.mysql.jdbc.Driver"); } catch (ClassNotFoundException e) { System.out.println("test" + e); } try { con = DriverManager.getConnection(url, user, pwd); sql = con.createStatement(); String sqlStatement; sqlStatement = "SELECT * From customers order by name"; rs = sql.executeQuery(sqlStatement); while (rs.next()) { String customerID = rs.getString("customerID"); String name = rs.getString("name"); String phone = rs.getString("phone"); System.out.println(customerID + " " + name + " " + Double.parseDouble(phone)); } con.close(); } catch (SQLException e) { System.out.println("数据库操作失败" + e); } } } ``` 注意,这段代码中的查询语句按照客户姓名升序排列,如果需要按照其他列排序,可以修改 SQL 语句中的 "order by" 子句。

相关推荐

根据这些信息提供创建数据库和创建数据表的Oracle代码:CUSTOMER: stores customer information such as name, address, phone number, email, and loyalty program status. This table has the primary key of CustomerID. • ORDER: stores information about each order such as the order date, order status, and total cost. This table has a primary key of OrderID and a foreign key to the Customer table. • ORDER_DETAILS: stores details about each item in an order such as the product name, price, quantity, and subtotal. This table has a primary key of OrderDetailID and foreign keys to the Order and PRODUCT tables. • PRODUCT: stores information about each product such as the product name, description, price, and category. This table has the primary key of ProductID. • STORE: stores information about each store such as the store name, location, and hours of operation. This table has the primary key of StoreID. • INVENTORY: stores information about the inventory for each product in each store such as the quantity on hand and the reorder point. This table has a composite primary key of ProductID and StoreID. Group Assignment • TRANSACTION: stores information about each transaction such as the transaction date, transaction type, and total amount. This table has a primary key of TransactionID and a foreign key to the Customer table. • TRANSACTION_DETAILS: stores details about each item in a transaction such as the product name, price, quantity, and subtotal. This table has the primary key of TransactionDetailID and foreign keys to the TRANSACTION and PRODUCT tables. • EMPLOYEE: stores employee information such as name, address, phone number, email, and position. This table has the primary key of EmployeeID. • SALARY: stores information about the salary for each employee such as the salary amount, start date, and end date. This table has a composite primary key of EmployeeID and StartDate. • SHIFT: stores information about the shift for each employee such as the start time, end time, and store location. This table has a primary key of ShiftID and foreign keys to the Employee and STORE tables.

最新推荐

recommend-type

需要系数法负荷计算软件.zip

需要系数法负荷计算软件
recommend-type

kettle插件-MongoDB Delete2.0

kettle 用于(按条件)删除MongoDB集合数据的插件。 只需要放入kettle 客户端安装目录的 plugins目录下,然后重启kettle客户端即可。
recommend-type

建筑电气负荷计算小软件.zip

建筑电气负荷计算小软件
recommend-type

电线选型、线管选型小型计算软件.zip

电线选型、线管选型小型计算软件
recommend-type

有限网卡驱动包,直接上传下载就行

啦啦啦啦
recommend-type

zigbee-cluster-library-specification

最新的zigbee-cluster-library-specification说明文档。
recommend-type

管理建模和仿真的文件

管理Boualem Benatallah引用此版本:布阿利姆·贝纳塔拉。管理建模和仿真。约瑟夫-傅立叶大学-格勒诺布尔第一大学,1996年。法语。NNT:电话:00345357HAL ID:电话:00345357https://theses.hal.science/tel-003453572008年12月9日提交HAL是一个多学科的开放存取档案馆,用于存放和传播科学研究论文,无论它们是否被公开。论文可以来自法国或国外的教学和研究机构,也可以来自公共或私人研究中心。L’archive ouverte pluridisciplinaire
recommend-type

实现实时数据湖架构:Kafka与Hive集成

![实现实时数据湖架构:Kafka与Hive集成](https://img-blog.csdnimg.cn/img_convert/10eb2e6972b3b6086286fc64c0b3ee41.jpeg) # 1. 实时数据湖架构概述** 实时数据湖是一种现代数据管理架构,它允许企业以低延迟的方式收集、存储和处理大量数据。与传统数据仓库不同,实时数据湖不依赖于预先定义的模式,而是采用灵活的架构,可以处理各种数据类型和格式。这种架构为企业提供了以下优势: - **实时洞察:**实时数据湖允许企业访问最新的数据,从而做出更明智的决策。 - **数据民主化:**实时数据湖使各种利益相关者都可
recommend-type

用matlab绘制高斯色噪声情况下的频率估计CRLB,其中w(n)是零均值高斯色噪声,w(n)=0.8*w(n-1)+e(n),e(n)服从零均值方差为se的高斯分布

以下是用matlab绘制高斯色噪声情况下频率估计CRLB的代码: ```matlab % 参数设置 N = 100; % 信号长度 se = 0.5; % 噪声方差 w = zeros(N,1); % 高斯色噪声 w(1) = randn(1)*sqrt(se); for n = 2:N w(n) = 0.8*w(n-1) + randn(1)*sqrt(se); end % 计算频率估计CRLB fs = 1; % 采样频率 df = 0.01; % 频率分辨率 f = 0:df:fs/2; % 频率范围 M = length(f); CRLB = zeros(M,1); for
recommend-type

JSBSim Reference Manual

JSBSim参考手册,其中包含JSBSim简介,JSBSim配置文件xml的编写语法,编程手册以及一些应用实例等。其中有部分内容还没有写完,估计有生之年很难看到完整版了,但是内容还是很有参考价值的。