mysql主從java端實現,mysql主從搭建原理

本文目錄一覽:

如何用java實現mysql資料庫的導入導出

MySql導出資料庫的命令如下:

Sql代碼

mysqldump -uusername -ppassword -hhost -Pport exportDatabaseName exportPath

mysqldump -uusername -ppassword -hhost -Pport exportDatabaseName exportPath

利用Java調用命令窗口執行命令來進行MySql導入資料庫一般分三步走:

第一步:登錄Mysql資料庫,在登錄資料庫的時候也可以指定登錄到哪個資料庫,如果指定了則可以跳過第二步;

第二步:切換資料庫到需要導入的目標資料庫

第三步:利用命令開始導入

在進行導出的時候,需要注意命令語句的運行環境,如果已經將mysql安裝路徑下的bin加入到

系統的path變數中,那麼在導出的時候可以直接使用命令語句,否則,就需要在執行命令語句的

時候加上命令所在位置的路徑,即mysql安裝路徑想的bin下的mysqldump命令。

怎麼樣實現java與MYSQL的連接?

package jdbc;

import java.sql.Connection;

import java.sql.DriverManager;

import java.sql.PreparedStatement;

import java.sql.ResultSet;

import java.sql.SQLException;

import java.sql.Statement;

import org.apache.struts.util.MessageResources;

public class DBconnection {

private String driveName = null;

private String userName = null;

private String password = null;

private String url = null;

private Connection con =null;

private Statement sta=null ;

private PreparedStatement pre = null;

private ResultSet result = null;

/** 創建一個沒有參數的資料庫構造器

*

*

*/

public DBconnection() {

construts();

linkDataBase();

}

public DBconnection(Connection con){

this.con=con;

}

/** 實例化對象時調用的方法

*

*MessageResources

*/

private void construts() {

MessageResources message=MessageResources.getMessageResources(“resources.application”);

driveName = message.getMessage(“driveName”);

url = message.getMessage(“url”);

userName = message.getMessage(“userName”);

password = message.getMessage(“password”);

}

/** 載入資料庫的驅動程ry {

Class.序

*

*

*/

private void linkDataBase() {

try {

Class.forName(driveName);

} catch (ClassNotFoundException e) {

System.out.println(“Link DataBase Fail!”);

}

}

/** 建立一個Connection對象

*

*

*/

public Connection createConnection() {

try {

con = DriverManager.getConnection(url, userName, password);

return con;

} catch (SQLException e) {

System.out.println(“Connection Opened Fail!!”);

e.printStackTrace();

}

return null;

}

/** 建立一個Statement對象

*

*

*/

public void createNormalStatement() {

try {

sta = con.createStatement();

} catch (SQLException e) {

e.printStackTrace();

System.out.println(“NormalStatement Opened Fail!!”);

}

}

/** 設置一個預編譯對象

*

* @param sql 初始化預編譯的語句

*/

public void createPreparedStatement(String sql) {

try {

pre = con.prepareStatement(sql);

} catch (SQLException e) {

System.out.println(“PreparedStatement Opened Fail!!”);

}

}

/**建立一個指定sql語句的ResultSet對象

*

* @param sql 一條普通的查詢Sql語句,

*/

public void setResultSet(String sql) {

try {

result = sta.executeQuery(sql);

} catch (SQLException e) {

e.printStackTrace();

System.out.println(“Nomal resultset get failer!”);

}

}

/**執行準備好的 SQL 查詢並返回針對查詢SQL漏洞

*

*

*/

public void setPreparedStatementResultSet() {

try {

result = pre.executeQuery();

} catch (SQLException e) {

e.printStackTrace();

System.out.println(“PreparedStatement resultset get failer!”);

}

}

/** 判斷當前的Result對象中是否有結果集,有返回真,沒有返回假

*

* @return 是否有下一條記錄

*/

public boolean haveMoreResult() {

try {

return result.next();

} catch (SQLException e) {

System.out.println(“block result next have errors”);

return false;

}

}

/**關閉當前對象的Statement對象

*

*

*/

public void closeNormalStatement() {

try {

sta.close();

} catch (SQLException e) {

e.printStackTrace();

System.out.println(“NormalStatement close fail!”);

}

}

/** 關閉當前的Prepared對象

*

*

*/

public void closePreparedStatement() {

try {

pre.close();

} catch (SQLException e) {

e.printStackTrace();

System.out.println(“PreparedStatement close fail!”);

}

}

/**關閉當前的ResulrSet對象

*

*

*/

public void closeResultSet() {

try {

result.close();

} catch (SQLException e) {

e.printStackTrace();

System.out.println(“Result close fail!”);

}

}

/** 關閉Connection對象,檢查ResultSet,Statement,PrepardStament是否為空,

* 不為空就關閉,最後關閉Connection

*

*

*/

public void closeConnection() {

try {

if (result != null) {

result.close();

}

if (sta != null) {

sta.close();

}

if (pre != null) {

pre.close();

}

con.close();

} catch (SQLException e) {

e.printStackTrace();

System.out.println(“Connection close fail!”);

}

}

/** 設置Connection對象事務的支持

*

* @param bool 開啟變數

*/

public void setAutoCommit(Boolean bool) {

try {

con.setAutoCommit(bool);

} catch (SQLException e) {

e.printStackTrace();

}

}

/** 通過整數獲取ResultSet中的字元串值

*

* @param number 指定ResultSet中的列索引

* @return 獲得該值

*/

public String getResultSetValueByNumber(int number) {

try {

return result.getString(number);

} catch (SQLException e) {

e.printStackTrace();

}

return null;

}

/** 通過名字來獲得ResultSet指定列的字元串值

*

* @param columnName 指定的ResultSet列的名字

* @return 獲得該值

*/

public String getResultSetValueByColumn(String columnName) {

try {

return result.getString(columnName);

} catch (SQLException e) {

e.printStackTrace();

}

return null;

}

/** 通過整數獲取ResultSet中的整數值

*

* @param number 指定ResultSet中的列索引

* @return 獲得該值

*/

public int getResultSetIntValueByNumber(int number) {

try {

return result.getInt(number);

} catch (SQLException e) {

e.printStackTrace();

}

return -1;

}

/**通過名字來獲得ResultSet指定列的整數值

*

* @param columnName 指定的ResultSet列的名字

* @return 獲得該值

*/

public int getResultSetIntValueByColumn(String columnName) {

try {

return result.getInt(columnName);

} catch (SQLException e) {

e.printStackTrace();

}

return -1;

}

/** 設置預編譯語句的指定的數值列的?String值

*

* @param number 預編譯語句中?的索引

* @param value 相應的要傳入的值

* @return 是否成功設置

*/

public boolean setPreparedStatementStringValue(int number, String value) {

try {

pre.setString(number, value);

return true;

} catch (SQLException e) {

e.printStackTrace();

System.out.println(“setPreparedStatementStringValue failer “

+ number);

}

return false;

}

/**設置預編譯語句的指定的數值列的?int值

*

* @param number 預編譯語句中?的索引

* @param value 相應的要傳入的值

*/

public void setPreparedStatementIntValue(int number, int value) {

try {

pre.setInt(number, value);

} catch (SQLException e) {

e.printStackTrace();

System.out.println(“setPreparedStatementIntValue failer ” + number);

}

}

/** 提交preparedStatement(預編譯)針對多表事物語句

*

* @return 是否成功提交該語句

*/

public boolean preExecuteUpdate() {

try {

pre.executeUpdate();

return true;

} catch (SQLException e) {

e.printStackTrace();

}

return false;

}

/**提交事物(多條SQL一起提交)

*

*

*/

public void conCommit() {

try {

con.commit();

} catch (SQLException e) {

e.printStackTrace();

System.out.println(“Transfer has failer!”);

}finally{

closeConnection();

}

}

/** 資料庫事物不成功的時候回滾

*

*

*/

public void conRollback() {

try {

con.rollback();

System.out.println(“database rollback”);

} catch (SQLException e) {

e.printStackTrace();

}finally{

closeConnection();

}

}

/** 運行一條普通的Insert,Update,Delete語句

*

* @param sql 普通的INSERT,UPDATE,DELETE語句

* @return 是否成功操作

*/

public boolean insertUpdateDelete(String sql) {

try {

sta.executeUpdate(sql);

return true;

} catch (SQLException e) {

e.printStackTrace();

}

return false;

}

// 測試程序

// public static void main(String[] args) {

// DBconnection db = new DBconnection();

// db.createConnection();

// db.createNormalStatement();

// String sql = “select * from book_infor;”;

// db.setResultSet(sql);

// if (db.haveMoreResult()) {

// System.out.println(db.getResultSetIntValueByNumber(1));

// System.out.println(db.getResultSetValueByNumber(2));

// System.out.println(db.getResultSetValueByNumber(3));

// System.out.println(db.getResultSetValueByNumber(4));

// System.out.println(db.getResultSetValueByNumber(5));

// System.out.println(db.getResultSetValueByNumber(6));

// }

// }

}這個是jdbc,海有個連接池。

MySQl安裝個安裝版的,有圖形界面的。

怎麼用java實現mysql的複製資料庫里所有的表跟數據

樓主要考慮的不僅僅是標題的需求。

1、複製資料庫里所有的表和數據的目的是什麼。

a、假設樓主是要做資料庫備份的話,且通過程序來做的話,可以使用程序來執行dos命令

如java:Runtime.getRuntime().exec(“e:\\MySQL\\bin\\mysqldump -h localhost -uroot -p123 db_name”)

b、假設樓主是要做庫與庫之間的同步的話,可以使用第三方客戶端進行,比如navicat,sqlyong等

c、假設樓主是要做庫與庫之間的同步且用程序進行的話,可以使用mysql中提供操作資料庫的api來做相對應的讀取工作和對比工作,然後寫入工作

如何用java 5分鐘實現一個最簡單的mysql代理伺服器

如何用java 5分鐘實現一個最簡單的mysql代理伺服器

首先,準備開發工具套件,我們並不會引入過多工具包,僅僅需要:

java8

vert.x 3

如果你是用maven做為項目管理工具,請將vert.x 3引入:

1

2

3

4

5

dependency

groupIdio.vertx/groupId

artifactIdvertx-core/artifactId

version3.3.2/version

/dependency

代碼實現:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

package

com.maxleap.mysqlproxy;

import

io.vertx.core.AbstractVerticle;

import

io.vertx.core.Vertx;

import

io.vertx.core.logging.Logger;

import

io.vertx.core.logging.LoggerFactory;

import

io.vertx.core.net.NetClient;

import

io.vertx.core.net.NetServer;

import

io.vertx.core.net.NetSocket;

/**

*

@author sneaky

*

@since 1.0.0

*/

public

class

MysqlProxyServer

{

private

static

final

Logger

logger

=

LoggerFactory.getLogger(MysqlProxyServer.class);

public

static

void

main(String[]

args)

{

Vertx.vertx().deployVerticle(new

MysqlProxyServerVerticle());

}

public

static

class

MysqlProxyServerVerticle

extends

AbstractVerticle

{

private

final

int

port

=

3306;

private

final

String

mysqlHost

=

“10.10.0.6”;

@Override

public

void

start()

throws

Exception

{

NetServer

netServer

=

vertx.createNetServer();//創建代理伺服器

NetClient

netClient

=

vertx.createNetClient();//創建連接mysql客戶端

netServer.connectHandler(socket

netClient.connect(port,

mysqlHost,

result

{

//響應來自客戶端的連接請求,成功之後,在建立一個與目標mysql伺服器的連接

if

(result.succeeded())

{

//與目標mysql伺服器成功連接連接之後,創造一個MysqlProxyConnection對象,並執行代理方法

new

MysqlProxyConnection(socket,

result.result()).proxy();

原創文章,作者:JV4YB,如若轉載,請註明出處:https://www.506064.com/zh-tw/n/128045.html

(0)
打賞 微信掃一掃 微信掃一掃 支付寶掃一掃 支付寶掃一掃
JV4YB的頭像JV4YB
上一篇 2024-10-03 23:24
下一篇 2024-10-03 23:24

相關推薦

  • 如何修改mysql的埠號

    本文將介紹如何修改mysql的埠號,方便開發者根據實際需求配置對應埠號。 一、為什麼需要修改mysql埠號 默認情況下,mysql使用的埠號是3306。在某些情況下,我們需…

    編程 2025-04-29
  • Java JsonPath 效率優化指南

    本篇文章將深入探討Java JsonPath的效率問題,並提供一些優化方案。 一、JsonPath 簡介 JsonPath是一個可用於從JSON數據中獲取信息的庫。它提供了一種DS…

    編程 2025-04-29
  • java client.getacsresponse 編譯報錯解決方法

    java client.getacsresponse 編譯報錯是Java編程過程中常見的錯誤,常見的原因是代碼的語法錯誤、類庫依賴問題和編譯環境的配置問題。下面將從多個方面進行分析…

    編程 2025-04-29
  • Java騰訊雲音視頻對接

    本文旨在從多個方面詳細闡述Java騰訊雲音視頻對接,提供完整的代碼示例。 一、騰訊雲音視頻介紹 騰訊雲音視頻服務(Cloud Tencent Real-Time Communica…

    編程 2025-04-29
  • Java Bean載入過程

    Java Bean載入過程涉及到類載入器、反射機制和Java虛擬機的執行過程。在本文中,將從這三個方面詳細闡述Java Bean載入的過程。 一、類載入器 類載入器是Java虛擬機…

    編程 2025-04-29
  • Java Milvus SearchParam withoutFields用法介紹

    本文將詳細介紹Java Milvus SearchParam withoutFields的相關知識和用法。 一、什麼是Java Milvus SearchParam without…

    編程 2025-04-29
  • Java 8中某一周的周一

    Java 8是Java語言中的一個版本,於2014年3月18日發布。本文將從多個方面對Java 8中某一周的周一進行詳細的闡述。 一、數組處理 Java 8新特性之一是Stream…

    編程 2025-04-29
  • Java判斷字元串是否存在多個

    本文將從以下幾個方面詳細闡述如何使用Java判斷一個字元串中是否存在多個指定字元: 一、字元串遍歷 字元串是Java編程中非常重要的一種數據類型。要判斷字元串中是否存在多個指定字元…

    編程 2025-04-29
  • VSCode為什麼無法運行Java

    解答:VSCode無法運行Java是因為默認情況下,VSCode並沒有集成Java運行環境,需要手動添加Java運行環境或安裝相關插件才能實現Java代碼的編寫、調試和運行。 一、…

    編程 2025-04-29
  • Java任務下發回滾系統的設計與實現

    本文將介紹一個Java任務下發回滾系統的設計與實現。該系統可以用於執行複雜的任務,包括可回滾的任務,及時恢復任務失敗前的狀態。系統使用Java語言進行開發,可以支持多種類型的任務。…

    編程 2025-04-29

發表回復

登錄後才能評論