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

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

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

  • myeclipse集成的tomcat在哪个目录下

    myeclipse集成了Tomcat服务器,本文介绍了myeclipse集成的tomcat在哪个目录下?查看myeclipse集成的tomcat在哪个目录下的方法

    2020年2月6日
    12.9K0
  • python 初学者练手上机实操一

    一、从键盘输入商品名称、商品的单价、商品的数量, 计算商品总价,并按如下格式输出: 商品名称:牛奶,单价:5元,数量:2件,总价:10元 二、从键盘输入年龄,如果>=18岁显示“已成年”,否则显示“未成年”。 三、输入三条边长,如果能构成三角形就计算周长并显示,否则显示“不能构成三角形”。 四、百分制成绩转换为等级制成绩。 要求:如果输入的成绩在90分…

    2023年4月7日
    4.0K0
  • Tkinter(Python GUI编程)从入门到精通(一)

    什么是Tkinter 一个 GUI 程序一般由窗口、下拉菜单或者对话框等图形化组件构成, 通过鼠标点击菜单栏、按钮或者弹出对话框的形式来实现人机互动,从而提升人机交互的体验,让“冰冷”的程序变得有“温度”。 注意:GUI 这一概念并非 Python 语言独有,它属于计算机科学技术领域中的一个概念,比如使用 C/C++ 语言开发的 Qt、GTK、Electro…

    2022年8月20日
    5.3K0
  • Python编程案例-4行代码绘制股票趋势图(numpy+matplotlib)

    新手学习Python编程案例,每日编写一个小程序! import numpy as np import matplotlib.pyplot as plt 时间轴 = np.arange(10) #print(时间轴) 股票价格轴 = 时间轴 * 2 + np.sin(时间轴) * 5 #print(股票价格轴) plt.plot(时间轴, 股票价格轴) pl…

    2023年1月26日
    22.5K0
  • 上机实战八:Java web编程综合案例

    建议学时:6 一、开发基于MVC模式的信息管理系统,如新闻发布系统,要求用户可查看、查询。管理员进入后台可对发布新闻(实现相应的增删查改)。 后台添加文章,建议使用UEditor! UEditor 是开源、免费的在线HTML编辑器,主要用于让用户在网站上获得所见即所得编辑效果。 UEditor兼容性很好,是网站站长开发的首选,官网地址:UEditor官网 包…

    2018年12月11日
    6.0K0
  • Python编程入门:英文词频统计

    text = “Got tho on super sale. Love it! Cuts my drying time in half Reckon I have had this about a year now,\ at least 7 months. Works great, I use it 5 days a week, blows hot air,…

    2023年10月3日
    7.8K0

发表回复

登录后才能评论