本文实例为大家分享了java连接mysql底层封装代码,供大家参考,具体内容如下

连接数据库

package com.dao.db;

import java.sql.Connection;
import java.sql.SQLException;

/**
 * 数据库连接层MYSQL
 * @author Administrator
 *
 */
public class DBConnection {
 
 
 /**
  * 连接数据库
  * @return
  */
 public static Connection getDBConnection()
 {
  // 1. 注册驱动
  try {
   Class.forName("com.mysql.jdbc.Driver");
  } catch (ClassNotFoundException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  }
  // 获取数据库的连接
  try {
   Connection conn = java.sql.DriverManager.getConnection("jdbc:mysql://localhost/mysql", "root", "root");
   return conn;
  } catch (SQLException e1) {
   e1.printStackTrace();
  }
  return null;
 }
 
}

数据层封装

package com.dao.db;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.sql.Types;
import java.util.ArrayList;
import java.util.HashMap;

/**
 * MYSQL数据库底层封装
 * @author Administrator
 *
 */
public class DBManager {
 
 private PreparedStatement pstmt;
 private Connection conn;
 private ResultSet rs;
 

 /**
  * 打开数据库
  */
 public DBManager() {
  conn = DBConnection.getDBConnection();
 }
 
 /**
  * 执行修改添加操作
  * @param coulmn
  * @param type
  * @param sql
  * @return
  * @throws SQLException
  */
 public boolean updateOrAdd(String[] coulmn, int[] type, String sql) throws SQLException
 {
  if(!setPstmtParam(coulmn, type, sql))
   return false;
  boolean flag = pstmt.executeUpdate()>0"htmlcode">
package com.dao.db;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;

/**
 * 数据集封装
 * @author Administrator
 *
 */
public class DataTable {
 
 public String[] column;//列字段
 public String[][] row; //行值
 public int rowCount = 0;//行数
 public int colCoun = 0;//列数
 
 
 public DataTable() {
  super();
 }
 
 public DataTable(String[] column, String[][] row, int rowCount, int colCoun) {
  super();
  this.column = column;
  this.row = row;
  this.rowCount = rowCount;
  this.colCoun = colCoun;
 }


 public void setDataTable(ArrayList<HashMap<String, String list) {
  rowCount = list.size();
  colCoun = list.get(0).size();
  column = new String[colCoun];
  row = new String[rowCount][colCoun];
  for (int i = 0; i < rowCount; i++) {
   Set<Map.Entry<String, String set = list.get(i).entrySet();
   int j = 0;
   for (Iterator<Map.Entry<String, String it = set.iterator(); it
     .hasNext();) {
    Map.Entry<String, String> entry = (Map.Entry<String, String>) it
      .next();
    row[i][j] = entry.getValue();
    if (i == rowCount - 1) {
     column[j] = entry.getKey();
    }
    j++;
   }
  }
 }

 public String[] getColumn() {
  return column;
 }

 public void setColumn(String[] column) {
  this.column = column;
 }

 public String[][] getRow() {
  return row;
 }

 public void setRow(String[][] row) {
  this.row = row;
 }

 public int getRowCount() {
  return rowCount;
 }

 public void setRowCount(int rowCount) {
  this.rowCount = rowCount;
 }

 public int getColCoun() {
  return colCoun;
 }

 public void setColCoun(int colCoun) {
  this.colCoun = colCoun;
 }
 
 

}

测试Demo

package com.bussiness.test;

import java.sql.SQLException;
import java.sql.Types;

import com.dao.db.DBManager;
import com.dao.db.DataTable;

public class TestBusIness{
 
 static String searchSql = "select * from score";
 static String insertSql = "insert into score(name, age, score)values(";
 static String deleteSql = "delete from score where id = ";
 static String updateSql = "update score set name = ";
 
 public static void main(String[] args) {
  intsertData();
  searchData();
 }
 
 private static void intsertData()
 { 
  DBManager dm = new DBManager();
  String[] coulmn = new String[]{"wyf2", "23", "89.5"};
  int[] type = new int[]{Types.CHAR, Types.INTEGER, Types.DOUBLE};
  
  try {
   boolean flag = dm.updateOrAdd(coulmn, type, insertSql);
   if(flag)
    System.out.println("插入成功");
  } catch (SQLException e) {
   e.printStackTrace();
  }
 }
 private static void searchData()
 { 
  DBManager dm = new DBManager();
  String[] coulmn = null;
  int[] type = null;
  
  try {
   DataTable dt = dm.getResultData(coulmn, type, searchSql);
   if(dt != null && dt.getRowCount()> 0){   
    for(int i = 0; i<dt.getRowCount(); i++)
    {
     for(int j = 0; j<dt.getColCoun(); j++)
     System.out.printf(dt.getRow()[i][j]+"\t");
     System.out.println();
    }
   }
   else
    System.out.println("查询失败");
  } catch (SQLException e) {
   e.printStackTrace();
  }
 }
}

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。

标签:
java连接mysql底层封装,java连接mysql底层,java连接mysql

免责声明:本站文章均来自网站采集或用户投稿,网站不提供任何软件下载或自行开发的软件! 如有用户或公司发现本站内容信息存在侵权行为,请邮件告知! 858582#qq.com
狼山资源网 Copyright www.pvsay.com

评论“java连接mysql底层封装详解”

暂无“java连接mysql底层封装详解”评论...

RTX 5090要首发 性能要翻倍!三星展示GDDR7显存

三星在GTC上展示了专为下一代游戏GPU设计的GDDR7内存。

首次推出的GDDR7内存模块密度为16GB,每个模块容量为2GB。其速度预设为32 Gbps(PAM3),但也可以降至28 Gbps,以提高产量和初始阶段的整体性能和成本效益。

据三星表示,GDDR7内存的能效将提高20%,同时工作电压仅为1.1V,低于标准的1.2V。通过采用更新的封装材料和优化的电路设计,使得在高速运行时的发热量降低,GDDR7的热阻比GDDR6降低了70%。