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()) { // String number = rs.getString("number"); // double height = rs.getDouble("height"); // Date birth = rs.getDate("birthday"); // String name = rs.getString("name"); // System.out.println(number + "," + name + "," + birth + "," + height); 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 08:11:17 浏览: 24
这段代码中存在一些问题,可能导致无法正确输出结果。首先,代码中的 SQL 语句是查询 message 表,但是在代码注释中提到的字段(number、height、birthday、name)并不属于 message 表,因此需要确认查询的表名和列名是否正确。其次,代码中的输出语句只打印了部分字段,如果需要输出所有字段,需要把注释掉的部分代码恢复。最后,如果无法输出结果,可能是数据库连接出现问题,需要检查数据库连接的用户名、密码、数据库名、表名等信息是否正确。可以尝试打印出异常信息,以便更好地定位问题。
相关问题
修正代码package text5_2; import java.awt.*; import java.awt.event.*; import javax.swing.*; import javax.swing.table.DefaultTableModel; public class table2 extends JFrame implements ActionListener{ public static void main(String[] args) { new table();
根据代码中的类名,应该将 `new table()` 改为 `new table2()`,即将 `table` 改为 `table2`。如下所示:
```
package text5_2;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.table.DefaultTableModel;
public class table2 extends JFrame implements ActionListener {
public static void main(String[] args) {
new table2();
}
}
```
package day0608.utils; import com.alibaba.druid.pool.DruidDataSource; import com.alibaba.druid.pool.DruidDataSourceFactory; import javax.sql.DataSource; import java.io.IOException; import java.io.InputStream; import java.util.Properties; /** * 连接池工具类
这是一个连接池工具类的代码,它使用阿里巴巴的Druid连接池来实现数据库连接的复用,避免频繁地创建和关闭数据库连接。通过读取配置文件中的数据库连接参数,创建Druid数据源,并通过数据源获取数据库连接。在使用完成后,需要将连接关闭并归还给连接池。以下是代码的详细解释:
1. 导入Druid相关的类和接口
```java
import com.alibaba.druid.pool.DruidDataSource;
import com.alibaba.druid.pool.DruidDataSourceFactory;
import javax.sql.DataSource;
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;
```
2. 创建连接池工具类
```java
public class ConnectionPoolUtil {
private static DataSource dataSource;
static {
try {
// 读取配置文件
InputStream inputStream = ConnectionPoolUtil.class.getClassLoader().getResourceAsStream("druid.properties");
Properties properties = new Properties();
properties.load(inputStream);
// 创建Druid数据源
dataSource = DruidDataSourceFactory.createDataSource(properties);
} catch (Exception e) {
e.printStackTrace();
}
}
// 获取数据库连接
public static Connection getConnection() throws SQLException {
return dataSource.getConnection();
}
// 关闭数据库连接
public static void closeConnection(Connection conn, PreparedStatement pstmt, ResultSet rs) {
try {
if (rs != null) {
rs.close();
}
if (pstmt != null) {
pstmt.close();
}
if (conn != null) {
conn.close();
}
} catch (SQLException e) {
e.printStackTrace();
}
}
}
```
3. 使用连接池工具类
```java
public class MyDao {
public List<User> getAllUsers() {
Connection conn = null;
PreparedStatement pstmt = null;
ResultSet rs = null;
List<User> userList = new ArrayList<>();
try {
// 获取数据库连接
conn = ConnectionPoolUtil.getConnection();
pstmt = conn.prepareStatement("SELECT * FROM user");
rs = pstmt.executeQuery();
// 处理查询结果
while (rs.next()) {
User user = new User();
user.setId(rs.getInt("id"));
user.setName(rs.getString("name"));
user.setAge(rs.getInt("age"));
userList.add(user);
}
} catch (SQLException e) {
e.printStackTrace();
} finally {
// 关闭数据库连接
ConnectionPoolUtil.closeConnection(conn, pstmt, rs);
}
return userList;
}
}
```
以上是连接池工具类的基本实现,可以在多线程环境下安全地使用。同时,Druid还提供了很多其他的功能,例如监控、防御SQL注入攻击等,可以根据实际需求进行配置和使用。