写一份关于Java数据库程序设计的实验
时间: 2024-03-15 18:43:38 浏览: 32
好的,以下是一份关于Java数据库程序设计的实验:
实验名称:Java数据库程序设计
实验目的:通过Java程序设计,实现对数据库的增删改查操作,掌握Java数据库编程的基本方法。
实验环境:Java开发工具(Eclipse或者IntelliJ IDEA)、MySQL数据库、MySQL驱动程序。
实验步骤:
1.创建数据库和表格
首先,需要在MySQL中创建一个新的数据库和一个表格,用于存储学生的信息。在MySQL中执行以下SQL语句:
CREATE DATABASE student_db;
USE student_db;
CREATE TABLE student (
id INT PRIMARY KEY AUTO_INCREMENT,
name VARCHAR(20) NOT NULL,
age INT NOT NULL,
gender VARCHAR(10) NOT NULL,
address VARCHAR(100) NOT NULL
);
2.编写Java程序
接下来,需要编写Java程序,实现对数据库的增删改查操作。具体的程序代码如下:
import java.sql.*;
public class Main {
static final String JDBC_DRIVER = "com.mysql.jdbc.Driver";
static final String DB_URL = "jdbc:mysql://localhost/student_db";
static final String USER = "root";
static final String PASS = "password";
public static void main(String[] args) {
Connection conn = null;
Statement stmt = null;
try {
Class.forName("com.mysql.jdbc.Driver");
conn = DriverManager.getConnection(DB_URL, USER, PASS);
stmt = conn.createStatement();
// 插入数据
String sql = "INSERT INTO student (name, age, gender, address) VALUES ('Tom', 18, 'Male', 'Beijing')";
stmt.executeUpdate(sql);
// 查询数据
sql = "SELECT id, name, age, gender, address FROM student";
ResultSet rs = stmt.executeQuery(sql);
while (rs.next()) {
int id = rs.getInt("id");
String name = rs.getString("name");
int age = rs.getInt("age");
String gender = rs.getString("gender");
String address = rs.getString("address");
System.out.print("ID: " + id);
System.out.print(", Name: " + name);
System.out.print(", Age: " + age);
System.out.print(", Gender: " + gender);
System.out.println(", Address: " + address);
}
// 更新数据
sql = "UPDATE student SET address = 'Shanghai' WHERE name = 'Tom'";
stmt.executeUpdate(sql);
// 删除数据
sql = "DELETE FROM student WHERE name = 'Tom'";
stmt.executeUpdate(sql);
rs.close();
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();
}
}
}
}
3.运行程序
最后,将Java程序运行起来,查看程序的输出结果,检查程序是否实现了对数据库的增删改查操作。
实验总结:通过本次实验,我们学习了Java程序设计中与数据库相关的基本知识,掌握了Java中对MySQL数据库进行增删改查操作的方法。在实际应用开发中,数据库的操作是必不可少的,因此本次实验对我们今后的实际开发有很大的帮助。
相关推荐
![](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)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)