通用数据库操作类及员工信息查询实例

通用数据库操作类及员工信息查询实例

package com.common;
import java.sql.*;
public class DataBaseConn {
public  Connection conn = null;
public void createConn(){
  String url = "jdbc:mysql://localhost:3306/employeeDb"; 
  String username = "root";// 数据库用户名
String password = "root";// 数据库密码
  if(conn == null){
   try{ Class.forName("com.mysql.jdbc.Driver");
     conn = DriverManager.getConnection(url,username,password);
    }catch(Exception e){e.toString();}
  }
} 
public  Connection getConn(){
  	if(conn == null){  createConn();}
  	return conn;
 } 

 public  void closeConn(){
     if(conn != null){
   	try {
    		conn.close();
   	 	conn = null;
   	} catch (Exception e) {   	 e.printStackTrace();   }
     }
 }
 public ResultSet executeQuery(String sql) {
  	ResultSet rs = null;
 	 if (conn == null) {createConn();}
  	try {
 		 Statement stmt = conn.createStatement();
  		 rs = stmt.executeQuery(sql);
  	} catch (Exception e) {   e.printStackTrace();  }
   return rs;
 } 
public void executeUpdate(String sql) {
  if (conn == null) {createConn(); }
       try {  Statement stmt =    conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_READ_ONLY);
      stmt.executeUpdate(sql);
     } catch (Exception e) {   e.printStackTrace();  }
 }
}

 

<jsp:useBean id="con" class="com.common.DataBaseConn" scope="page"/>
<%    con.getConn();
     String  sql="select * from  employee_info";
     ResultSet rs= con.executeQuery(sql); 
 %>
<table border='1' width='100%'>
 <tr> <th>雇员号</th> <th>姓名</th> <th>出生日期</th><th >薪水</th></tr>
     <% 
while( rs.next() ) {  out.print("<tr>");
             out.print("<td >"+rs.getString(1)+"</td>"); 
             out.print("<td >"+rs.getString(2)+"</td>");
             out.print("<td >"+rs.getDate("birthday")+"</td>"); 
             out.print("<td >"+rs.getInt("salary")+"</td>");
              out.print("</tr>") ;   }
        out.print("</table>");
        con.closeConn();
    %>

125jz网原创文章。发布者:江山如画,转载请注明出处:http://www.125jz.com/4265.html

(0)
上一篇 2019年11月26日 下午8:04
下一篇 2019年11月27日 上午8:32

99%的人还看了以下文章

  • 免费网站打包APP,网址打包APP教程 – HBuilder

    网上有很多将网站在线打包成APP的网站,都是收费,其实网站只要做好移动端优化,或本身就是响应式网站,只需要简单的几个步骤就能把网站打包成简洁的APP。 相应工具很多,中国网页设计推荐使用:HBuilder:官网dcloud.io 会做网页就会制作APP 先学习如何制作APP   VS   先学的制作网页 其实很多APP都是利用网页打包成的… 利用HBuild…

    2019年6月24日 编程开发
    6.0K0
  • 第1课:1分钟了解C语言

    C语言的发展 最初程序员使用的程序设计语言是一种用二进制代码“0”和“1”形式表示的、能被计算机直接识别和执行的语言,称为机器语言。它是一种低级语言,用机器语言编写的程序不便于记忆、阅读和书写。通常不用机器语言直接编写程序。 在机器语言的基础上,设计出了汇编语言,它可以将机器语言用便于人们记忆和阅读的助记符表示,如ADD、SUB、MOV等。汇编语言适用于编写…

    2020年4月5日
    1.4K0
  • 第一个Spring MVC 项目:Hello World(Eclipse版)

    125建站网前面分享了《Spring框架概述》,新学习的同学可以先阅读引文章,今天给大家分享第一个Spring MVC实战项目:Hello World 目录  一、MVC概要 二、Spring MVC介绍 三、第一个Spring MVC 项目:Hello World(Eclipse版) 3.1、通过Maven新建一个Web项目 3.2、添加依赖的jar包 3…

    2023年1月24日 编程开发
    1290
  • JSP+MySQL MVC综合案例:完整的分页查询

    JSP+MySQL数据库开发教程,MVC综合案例,完整的分页查询代码。

    2018年2月22日
    3.3K0
  • Ubantu为应用程序添加桌面图标

    按要求配置应用程序参数,为应用程序添加桌面图标,实现系统快捷启动。 Ubuntu桌面的左边(默认在左边)有一个启动器,类似于window的任务栏。在Ubuntu18中叫dock(船坞),有时候又叫收藏夹。本文统一叫做启动器 在使用 Ubuntu 操作系统进行开发过程中,由于Ubuntu 系统其操作方式主要通过命令行终端进行交互,故启动软件也一般通过终端键入应…

    2022年9月1日 编程开发
    1930
  • 上机实战七:EL和JSTL的使用

    建议学时:2 一、使用EL表达式简化javaBean的开发 编写一个用户登录的JavaBean,用户信息包括用户名和密码。 编写user.jsp,使用setProperty设置用户名为125jz,密码为123。 使用EL获取用户名和密码并显示。 二、使用EL实现问卷调查 用户输入昵称、所在城市,并且以多选的方式让用户选择所使用的开发语言,然后使用EL表达式显…

    2018年12月4日
    3.3K0

发表回复

登录后才能评论