javaweb简单的增删改查之“加”

我相信对每一个初学者来说能有一个好的教程真的帮助非常大,虽然我也只是一个初学者,我相信我所学到的转化为自己的语言对大家会有帮助

1.连接数据库

public class Databass {
public static String db_url = "jdbc:mysql://localhost:3306/blog?characterEncoding=utf8";
public static String db_user = "root";
public static String db_pass = "123";

public static Connection getConn () {
Connection conn = null;

try {
Class.forName("com.mysql.jdbc.Driver");//加载JDBC驱动
System.out.println("JDBC加载成功");
conn = DriverManager.getConnection(db_url, db_user, db_pass);
System.out.println("数据库连接成功");
} catch (Exception e) {
e.printStackTrace();
}

return conn;
}

/**
* 数据库用完之后是不是要关一下
* @param state
* @param conn
*/
public static void close (Statement state, Connection conn) {
if (state != null) {
try {
state.close();
} catch (SQLException e) {
e.printStackTrace();
}
}

if (conn != null) {
try {
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}

public static void close (ResultSet rs, Statement state, Connection conn) {
if (rs != null) {
try {
rs.close();
} catch (SQLException e) {
e.printStackTrace();
}
}

if (state != null) {
try {
state.close();
} catch (SQLException e) {
e.printStackTrace();
}
}

if (conn != null) {
try {
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
public static void main(String[] args) {
getConn ();

}
}

2.创建JavaBean对数据库参数进行封装(也就是数据库中的表内容)

public class Member {
private String name;
private String sex;
public Member(String name, String sex) {
super();
this.name = name;
this.sex = sex;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}

}

3.方法,向数据库添加信息

public class Method {
Connection con=Databass.Databass.getConn();
public boolean add(Member user) {
boolean flash=false;
PreparedStatement pre=null;
String sql="insert into Blog value('"+user.getName()+"','"+user.getSex()+"')";
try {
pre=con.prepareStatement(sql);
int i=pre.executeUpdate();
if(i>0) {
flash=true;
}
}catch(SQLException e) {
e.printStackTrace();
}finally {
Databass.Databass.close(pre, con);
}
return flash;
}

}

4.创建servlet(其实就是一个.java与.jsp或.html信息互相传递的一个过程)

public class ServletAdd extends HttpServlet {
private static final long serialVersionUID = 1L;

/**
* @see HttpServlet#HttpServlet()
*/
public ServletAdd() {
super();
// TODO Auto-generated constructor stub
}

/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
response.getWriter().append("Served at: ").append(request.getContextPath());
}

/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
//设置请求字体
request.setCharacterEncoding("utf-8");
response.setContentType("text/html;charset=utf-8");
//从HTML或jsp中获取数据
String name=request.getParameter("name");
String age=request.getParameter("age");
//下面要做的就是吧获取的值添加到数据库中
Method ww=new Method();
ww.add(new Member(name,age));
}

}

5.下面就是写一个非常简单的jsp文件

<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<form action="ServletAdd" method="post">
姓名:
<input type="text" name="name"><br>
年龄:
<input type="text" name="age"><br>
<input type="submit" value="添加">
</form>

</body>
</html>

结果如下:

 虽然非常简单,但是以后难度会增加的,这是我第一次认真的写博客!

猜你喜欢

转载自www.cnblogs.com/lianggegege123/p/12052227.html
今日推荐