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

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

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%的人还看了以下文章

  • 人工智能基础测试

    1、 print(“Hi”) print(“3*6”) 程序输出结果:(5分) A、 Hi 18 B、 Hi 3*6 C、 Hi 3*6 D、 Hi 18 2、人工智能的概念最早是由哪一位科学家提出来的()(5分) A、 麦卡锡 B、 图灵 C、 冯·诺依曼 D、 马明斯基 3、下列关于人工智能的叙述不正确的有…

    2023年6月2日
    7.3K0
  • 10秒倒计时、考试结束倒计时功能实现代码-JS

    注册成功或登录后网页会有倒计时,如5秒后跳转到哪个页面的功能。 在做一些在线测试,网上考试系统时,会用到倒计时功能。 如网上考试系统里,会有时间提示离考试结束还有多长时间,临近考试结束剩10分钟,还可以弹窗提示考生。 JavaScript实现倒计时功能代码 <!DOCTYPE html> <html> <head> &lt…

    2020年11月1日
    4.3K0
  • AdaGrad算法—随机梯度下降算法

    AdaGrad算法,它可以根据自变量在每个维度的梯度值的大小来调整各个维度上的学习率,从而避免统一的学习率难以适应所有维度的问题。

    2023年10月9日
    3.6K0
  • 第五章 JavaBean技术

    掌握:
    JavaBean的概念及规范
    JavaBean的创建与使用
    JavaBean属性的获取及修改
    getProperty:获取bean的属性值
    setProperty:设置bean的属性值

    2018年2月22日
    6.8K0
  • 程序设计基础(C语言)—教学设计、教案

    教学设计——程序设计基础 教学基本信息 课程名称 程序设计基础 性质 专业基础课 学分 3 学时 48 题目 数据类型 专业年级 软件工程专业一年级 教材 书名:C程序设计(第五版) 出版社:清华大学出版社    出版日期: 2017年8月 教学背景分析 一、学习内容分析: 本节课要介绍的知识点——数据类型比较简单,但都是概念。对于这些陌生的、枯燥的纯概念性…

    2020年4月10日
    13.1K0
  • python 初学者练手上机实操五-循环语句练习

    一、题目:定义一个名称为numList的列表[1,5,9,8,12,43],使用for循环 遍历该列表,输出包含的元素 1、新建一个“for.py”文件。 2、编写程序。 3、调试程序。 4、排除错误。 二、、题目:定义一个名称为numTuple的元级(1,5,9,8,12,43),使用for循环 遍历该元组,输出包含的元素 三、分别使用for循环和whil…

    2023年5月26日
    12.2K1

发表回复

登录后才能评论