帮我写个仓库入库Java代码
时间: 2023-07-10 07:09:05 浏览: 61
好的,以下是一个简单的 Java 代码示例,用于将数据存储到仓库中:
```java
import java.sql.*;
public class Warehouse {
static final String JDBC_DRIVER = "com.mysql.jdbc.Driver";
static final String DB_URL = "jdbc:mysql://localhost/EMP";
static final String USER = "username";
static final String PASS = "password";
public static void main(String[] args) {
Connection conn = null;
Statement stmt = null;
try{
Class.forName("com.mysql.jdbc.Driver");
System.out.println("Connecting to database...");
conn = DriverManager.getConnection(DB_URL,USER,PASS);
System.out.println("Creating statement...");
stmt = conn.createStatement();
String sql = "INSERT INTO Employees (id, first_name, last_name, age) " +
"VALUES (101, 'John', 'Smith', 28)";
stmt.executeUpdate(sql);
System.out.println("Data inserted successfully...");
stmt.close();
conn.close();
}catch(SQLException se){
se.printStackTrace();
}catch(Exception e){
e.printStackTrace();
}finally{
try{
if(stmt!=null)
stmt.close();
}catch(SQLException se2){
}
try{
if(conn!=null)
conn.close();
}catch(SQLException se){
se.printStackTrace();
}
}
}
}
```
这个代码示例使用 JDBC 驱动连接到 MySQL 数据库,并将一条新的数据插入到名为 "Employees" 的表中。你需要将 `DB_URL`、`USER` 和 `PASS` 替换为你自己的数据库连接信息,以及根据你的表结构修改 SQL 语句。