从JSP页面向数据库数据表中插入数据

<%@ page language="java" import="java.sql.*" pageEncoding="UTF-8"%>
<html>
  <head>  
    <title>插入界面</title>
  </head>
  
  <body>
  <%
     String driverName = "com.microsoft.sqlserver.jdbc.SQLServerDriver"; 
    //加载JDBC驱动
    String dbURL = "jdbc:sqlserver://localhost:1433; DatabaseName=JSPTest"; 
    //连接服务器和数据库
    String userName = "sa"; //默认用户名
    String userPwd = "123456"; //密码
       Class.forName(driverName);
       Connection Conn = DriverManager.getConnection(dbURL, userName, userPwd);
       System.out.println("Connection Successful!"); 
       String sql="Insert into regist(ID,password) values(?,?)";
       PreparedStatement pstmt=Conn.prepareStatement(sql);
       pstmt.setString(1, "201504");
       pstmt.setInt(2, 111);
       int n=pstmt.executeUpdate();
       if(n==1){%>数据插入成功!<br><%}  
       else{%>数据插入失败!<br><%}
   %>
  </body>
</html>

 

posted @ 2017-10-20 23:23  随心佩  阅读(13920)  评论(0编辑  收藏  举报