用Java设计目录结构 建表一下表Sno 201215122 201215123 201215125; Sname 刘晨王敏张立 ;Ssex女女男; Sage 19 18 19 ;Sdept CS MA IS
时间: 2023-12-03 13:43:34 浏览: 80
目录结构:
```
src/
└── main/
├── java/
│ ├── model/
│ │ └── Student.java
│ ├── dao/
│ │ └── StudentDao.java
│ └── util/
│ └── DatabaseUtil.java
└── resources/
└── db.properties
```
建表 SQL:
```sql
CREATE TABLE student (
sno VARCHAR(10) PRIMARY KEY,
sname VARCHAR(20) NOT NULL,
ssex VARCHAR(2) NOT NULL,
sage INT NOT NULL,
sdept VARCHAR(20) NOT NULL
);
```
Java 代码:
Student.java
```java
package model;
public class Student {
private String sno;
private String sname;
private String ssex;
private int sage;
private String sdept;
public Student(String sno, String sname, String ssex, int sage, String sdept) {
this.sno = sno;
this.sname = sname;
this.ssex = ssex;
this.sage = sage;
this.sdept = sdept;
}
public String getSno() {
return sno;
}
public void setSno(String sno) {
this.sno = sno;
}
public String getSname() {
return sname;
}
public void setSname(String sname) {
this.sname = sname;
}
public String getSsex() {
return ssex;
}
public void setSsex(String ssex) {
this.ssex = ssex;
}
public int getSage() {
return sage;
}
public void setSage(int sage) {
this.sage = sage;
}
public String getSdept() {
return sdept;
}
public void setSdept(String sdept) {
this.sdept = sdept;
}
}
```
StudentDao.java
```java
package dao;
import model.Student;
import java.sql.*;
import java.util.ArrayList;
import java.util.List;
public class StudentDao {
private String url;
private String username;
private String password;
public StudentDao(String url, String username, String password) {
this.url = url;
this.username = username;
this.password = password;
}
public List<Student> getAllStudents() {
List<Student> students = new ArrayList<>();
try (Connection connection = DriverManager.getConnection(url, username, password);
Statement statement = connection.createStatement();
ResultSet resultSet = statement.executeQuery("SELECT * FROM student")) {
while (resultSet.next()) {
String sno = resultSet.getString("sno");
String sname = resultSet.getString("sname");
String ssex = resultSet.getString("ssex");
int sage = resultSet.getInt("sage");
String sdept = resultSet.getString("sdept");
Student student = new Student(sno, sname, ssex, sage, sdept);
students.add(student);
}
} catch (SQLException e) {
e.printStackTrace();
}
return students;
}
public boolean addStudent(Student student) {
try (Connection connection = DriverManager.getConnection(url, username, password);
PreparedStatement statement = connection.prepareStatement(
"INSERT INTO student (sno, sname, ssex, sage, sdept) VALUES (?, ?, ?, ?, ?)")) {
statement.setString(1, student.getSno());
statement.setString(2, student.getSname());
statement.setString(3, student.getSsex());
statement.setInt(4, student.getSage());
statement.setString(5, student.getSdept());
int rows = statement.executeUpdate();
return rows > 0;
} catch (SQLException e) {
e.printStackTrace();
return false;
}
}
public boolean updateStudent(Student student) {
try (Connection connection = DriverManager.getConnection(url, username, password);
PreparedStatement statement = connection.prepareStatement(
"UPDATE student SET sname=?, ssex=?, sage=?, sdept=? WHERE sno=?")) {
statement.setString(1, student.getSname());
statement.setString(2, student.getSsex());
statement.setInt(3, student.getSage());
statement.setString(4, student.getSdept());
statement.setString(5, student.getSno());
int rows = statement.executeUpdate();
return rows > 0;
} catch (SQLException e) {
e.printStackTrace();
return false;
}
}
public boolean deleteStudent(String sno) {
try (Connection connection = DriverManager.getConnection(url, username, password);
PreparedStatement statement = connection.prepareStatement("DELETE FROM student WHERE sno=?")) {
statement.setString(1, sno);
int rows = statement.executeUpdate();
return rows > 0;
} catch (SQLException e) {
e.printStackTrace();
return false;
}
}
}
```
DatabaseUtil.java
```java
package util;
import java.io.IOException;
import java.io.InputStream;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.Properties;
public class DatabaseUtil {
private static String url;
private static String username;
private static String password;
static {
try (InputStream inputStream = DatabaseUtil.class.getClassLoader().getResourceAsStream("db.properties")) {
Properties properties = new Properties();
properties.load(inputStream);
url = properties.getProperty("url");
username = properties.getProperty("username");
password = properties.getProperty("password");
Class.forName(properties.getProperty("driver"));
} catch (IOException | ClassNotFoundException e) {
e.printStackTrace();
}
}
public static Connection getConnection() throws SQLException {
return DriverManager.getConnection(url, username, password);
}
}
```
db.properties
```
driver=com.mysql.jdbc.Driver
url=jdbc:mysql://localhost:3306/test?useSSL=false
username=root
password=123456
```
阅读全文