1个空间做2个网站吗,wordpress全程ssl,山东恒昆建设工程有限公司网站,中山模板网站建设创建一个Java程序#xff0c;建立与本机mysql服务器上student数据库的连接#xff0c;实现在tb_student学生表上插入一条学生信息#xff1a;学号21540118#xff0c;姓名王五#xff0c;性别男#xff0c;出生日期2003-12-10#xff0c;所在学院5。
使用JDBC连接数据库…创建一个Java程序建立与本机mysql服务器上student数据库的连接实现在tb_student学生表上插入一条学生信息学号21540118姓名王五性别男出生日期2003-12-10所在学院5。
使用JDBC连接数据库后实现数据库插入操作代码如下
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;public class case1 {static final String JDBC_DRIVER com.mysql.cj.jdbc.Driver; static final String DB_URL jdbc:mysql://localhost:3306/student;static final String USER user; // 替换为你的用户名static final String PASS password; // 替换为你的密码public static void main(String[] args) {Connection conn null;PreparedStatement pstmt null;try {// 加载和注册 JDBC 驱动Class.forName(JDBC_DRIVER);// 建立数据库连接conn DriverManager.getConnection(DB_URL, USER, PASS);// SQL 插入语句String sql INSERT INTO tb_student (stu_id, stu_name, 性别, 出生日期, 所在学院) VALUES (?, ?, ?, ?, ?);// 创建 PreparedStatement 对象pstmt conn.prepareStatement(sql);// 设置参数值pstmt.setInt(1, 21540118);pstmt.setString(2, 王五);pstmt.setString(3, 男);pstmt.setDate(4, java.sql.Date.valueOf(2003-12-10));pstmt.setInt(5, 5);int result pstmt.executeUpdate();if (result 0) {System.out.println(学生信息插入成功);}} catch (ClassNotFoundException e) {System.out.println(找不到 JDBC 驱动程序。);e.printStackTrace();} catch (SQLException e) {System.out.println(数据库连接失败。);e.printStackTrace();} finally {try {if (pstmt ! null) pstmt.close();if (conn ! null) conn.close();} catch (SQLException se) {se.printStackTrace();}}}
}