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

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

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

  • 上机四 数据库访问技术

    2学时 一、数据库编程案例:读取数据库信息并显示 数据库员工信息表字段如下: 使用JDBC连接数据库,读取employee表中信息并显示到页面。 mysql驱动下载:mysql-connector-java-5.1.41-bin.zip 注意事项:将驱动程序mysql-connector-java-5.1.6-bin,拷贝到Web应用程序的WEB-INF\l…

    2018年10月16日 编程开发
    6.3K0
  • Pandas读取excel:Excel file format cannot be determined解决方法

    Pandas读取excel时报错,excel表格不能被指定,是什么原因? 这个问题我搞了很久,最后终于搞明白了,网上各种什么utf-8呀,格式化或者另存都不行,我都试过了。 Excel file format cannot be determined解决方法 首先要确定excel已经放在项目目录下,路径是正确的。 一、然后确保安装了所需要的模块 pip in…

    2022年9月11日
    14.3K0
  • JQuery change ()、.on(‘change’, function (){})事件只触发一次原因详解

    JQuery中使用  $(“input”).on(“change”,function(){})  仅仅会触发一次,第二次点击或输入不再触发。 JQuery change ()、.on(‘change’, function (){})事件只触发一次解决方法 (1)用jQuery的live代…

    2019年11月13日
    9.5K0
  • %matplotlib inline使用详解

    #内嵌画图 %matplotlib inline import matplotlib # 注意这个也要import一次 import matplotlib.pyplot as plt myfont = matplotlib.font_manager.FontProperties(fname=r’C:/Windows/Fonts/msyh.ttf’) # 这一…

    2023年1月13日
    1.4K0
  • Pycharm Django项目 NameError: name ‘os’ is not defined

    Pycharm Djarngo项目报错 NameError: name ‘os’ is not defined 原因:这里调用了os模块,但是文件头并没引用os模块解决办法:在settings.py文件头加上 import os

    2024年12月2日
    5340
  • Pillow-优秀的Python图像处理库安装及入门教程

    Pillow库是Python 图像处理库(Python image library)的一个派生分支,提供了广泛的文件格式(BMP,PNG,JPEG等)支持,提供基本的图像处理能力,如: 图像存储、图像显示、改变图像大小,旋转图像,图像格式转换,色场空间转换,图像增强,直方图处理,插值和滤波等等。 比起OpenCV库的图像处理,功能有限,但函数使用非常方便,大…

    2020年12月8日
    3.2K0

发表回复

登录后才能评论