• myecilpse +TOMCAT+web:jsp向mysql添加数据,查询在jsp页面显示


    <%@ page language="java" import="java.util.*" import="com.mysql.jdbc.Driver" import="java.sql.*" contentType="text/html;charset=gb2312" pageEncoding="UTF-8"%>
    <!--导入相关的类 ,规定编码gb2312-->
    <%
    String path = request.getContextPath();
    String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
    %>
    
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
    <html>
      <head>
        <base href="<%=basePath%>">
        
        <title>My JSP 'insertVal.jsp' starting page</title>
        
        <meta http-equiv="pragma" content="no-cache">
        <meta http-equiv="cache-control" content="no-cache">
        <meta http-equiv="expires" content="0">    
        <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
        <meta http-equiv="description" content="This is my page">
        <!--
        <link rel="stylesheet" type="text/css" href="styles.css">
        -->
    
      </head>
      
      <body> 
        bThis is my JSP page. <br>
        <%
           
           Connection conn;
           Statement stat;
           //设置连接的url,其中student是数据库名称
           String url="jdbc:mysql://localhost:3306/student";
           //我使用的是免安装版的mysql,用户:root,密码:zhangweijie
           String userName="root";
           String password="zhangweijie";
            
           try{
           //注册JDBC驱动程序
              Class.forName("com.mysql.jdbc.Driver");
           
           }
           catch(ClassNotFoundException ex){
           
               out.println("找不到驱动程序!");
           }
           //打开数据库连接
           conn=DriverManager.getConnection(url, userName, password);
          
           try{
                stat = conn.createStatement();
                stat.executeUpdate("insert into Student_Info values(20140001,'宋江','13502022')");
                stat.executeUpdate("insert into Student_Info values(20140002,'张江','13502042')");
                stat.executeUpdate("insert into Student_Info values(20140003,'成江','13502032')");
                stat.executeUpdate("insert into Student_Info values(20140004,'刘江','13503422')");
               //获取查询结果 
                ResultSet result=stat.executeQuery("select * from Student_Info;");
                out.println("<Table Border>");
                out.println("<TR><TD colspan=3 align=center>学生信息 </TD></TR>");
                out.println("<TR>");
                out.println("<Td width=150>学号</td>");
                out.println("<Td width=150>学生姓名 </td>");      
                out.println("<Td width=150>联系电话 </td>");
                out.println("</TR>"); 
               
                while(result.next()){
                     out.println("<TR>");
                     out.println("<Td>"+result.getInt(1)+"</td>");
                     out.println("<Td>"+result.getString(2)+"</td>");      
                     out.println("<Td>"+result.getInt(3)+"</td>"); 
                     out.println("</TR>");
                }  
              }  
          
           
           catch(SQLException ex)
           {
              //再次刷新就会提示如下,原因是Student_Info数据表中的数据已经存在,
             //再次刷新页面相当于重新把上面的数据添加到数据表中,由于原来已经存在,故提示如下
                out.println("数据表操作失败!");
           }
        
          finally{
              //关闭数据库连接
               conn.close();
          }
         %>
        
      </body>
    </html>
  • 相关阅读:
    Windows 2003,XP安装Windows Phone 7 狼人:
    Android实例剖析笔记(六) 狼人:
    Android实例剖析笔记(一) 狼人:
    Android NDK带来什么 狼人:
    Windows Phone 7常用资源大集合 狼人:
    Android实例剖析笔记(七) 狼人:
    JDK环境变量配置
    jQuery去掉字符串起始和结尾的空格
    结构型设计模式
    主题:hibernate生成tree(基于注解方式)
  • 原文地址:https://www.cnblogs.com/yuhuameng/p/3746851.html
Copyright © 2020-2023  润新知