本文目錄一覽:
- 1、如何用java實現mysql數據庫的導入導出
- 2、怎麼樣實現java與MYSQL的連接?
- 3、怎麼用java實現mysql的複製數據庫里所有的表跟數據
- 4、如何用java 5分鐘實現一個最簡單的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-hant/n/128045.html