idea 连接数据库实现对数据库的增删改查
数据库截图
表格截图
user Bean user.java代码``
package com.name;
public class user {
private String username;
private String password;
private String gender;
private int age;
public String getUsername()
{
return username;
}
public void setUsername(String username){
this.username=username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password= password;
}
public String getGender() {
return gender;
}
public void setGender(String gender) {
this.gender= gender;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
}
index代码``
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>$Title$</title>
</head>
<body>
<form method="post" action="register.jsp">
用户名:<input type="text" name="username"><br>
密码:<input type="text" name="password"><br>
性别:<input type="text" name="gender"><br>
年龄:<input type="text" name="age"><br>
<button type="submit">注册</button>
</form>
</body>
</html>
增加数据库用户代码
<jsp:useBean id="user" class="com.name.user">
</jsp:useBean
<jsp:setProperty name="user" property="*"></jsp:setProperty>
<%
Class.forName("com.mysql.jdbc.Driver");
String url ="jdbc:mysql://127.0.0.1:3306/book?user=root&password=root";
Connection connection = DriverManager.getConnection(url);
String sql = "insert into user (username,password,gender,age) values ('"+user.getUsername()+"','"+user.getPassword()+"',1,23)";
Statement stmt = connection.createStatement();
int count = stmt.executeUpdate(sql);
if(count == 0){
out.print("注册失败");
}else{
out.print("注册成功");
}
%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<jsp:useBean id="user" class="com.name.user"></jsp:useBean>
<jsp:setProperty name="user" property="*"></jsp:setProperty>
<%
Class.forName("com.mysql.jdbc.Driver");
String url ="jdbc:mysql://127.0.0.1:3306/book";
Connection connection = DriverManager.getConnection(url,"book","book");
String sql = "insert into user(username,password,gender,age) values (?,?,?,?)";
PreparedStatement temt = connection.prepareStatement(sql);
temt.setString(1,request.getParameter("username"));
temt.setString(2,request.getParameter("password"));
temt.setString(3,request.getParameter("gender"));
temt.setInt(4,request.getParameter("age"));
ResultSet rs = temt.executeQuery();
删除数据库用户
<jsp:setProperty name="user" property="*"></jsp:setProperty>
<%
Class.forName("com.mysql.jdbc.Driver");
String url ="jdbc:mysql://127.0.0.1:3306/book?user=root&password=root"
Connection connection = DriverManager.getConnection(url);
Statement stmt = connection.createStatement();
String sql="delete from user where id = 4 ";
int count = stmt.executeUpdate(sql);
if(count == 0){
out.print("删除失败");
}else{
out.print("删除成功");
}
%>
删除id为4的用户
数据库的查询
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>主页</title>
</head>
<body>
<jsp>
<%
Class.forName("com.mysql.jdbc.Driver");
String url = "jdbc:mysql://localhost:3306/book";
Connection connection = DriverManager.getConnection(url,"root","root");
String sql = "select * from user where username = ? and password= ?";
PreparedStatement pstm = connection.prepareStatement(sql);
pstm.setString(1,request.getParameter("username"));
pstm.setString(2,request.getParameter("password"));
ResultSet rs=pstm.executeQuery();
/*Statement stmt = connection.createStatement();
String sql = "select * from user";
ResultSet rs = stmt.executeQuery(sql);
*/
while (rs.next()) {
out.print(rs.getString("username")+"-"+rs.getString(2)+ "<br>");
}
%>
</body>
</html>