推荐!一个通用的javaBean很方便的实现连接数据库、查询、插入、修改功能

这是一个通用的javaBean,能够很方便的实现连接数据库、查询、插入、修改功能。、

package com.common;
import java.sql.*;
public class DataBaseConn {
 public  Connection conn = null;
 
 public void createConn(){
  String DRIVER = "org.gjt.mm.mysql.Driver";
  String url="jdbc:mysql://localhost/xin126?user=root&password=789456&useUnicode=true&characterEncoding=GB2312";
  if(conn == null){
   try{
    Class.forName(DRIVER).newInstance();
    conn = DriverManager.getConnection(url);
   }catch(Exception e){
    e.printStackTrace();
   }
  }
 }
 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();
  }
 }
}
相关文章:

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

(0)
江山如画的头像江山如画管理团队
一个完整的servlet验证登录用户名和密码实例
上一篇 2020年12月6日 下午7:26
最全!jsp连接各种数据库代码及读取数据实例
下一篇 2020年12月6日 下午7:41

99%的人还看了以下文章

  • servlet中如何使用Session?

    servlet中Session的用法 使用Servlet中的request对象获取session对象并输出其属性:HttpSession session = request.getSession(); request.setCharacterEncoding(“utf-8”); response.setContentType(“text/html;chars…

    2020年4月4日
    7.3K0
  • opencv 图像旋转 cv2.rotate和np.rot90案例精讲

    OpenCV 方法 OpenCV 中带有一个旋转图像的函数 cv2.rotate rotate(src, rotateCode[, dst]) -> dst参数:src:输入图像rotateCode:旋转方式1、cv2.ROTATE_90_CLOCKWISE:顺时针 90 度2、cv2.ROTATE_180:顺时针 180 度3、cv2.ROTATE_…

    2022年12月1日
    9.8K0
  • Myeclipse设置JSP视图和代码显示在同一个窗口

    Myeclipse设置JSP视图和代码显示在同一个窗口Myeclipse设置JSP视图和代码显示在同一个窗口Myeclipse设置JSP视图和代码显示在同一个窗口Myeclipse设置JSP视图和代码显示在同一个窗口

    Myeclipse开发时,想同时查看页面及代码视图,如上图,而默认只能看到代码。 Myeclipse设置JSP页面和代码显示在同一个窗口的方法有两种。 一、在JSP页面上右击-open with-other… 在弹出的Editor selection窗口中选择web page editor,就可以了。 二、选择 window-preferences,如下图,…

    2018年12月17日 编程开发
    5.5K0
  • 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日
    10.7K0
  • 新闻管理系统数据库设计

    新闻管理系统数据库,包含用户表、新闻评论表、管理员表、栏目表、新闻表、评论表、关键字表 每个表的数据字段,数据类型,是否为空,描述,见下表:

    2020年4月11日
    11.6K0
  • jsp写mysql数据库出现中文乱码

    今天用jsp做个图片书管理系统,向mysql数据库中存中文的时候显示乱码,如图书名: web???? 修改页面是http://localhost:8080/library/book?action=bookModifyQuery&ID=14 根据中文乱码的处理方法: 表单method方式为post或get中文乱码的解决方法 jsp:include包含h…

    2020年8月22日
    4.9K0

发表回复

登录后才能评论