JSP实现向数据库插入数据

假设我们向mysql数据库中的news数据库中的users表中插入roleID、username、password三项数值,数据库地址为本地,账号密码都是root。

首先第一步:

我们需要建立数据库,在数据库中建立users表,字段分别为id(主键) username(账号) password(密码) roleID(角色),这个不做详细解释。

接下来我们需要建立jsp页面,在这里建立3个jsp页面,分别为index.jsp(插入数据页面)、fuction.jsp(执行插入数据的逻辑操作)、success.jsp(跳转成功页面)。PS:其实还可以建立一个插入失败时跳转的页面,自己可以试试。

下面是关键代码:

index.jsp中主要是几个文本框用来填写数据:

<%@pagelanguage="java"import="java.util.*"pageEncoding="utf-8"%>

<%

String path = request.getContextPath();

String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";

%>

<!DOCTYPEHTMLPUBLIC"-//W3C//DTD HTML 4.01 Transitional//EN">

<html>

<head>

<basehref="<%=basePath%>">

<title>My JSP 'index.jsp' starting page</title>

<metahttp-equiv="pragma"content="no-cache">

<metahttp-equiv="cache-control"content="no-cache">

<metahttp-equiv="expires"content="0">

<metahttp-equiv="keywords"content="keyword1,keyword2,keyword3">

<metahttp-equiv="description"content="This is my page">

<!--www.mwcly.cn

<link rel="stylesheet" type="text/css" href="styles.css">

-->

</head>

<body>

<formaction="fuction.jsp"method="post">

<tablewidth="400"align="center"border="1"cellpadding="0"cellspacing="0">

<tr><tdcolspan="2"align="center">向数据库插入信息测试</td></tr>

<tr><td>username:</td><td><inputtype="text"name="username"/></td></tr>

<tr><td>password:</td><td><inputtype="text"name="password"/></td></tr>

<tr><td>roleID:</td><td><inputtype="text"name="roleID"/></td></tr>

<tr><tdcolspan="2"align="center"><inputtype="submit"value="插入"/></td></tr>

</table>

</form>

</body>

</html>

第二个是fuction.jsp主要逻辑操作都在里面

<%@pagelanguage="java"import="java.util.*,java.sql.*"pageEncoding="utf-8"%>

<%

String path = request.getContextPath();

String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";

%>

<!DOCTYPEHTMLPUBLIC"-//W3C//DTD HTML 4.01 Transitional//EN">

<html>

<head>

<basehref="<%=basePath%>">

<title>My JSP 'fuction.jsp' starting page</title>

<metahttp-equiv="pragma"content="no-cache">

<metahttp-equiv="cache-control"content="no-cache">

<metahttp-equiv="expires"content="0">

<metahttp-equiv="keywords"content="keyword1,keyword2,keyword3">

<metahttp-equiv="description"content="This is my page">

<!--

<link rel="stylesheet" type="text/css" href="styles.css">

-->

</head>

<body>

<%

     String username=request.getParameter("username");

     String passwd=request.getParameter("password");

int roleID=Integer.parseInt(request.getParameter("roleID"));

     String className="com.mysql.jdbc.Driver";

 String url="jdbc:mysql://localhost:3306/news";

 String user="root";

 String password="root";

 Connection conn; 

     Statement st;

Class.forName(className);

conn=DriverManager.getConnection(url, user, password);

String sql="INSERT INTO users(username,password,roleID) 

VALUES('"+username+"','"+passwd+"',"+roleID+")";

    st = (Statement) conn.createStatement();    // 创建用于执行静态sql语句的Statement对象   

int count = st.executeUpdate(sql);  // 执行插入操作的sql语句,并返回插入数据的个数   

if(count>0)

    {

    response.sendRedirect("success.jsp");

    }                   

    conn.close();   //关闭数据库连接   

%>

</body>

</html>

第三个成功页面success.jsp

<%@pagelanguage="java"import="java.util.*"pageEncoding="utf-8"%>

<%

String path = request.getContextPath();

String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";

%>

<!DOCTYPEHTMLPUBLIC"-//W3C//DTD HTML 4.01 Transitional//EN">

<html>

<head>

<basehref="<%=basePath%>">

<title>My JSP 'success.jsp' starting page</title>

<metahttp-equiv="pragma"content="no-cache">

<metahttp-equiv="cache-control"content="no-cache">

<metahttp-equiv="expires"content="0">

<metahttp-equiv="keywords"content="www.mwcly.cn,keyword2,keyword3">

<metahttp-equiv="description"content="This is my page">

<!--

<link rel="stylesheet" type="text/css" href="styles.css">

-->

</head>

<body>

    成功插入数据!<ahref="index.jsp">返回继续</a>

</body>

</html>

然后我们可以来看看运行结果是什么样

向数据库插入数据

 插入成功

检查数据库

 

原文地址:https://www.cnblogs.com/xiaona19841010/p/5157879.html