jsp+javabean 连接 mysql 数据库

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>


你可能感兴趣的:(java,tomcat,jsp,jdbc,javabean)