import java.sql.*;public class BookManagementSystem { private Connection connection; private Statement statement; private ResultSet resultSet; public BookManagementSystem() { try { Class.forName("com.mysql.jdbc.Driver"); connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/library", "root", "password"); statement = connection.createStatement(); } catch (Exception e) { e.printStackTrace(); } } public void query(String isbn) { try { resultSet = statement.executeQuery("SELECT * FROM books WHERE isbn='" + isbn + "'"); if (resultSet.next()) { System.out.println(resultSet.getString("title") + ", 作者:" + resultSet.getString("author") + ", 价格:" + resultSet.getDouble("price") + ", 出版社:" + resultSet.getString("publisher")); } else { System.out.println("没有找到书籍"); } } catch (Exception e) { e.printStackTrace(); } } public void input(String isbn, String title, String author, double price, String publisher) { try { statement.executeUpdate("INSERT INTO books VALUES('" + isbn + "', '" + title + "', '" + author + "', " + price + ", '" + publisher + "')"); System.out.println("书籍添加成功,图书信息为:" + isbn + " " + title + ", 作者:" + author + ", 价格:" + price + ", 出版社:" + publisher); } catch (Exception e) { e.printStackTrace(); } } public static void main(String[] args) { BookManagementSystem bms = new BookManagementSystem(); if (args[0].equals("query")) { bms.query(args[1]); } else if (args[0].equals("input")) { bms.input(args[1], args[2], args[3], Double.parseDouble(args[4]), args[5]); } }}
时间: 2024-04-01 12:31:54 浏览: 18
这段代码是一个Java程序,实现了一个简单的图书管理系统。它连接到一个MySQL数据库,包含两个方法:query和input。query方法用来查询指定ISBN号的书籍信息,input方法用来向数据库中添加新的书籍信息。程序的启动方式是从命令行接收参数,如果第一个参数是"query",则调用query方法查询书籍信息;如果第一个参数是"input",则调用input方法添加新的书籍信息。
相关问题
package connectmysql; import java.sql.Connection; import java.sql.DriverManager; import java.sql.ResultSet; import java.sql.Statement; import java.util.Scanner; import java.sql.SQLException; import java.sql.Statement; public class text { private final static String url ="jdbc:mysql://localhost:3306/mytextsql?severTimezone=GMT"; private final static String username="root"; private final static String password="yxh9497"; public static void main(String args[]) { try { Class.forName("com.mysql.cj.jdbc.Driver"); }catch(Exception e) { e.printStackTrace(); } try { Connection connect=DriverManager.getConnection(url,username,password); Statement stmt=connect.createStatement(); Scanner a=new Scanner(System.in); System.out.print("删除的是:"+'\n'); String a4=a.next(); String sql="delete from user where name="+a4; int i=stmt.executeUpdate(sql); if(i > 0){ System.out.println("删除成功"); } }catch(Exception e) { e.printStackTrace(); } } }
看起来您的代码是在使用Java连接MySQL数据库,并尝试删除一个名为`user`的表中的行。但是,这段代码存在一些潜在的问题。
首先,您缺少`import java.sql.SQLException`语句,因此编译器可能会报告找不到该类的错误。因此,您需要在代码顶部添加此导入语句。
其次,您在SQL语句中使用了字符串变量`a4`,但没有将其用引号括起来。这可能导致SQL语句无法正确解析,从而导致删除操作失败。因此,您需要将`a4`变量用引号括起来,以确保SQL语句正确。
最后,您使用了两个`Statement`对象,这应该会导致编译错误。您需要删除其中一个`import java.sql.Statement`语句,并将`stmt`对象的声明放置在`try`块中。
下面是修改后的代码:
```
package connectmysql;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.Scanner;
import java.sql.SQLException;
public class text {
private final static String url ="jdbc:mysql://localhost:3306/mytextsql?severTimezone=GMT";
private final static String username="root";
private final static String password="yxh9497";
public static void main(String args[]) {
try {
Class.forName("com.mysql.cj.jdbc.Driver");
}catch(Exception e) {
e.printStackTrace();
}
try {
Connection connect=DriverManager.getConnection(url,username,password);
Statement stmt=connect.createStatement();
Scanner a=new Scanner(System.in);
System.out.print("删除的是:"+'\n');
String a4=a.next();
String sql="delete from user where name='"+a4+"'";
int i=stmt.executeUpdate(sql);
if(i > 0){
System.out.println("删除成功");
}
}catch(SQLException e) {
e.printStackTrace();
}
}
}
```
希望这可以帮助您解决问题。
package db; import java.sql.*; public class Shujuku { private static final String URL = "jdbc:mysql://localhost:3306/?db_student"; private static final String USERNAME = "root"; private static final String PASSWORD = "123456"; private static final String DRIVER_CLASS = "com.mysql.jdbc.Driver"; public void kk(){ try{ Class.forName(DRIVER_CLASS); Connection conn = DriverManager.getConnection(URL, USERNAME, PASSWORD); Statement statement=conn.createStatement(); System.out.println("已经成功连接到数据库!"); } catch (Exception e) { throw new RuntimeException(e); } }}优化
优化点:
1. 数据库连接信息应该封装到配置文件中,以便于修改和维护。
2. 数据库连接应该使用连接池进行管理,避免频繁创建和销毁连接对象。
3. 应该使用 PreparedStatement 对象执行 SQL 语句,避免 SQL 注入攻击。
4. 避免在方法中抛出 RuntimeException 异常,应该捕获异常并进行处理。
下面是优化后的代码:
```
package db;
import java.io.InputStream;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Properties;
import javax.sql.DataSource;
import com.alibaba.druid.pool.DruidDataSourceFactory;
public class DatabaseUtil {
private static DataSource ds;
static {
try {
InputStream is = DatabaseUtil.class.getClassLoader().getResourceAsStream("db.properties");
Properties props = new Properties();
props.load(is);
ds = DruidDataSourceFactory.createDataSource(props);
} catch (Exception e) {
throw new ExceptionInInitializerError(e);
}
}
public static Connection getConnection() throws SQLException {
return ds.getConnection();
}
public static void close(Connection conn, PreparedStatement ps, ResultSet rs) {
try {
if (rs != null) {
rs.close();
}
if (ps != null) {
ps.close();
}
if (conn != null) {
conn.close();
}
} catch (SQLException e) {
e.printStackTrace();
}
}
}
```
```
package db;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
public class Shujuku {
public void kk() {
Connection conn = null;
PreparedStatement ps = null;
ResultSet rs = null;
try {
conn = DatabaseUtil.getConnection();
ps = conn.prepareStatement("select * from student");
rs = ps.executeQuery();
while (rs.next()) {
System.out.println(rs.getInt("id") + " " + rs.getString("name") + " " + rs.getInt("age"));
}
} catch (SQLException e) {
e.printStackTrace();
} finally {
DatabaseUtil.close(conn, ps, rs);
}
}
}
```
这里使用了阿里巴巴的 Druid 数据库连接池,可以在 Maven 中添加以下依赖项来获取:
```
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.2.4</version>
</dependency>
```
需要在类路径中添加 `db.properties` 配置文件,内容如下:
```
driverClassName=com.mysql.jdbc.Driver
url=jdbc:mysql://localhost:3306/db_student
username=root
password=123456
initialSize=5
minIdle=5
maxActive=20
```
其中 `initialSize` 表示初始化连接数,`minIdle` 表示最小空闲连接数,`maxActive` 表示最大连接数。
相关推荐
![docx](https://img-home.csdnimg.cn/images/20210720083331.png)
![doc](https://img-home.csdnimg.cn/images/20210720083327.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)