日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 运维知识 > 数据库 >内容正文

数据库

链接数据库增删改通用

發(fā)布時間:2025/3/12 数据库 28 豆豆
生活随笔 收集整理的這篇文章主要介紹了 链接数据库增删改通用 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

實現(xiàn)對SQLServer和MySql數(shù)據(jù)庫通用鏈接及數(shù)據(jù)的增刪改

我們經(jīng)常需要和數(shù)據(jù)庫打交道,對數(shù)據(jù)庫數(shù)據(jù)進行增改刪查的操作,首先我們必須要先鏈接數(shù)據(jù)庫,然后對數(shù)據(jù)內(nèi)容進行相關增刪改操作。

首先看一下目錄結構

程序解讀:一共有三個子包分別為model包test包和util包
@:model包是數(shù)據(jù)信息的類
@@:test包是對數(shù)據(jù)進行查詢獲取內(nèi)容
@@@:util包里的DBUtil是對數(shù)據(jù)庫進行鏈接和增刪改操作的內(nèi)容,info文件是數(shù)據(jù)庫的相關信息包括,url,name和pwd等

下面向大家展示代碼

util包的DBUtil.java

package com.util;import java.io.IOException; import java.sql.*; import java.util.ArrayList; import java.util.List; import java.util.Properties;/** 通用增刪改!!!!!!!!!!!!!!* 數(shù)據(jù)庫驅(qū)動* 張叔行*/ public class DBUtil {public DBUtil() {System.out.println("啟動jdbc");}/*** 定義變量*/private static String driver = null;//驅(qū)動private static String url = null;//mysql鏈接private static String name = null;//數(shù)據(jù)庫鏈接賬戶private static String pwd = null;//數(shù)據(jù)庫鏈接密碼/*** 定義靜態(tài)代碼塊*/static {Properties porperties = new Properties();//java 的配置文件try {porperties.load(DBUtil.class.getResourceAsStream("info.properties"));driver = porperties.getProperty("driver");url = porperties.getProperty("url");name = porperties.getProperty("name");pwd = porperties.getProperty("pwd");} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();}}/*** 鏈接數(shù)據(jù)庫*/public static Connection getConn() {Connection conn = null;try {Driver.class.forName(driver);conn = DriverManager.getConnection(url,name,pwd);} catch (ClassNotFoundException e) {// TODO Auto-generated catch blocke.printStackTrace();} catch (SQLException e) {// TODO Auto-generated catch blocke.printStackTrace();}System.out.println("數(shù)據(jù)庫鏈接成功");return conn;}/*** 關閉數(shù)據(jù)庫*/public static void setClose(Connection conn,PreparedStatement ps , ResultSet rs) {try {if(conn != null) {conn.close();}else if(ps != null) {ps.close();}else if(rs != null) {rs.close();}} catch (SQLException e) {// TODO Auto-generated catch blocke.printStackTrace();}}/*** 通用增刪改*/public static boolean executeUpdate(String sql , Object[] obj) {PreparedStatement ps = null;Connection conn = getConn();Boolean bool = false;int rsInt = 0;try {sql = "insert into user(u_id,u_name,u_sex,u_phone,u_money,u_map) values(?,?,?,?,?,?)";ps = conn.prepareStatement(sql);for(int i = 0;i<obj.length;i++) {ps.setObject(i+1, obj[i]);}rsInt = ps.executeUpdate();if(rsInt < 0) {System.out.println("添加失敗");bool = false;}else {System.out.println("添加成功");bool = true;}} catch (SQLException e) {// TODO Auto-generated catch blocke.printStackTrace();}return bool;}/*** 通用查通用查不能用list指定的返回類型要是通用的ResultSet來返回* 通用指的是任何查詢都能用*/public static ResultSet executeSelect(String sql , Object[] obj) {Connection conn = getConn();PreparedStatement ps = null;ResultSet rs = null;try {ps = conn.prepareStatement(sql);//如果object的值不等于空來執(zhí)行這一句話if( obj != null) {for(int i = 0;i<obj.length;i++) {ps.setObject(i+1, obj[i]);}}rs = ps.executeQuery();} catch (SQLException e) {// TODO Auto-generated catch blocke.printStackTrace();}return rs;} }

util包里的info文件:

特別注意:此處是實現(xiàn)對不同數(shù)據(jù)庫鏈接及增刪改操作的重要部分,***鏈接不同的數(shù)據(jù)庫要修改相對應的url,name和pwd***,修改了之后就可以實現(xiàn)對SQLServer或者MySql數(shù)據(jù)庫等的鏈接及增刪改操作了。

driver=com.mysql.jdbc.Driver url=jdbc:mysql://localhost:3306/log?useUnicode=true&characterEncoding=utf8 name=root pwd=root

test包里的test.java

package com.test;import com.util.DBUtil;import java.sql.ResultSet; import java.sql.SQLException;public class test {public static void main(String[] args) {// TODO Auto-generated method stub//通用的增刪改String sql = "select * from user where u_name = ?";Object obj[] = {"hwl"};ResultSet selectS = DBUtil.executeSelect(sql, obj);try {while(selectS.next()) {System.out.println(selectS.getString(1)+"\t"+selectS.getString(2)+"\t"+selectS.getString(3)+"\t"+selectS.getInt(4)+"\t"+selectS.getString(5)+"\t"+selectS.getString(6));}} catch (SQLException e) {e.printStackTrace();}}}

model包里的user.java

package com.model;public class user {private static String UId;private static String UName;private static String USex;private static String UPhone;private static int UMoney;private static String UMap;public static String getUId() {return UId;}public static void setUId(String uId) {UId = uId;}public static String getUName() {return UName;}public static void setUName(String uName) {UName = uName;}public static String getUSex() {return USex;}public static void setUSex(String uSex) {USex = uSex;}public static String getUPhone() {return UPhone;}public static void setUPhone(String uPhone) {UPhone = uPhone;}public static int getUMoney() {return UMoney;}public static void setUMoney(int uMoney) {UMoney = uMoney;}public static String getUMap() {return UMap;}public static void setUMap(String uMap) {UMap = uMap;}}

看到這里相信已經(jīng)能夠解決你的困惑了,快去執(zhí)行數(shù)據(jù)庫吧!!!

總結

以上是生活随笔為你收集整理的链接数据库增删改通用的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。