jsp+javabean 连接 mysql 数据库

news/2024/5/18 23:27:20 标签: java, jsp, tomcat, jdbc, javabean

JSP+JavaBean

 

1、JavaBean类

 

首先先澄清  这个三个都是绝对路径 没有相对路径, E://test.txt 和 E:/test.txt 是一个意思  E:\\text.txt 用这样的写法是 \\ 转义了 ‘\’  .

为什么有\\ 的写法 和 / 这样的写法呢  ,因为window 默认的是\\ 这样的写法  如果你把这样的写法放到了 linux系统下面,那么这样的写法就是错误的了,linux 系统下面只认识/ 的写法。

File file =New File("E://test.txt")、

File file =New File("E:\\test.txt")、

File file =New File("E:/test.txt")

System.getProperty("user.dir")+"/src/data/qlsxgh.xls"我通过这个也行

 

//private String filepath="javabeantest\\src\\util\\JDBCconfig.properties";//error

    //private String filepath="src\\util\\JDBCconfig.properties";//error

    //private String filepath="javabeantest/src/util/JDBCconfig.properties";//error

    //private String filepath="util\\JDBCconfig.properties";//error以上只适合本地路径,不能放到tomcat下面的哦。

    //private String filepath = "/util/JDBCconfig.properties";//righttomcat andlinux only right in this way..

    //private String filepath = "\\util\\JDBCconfig.properties";//error

    private String filepath = "//util//JDBCconfig.properties";//righttomcat

 

 

1显示学生信息内容

 

 

 

JavaBean

 

package com.DataBase;

import java.sql.*;

public class MyDbBean

{

   private Statement stmt = null;

   ResultSet rs = null;

   private Connection conn = null;

   private String dsn;

   //构造函数

   public MyDbBean() { }

   //根据dsn参数,加载驱动程序,建立连接

   public void getConn(String dbname, String uid, String pwd) throwsException

    {

       try

       {

           dsn ="jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=" +dbname;

           Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver").newInstance();

           conn = DriverManager.getConnection(dsn, uid, pwd);

       }

       catch (Exception ex)

       {

           System.err.println("aq.executeQuery: " + ex.getMessage());

       }

    }

   //执行查询类的SQL语句,有返回集

   public ResultSet executeQuery1(String sql)

    {

       rs = null;

       try

       {

       stmt =conn.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_READ_ONLY);

           rs = stmt.executeQuery(sql);

       }

       catch(SQLException ex)

       {

           System.err.println("aq.executeQuery:"+ex.getMessage());

       }

       return rs;

    }

   //执行更新类的SQL语句,无返回集

   public void executeUpdate2(String sql)

    {

       stmt = null;

       rs=null;

       try

       {

      stmt =conn.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_READ_ONLY);

           stmt.executeQuery(sql);

           stmt.close();

           conn.close();

       }

       catch(SQLException ex)

        {

           System.err.println("aq.executeQuery: " + ex.getMessage());

       }

    }

   //关闭对象

   public void closeStmt()

    {

       try{   stmt.close();   }

       catch(SQLException ex)

       {

           System.err.println("aq.executeQuery: " + ex.getMessage());

       }

    }

   public void closeConn()

    {

       try{   conn.close();  }

       catch(SQLException ex)

       {

           System.err.println("aq.executeQuery: " + ex.getMessage());

       }

    }

}

 

 

 

list.jsp

 

 

 

 

<%@pagecontentType="text/html" pageEncoding="UTF-8"%>

<%@pageimport="java.sql.*" %>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"

  "http://www.w3.org/TR/html4/loose.dtd">

   <jsp:useBean id="testbean"scope="session" class="com.DataBase.MyDbBean" />

<html>

   <head>

        <metahttp-equiv="Content-Type" content="text/html; charset=UTF-8">

       <title>学籍管理系统</title>

     </head>

  <%!   String url,sql; %>  

  <%! int i;%>

  <body bgcolor="#ffffff">

     <div align="center"><font color="#000000"size="5">学籍管理系统 </font> </div>

     <table width="75%" border="1" cellspacing="1"cellpadding="1" align="center">

     <tr>

        <td width=16% align=center>学号</td>

            <td width=16% align=center>姓名</td>

            <td width=8% align=center>性别</td>

            <td width=8% align=center>年龄</td>

           <td width=16% align=center>籍贯</td>

           <td width=12% align=center>院系</td>

           <td width=12% align=center>更改</td>

           <td width=12% align=center>删除</td>

   </tr>

    <%

      //调用getConn方法与数据库建立连接

       testbean.getConn("Data_Stu","sa","");

         sql="select * from stuInfo";

         ResultSet  rs=testbean.executeQuery1(sql);//查询数据库

          while(rs.next()){

    %>

      <tr>

        <td width=16%align=center><%=rs.getString(1)%></td>

           <td width=16%align=center><%=rs.getString(2)%></td>

           <td width=8%align=center><%=rs.getString(3)%></td>

           <tdwidth=8% align=center><%=rs.getInt(4)%></td>

           <td width=16%align=center><%=rs.getString(5)%></td>

           <td width=12%align=center><%=rs.getString(6)%></td>

           <td width=12% align=center><ahref="change.jsp?xuehao=<%=rs.getString(1)%>">修改</a></td>

           <td width=12% align=center><ahref="del.jsp?xuehao=<%=rs.getString(1)%>">删除</a></td>

      </tr>

      <%

          }

           rs.close();

           testbean.closeStmt();

           testbean.closeConn();

    %>

</table>

<div align="center"><ahref="insert.jsp">添加新记录 </a> </div>

</body>

</html>



http://www.niftyadmin.cn/n/865896.html

相关文章

说说 Spring 的注解配置方式

1 定义 Bean XML 或注解配置方式&#xff0c;都是表达 Bean 定义的载体&#xff0c;其实质都是为 Spring 容器提供 Bean 定义的信息 。 基于注解的配置方式&#xff0c;从 Spring2.0 开始引入&#xff0c; Spring2.5 完善&#xff0c; Spring4.0 得到了进一步的增强 。 Spri…

java socket 编程实例

package myfistsocket; /*一个作为Server*/ import java.io.*; import java.net.*; class MynewServer { public static void main(String[] args) throws IOException { ServerSocket s new ServerSocket(6666); System.out.println("服务器端------监听中.....");…

说说在 Spring 中如何基于 Java 类进行配置

JavaConfig 原来是 Spring 的一个子项目&#xff0c;它通过 Java 类的方式提供 Bean 的定义信息&#xff0c;在 Spring4 的版本&#xff0c; JavaConfig 已正式成为 Spring4 的核心功能 。 1 定义 Bean 普通的 POJO 只要标注了 Configuration 注解&#xff0c;就可以为 Spri…

myeclipse 和 eclipse 报错总结(查看第三发jar源码)

error 1 : 2014-4-15 9:40:19 org.apache.catalina.loader.WebappClassLoader validateJarFile 信息: validateJarFile(D:\tomcat\apache-tomcat-5.5.34\temp\10-ShoppingBook10.1\WEB-INF\lib\servlet-api.jar) - jar not loaded. See Servlet Spec 2.3, section 9.7.2. Off…

抛出 NoClassDefFoundError: javax/validation/constraints/Size 问题的解决方法

Error:java: java.lang.NoClassDefFoundError: javax/validation/constraints/Size 问题很明显&#xff0c;找不到相关类。我们可以在 pom.xml 中引入 validation-api&#xff1a; <dependency><groupId>javax.validation</groupId><artifactId>vali…

flex 圆饼图的简单实现——xml数据

<s:Panel id"PieChart" x"30" y"300" width"500" height"500" title"消费一览表"> <!-- 创建 PieChart 图表 --> <mx:Legend dataProvider"{myPieChart}"/><!-- <mx:legend 表…

说说在 Spring 中如何使用编码方式动态配置 Bean

1 DefaultListableBeanFactory DefaultListableBeanFactory 实现了 ConfigurableListableBeanFactory 接口&#xff0c;可以通过这个类来动态注入 Bean。为了保证注入的 Bean 也能被 AOP 增强&#xff0c;我们需要实现 Bean 的工厂后置处理器接口 BeanFactoryPostProcessor。 …

说说 jBPM 工作流的定时器

jBPM 定时器&#xff08;Timer&#xff09;实现了以下功能&#xff1a; timer 定义在 transition 元素中&#xff0c;当流程处于 state、task、sub-process 类型活动的等待状态时&#xff0c;会开始计算 timer 的时间&#xff0c;当时间耗尽时&#xff0c;就会触发转移。timer…