Home  >  Article  >  Database  >  我的Java链接数据库,以及做添删查改所用的工具包--源代码_MySQL

我的Java链接数据库,以及做添删查改所用的工具包--源代码_MySQL

WBOY
WBOYOriginal
2016-06-01 13:07:351119browse



package com.msit.util;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

/**
 * 数据库工具类 链接MySql数据库
 *
 * @author Administrator
 *
 */
public class SqlHelper {
 // 链接需要的数据(这些数据直接写到加密后的数据文件中)。
 private static String url = "jdbc:mysql://127.0.0.1:3306/msitdb";
 private static String user = "root";
 private static String password = "root";
 private static String driverName = "com.mysql.jdbc.Driver";
 // 声明静态链接对象
 private static Connection connection = null;

 // 编写静态代码块(比构造函数先加载) 用来加载驱动类
 static {
  try {
   Class.forName(driverName);
  } catch (ClassNotFoundException e) {
   e.printStackTrace();
  }
 }

 public static Connection getConnection() {
  // 创建链接对象
  try {
   connection = DriverManager.getConnection(url, user, password);
  } catch (SQLException e) {
   e.printStackTrace();
  }
  return connection;
 }

 /**
  * 执行 添加 删除 修改的sql语句方法
  *
  * @param sql
  * @return 受影响行数
  */
 public static int executeNotQuery(String sql) {
  int num = 0;
  // 1.得到链接对象
  Connection conn = getConnection();
  // 2.创建命令执行对象
  Statement stm = null;
  try {
   stm = conn.createStatement();
   num = stm.executeUpdate(sql);
  } catch (SQLException e) {
   e.printStackTrace();
  } finally {
   close(null, stm, conn);
  }
  return num;
 }

 /**
  * 关闭资源的方法
  *
  * @param rs
  *            结果集
  * @param stm
  *            执行对象
  * @param conn
  *            链接对象
  */
 private static void close(ResultSet rs, Statement stm, Connection conn) {
  if (null != rs) {
   try {
    rs.close();
   } catch (SQLException e) {
    System.out.println("关闭结果集异常!");
    e.printStackTrace();
   }
  }
  if (null != stm) {
   try {
    stm.close();
   } catch (SQLException e) {
    System.out.println("关闭执行对象异常!");
    e.printStackTrace();
   }
  }
  if (null != conn) {
   try {
    conn.close();
   } catch (SQLException e) {
    System.out.println("关闭链接对象异常!");
    e.printStackTrace();
   }
  }
  rs = null;
  stm = null;
  conn = null;
 }// end

 /**
  * 执行查询语句的方法
  *
  * @param sql
  * @return 返回结果集
  */
 public static ResultSet executeQuery(String sql) {
  ResultSet rs = null;
  // 1.得到链接对象
  Connection conn = getConnection();
  // 2.创建命令对象
  try {
   Statement stm = conn.createStatement();
   // 3.执行查询方法 得到结果集
   rs = stm.executeQuery(sql);
  } catch (SQLException e) {
   e.printStackTrace();
  } finally {
   // 4.关闭资源
   //close(rs, stm, conn);
  }
  return rs;
 }
 /**
  * 执行预编译sql的方法
  * @param sql
  * @param pams
  * @return
  */
 public static int executPreStm(String sql,String[] pams){
  int num = 0;
  PreparedStatement pstm= null;
  try {
   //创建编译对象
   pstm = getConnection().prepareStatement(sql);
   //添加占位符的数据
   for (int i = 0; i     pstm.setString(i, pams[i-1]);
   }
   //执行sql语句
   num=pstm.executeUpdate();//只能执行非查询语句
   //关闭资源
   pstm.close();
   connection.close();
  } catch (SQLException e) {
   e.printStackTrace();
  }
  return num;
 }
}

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn