按照老师的要求,掌握数据库的连接,增删查改
大作业展示
思路:
建个表格
实现增删查改
完事
<%@ page contentType="text/html; charset=UTF-8" import="java.sql.*" %>
成绩
学生成绩表
添加信息
学号
姓名
科目
成绩
删除
更新
<%
}
%>
<%
String JDBC_DRIVER = "com.mysql.jdbc.Driver";
String DB_URL = "jdbc:mysql://localhost:3306/test";
String USER = "root";
String PASS = "password";
%>
<%@ page contentType="text/html; charset=UTF-8" import="java.sql.*" %>
<%
String id = request.getParameter("id");
String name = request.getParameter("name");
String course = request.getParameter("course");
String score = request.getParameter("score");
String JDBC_DRIVER = "com.mysql.jdbc.Driver";
String DB_URL = "jdbc:mysql://localhost:3306/test";
String USER = "root";
String PASS = "password";
try {
Class.forName(JDBC_DRIVER);
Connection conn = DriverManager.getConnection(DB_URL, USER, PASS);
String sql = "INSERT INTO test(id,name,course,score) value(?,?,?,?)";
PreparedStatement psmt = conn.prepareStatement(sql);
psmt.setInt(1, Integer.parseInt(id));
psmt.setString(2, name);
psmt.setString(3, course);
psmt.setInt(4, Integer.parseInt(score));
int rs = psmt.executeUpdate();
conn.close();
response.sendRedirect("index.jsp");
} catch (ClassNotFoundException | SQLException e) {
e.printStackTrace();
}
%>
<%@ page contentType="text/html; charset=UTF-8" import="java.sql.*" %>
<%
String JDBC_DRIVER = "com.mysql.jdbc.Driver";
String DB_URL = "jdbc:mysql://localhost:3306/test";
String USER = "root";
String PASS = "password";
String sql = "SELECT max(id) FROM test";
try {
Class.forName(JDBC_DRIVER);
Connection conn = DriverManager.getConnection(DB_URL, USER, PASS);
PreparedStatement psmt = conn.prepareStatement(sql);
ResultSet rs = psmt.executeQuery();
rs.next();
int id = rs.getInt(1) + 1;
conn.close();
%>
插入页面
insert message
<%
} catch (ClassNotFoundException | SQLException e) {
e.printStackTrace();
}
%>
<%@ page contentType="text/html; charset=UTF-8" import="java.sql.*" %>
<%
String id = request.getParameter("id");
String JDBC_DRIVER = "com.mysql.jdbc.Driver";
String DB_URL = "jdbc:mysql://localhost:3306/test";
String USER = "root";
String PASS = "password";
try {
Class.forName(JDBC_DRIVER);
Connection conn = DriverManager.getConnection(DB_URL, USER, PASS);
String sql="DELETE FROM test WHERE id=?";
PreparedStatement psmt=conn.prepareStatement(sql);
psmt.setInt(1,Integer.parseInt(id));
psmt.executeUpdate();
conn.close();
response.sendRedirect("index.jsp");
} catch (ClassNotFoundException | SQLException e) {
e.printStackTrace();
}
%>
<%@ page contentType="text/html; charset=UTF-8" import="java.sql.*" %>
<%
String id = request.getParameter("id");
String name = request.getParameter("name");
String course = request.getParameter("course");
String score = request.getParameter("score");
String JDBC_DRIVER = "com.mysql.jdbc.Driver";
String DB_URL = "jdbc:mysql://localhost:3306/test";
String USER = "root";
String PASS = "password";
String sql="UPDATE test SET name=? ,course=?,score=?,id=?";
try {
Class.forName(JDBC_DRIVER);
Connection conn = DriverManager.getConnection(DB_URL, USER, PASS);
PreparedStatement psmt=conn.prepareStatement(sql);
psmt.setString(1,name);
psmt.setString(2,course);
psmt.setInt(3,Integer.parseInt(score));
psmt.setInt(4,Integer.parseInt(id));
psmt.executeUpdate();
conn.close();
response.sendRedirect("index.jsp");
} catch (ClassNotFoundException | SQLException e) {
e.printStackTrace();
}
%>