㈠ 如何開啟mysql的事務支持
看你是什麼事務,jdbc事務,還是分布式事務,還是容器事務
1,編程式事務管理(jdbc的事務是綁定在connection上的)
Connection conn = null;
try
{
Class.forName("com.mysql.jdbc.Driver");
conn = DriverManager.getConnection("jdbc:oracle:thin:@host:1521:SID","username","password");
conn.setAutoCommit(false); //取消自動提交
PreparedStatement ps = conn.prepareCall("update something");
ResultSet rs = ps.executeQuery();
conn.commit(); //手動提交
}
catch (Exception e)
{
conn.rollback();
e.printStackTrace();
}
finally
{
conn.close();
}
2,聲明式事務
先在工程的application.xml配置文件中添加如下代碼,開啟事務
<!-- 聲明式事務控制配置 -->
<tx:annotation-driven transaction-manager="txManager"/>
<bean id="txManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="datasource" ref="bassDataSource"></property>
</bean>
然後在你需要開啟事務的介面前面添加註解
@Transactional(rollbackFor = IOException.class)
public void add(String name) throws IOException
{
System.out.println("可以再類里和方法裡面添加事務註解0~0");
throw new IOException();
}
直接調用介面方法就好
分布式事務處理(mysql貌似在5.X之後才支持) 的話,
1.可以直接使用spring+atomikos框架進行管理
參考:http://blog.chinaunix.net/uid-21162795-id-3424973.html
就不貼測試代碼了,自己看著配置吧
2,使用JTA(java Transaction API)進行分布式事務管理(測試代碼如下)
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import javax.naming.InitialContext;
import javax.sql.DataSource;
import javax.transaction.SystemException;
import javax.transaction.UserTransaction;
import com.mysql.jdbc.jdbc2.optional.MysqlDataSource;
//分布式事務處理
public class transferAccount
{
@SuppressWarnings("null")
public void testTransferAccount()
{
UserTransaction userts = null;
Connection connA = null;
PreparedStatement psA = null;
InitialContext context = null;
Connection connB = null;
PreparedStatement psB = null;
try
{
//獲得事務管理對象
userts = (UserTransaction) context.lookup("java:comp/UserTransaction");
//獲取兩個資料庫
connA = getDataSourceA().getConnection();
connB = getDataSourceB().getConnection();
//開啟事務
userts.begin();
//sql語句
psA = connA.prepareStatement("我加1");
psB = connB.prepareStatement("我減1");
//執行sql
psA.executeUpdate();
psB.executeUpdate();
//事務提交
userts.commit();
} catch (Exception e)
{
try
{
userts.rollback();
} catch (IllegalStateException | SecurityException
| SystemException e1)
{
e1.printStackTrace();
}
e.printStackTrace();
}
finally
{
try
{
psA.close();
psB.close();
connA.close();
connB.close();
} catch (SQLException e)
{
e.printStackTrace();
}
}
}
public DataSource getDataSourceA()
{
MysqlDataSource dataSource = new MysqlDataSource();
dataSource.setDatabaseName("mysql");
dataSource.setServerName("server");
dataSource.setPortNumber(1433);
dataSource.setUser("test");
dataSource.setPassword("test");
return dataSource;
}
public DataSource getDataSourceB()
{
MysqlDataSource dataSource = new MysqlDataSource();
dataSource.setDatabaseName("mysql");
dataSource.setServerName("server");
dataSource.setPortNumber(1435);
dataSource.setUser("test1");
dataSource.setPassword("test1");
return dataSource;
}
}
㈡ 大數據分析師面試必備:java與mysql解析
【導讀】作為大數據工程師,其必須要掌握的基礎知識就是java與mysql的關系、交互和連接,作為基礎,也是面試考官經常會考的內容,為了幫助大家都能順利通過考試,今天小編就來和大家嘮一嘮java與mysql的關系、交互和連接,好了,開始今天的內容大數據分析師面試必備:java與mysql解析。
1. SQL語言四大類:
DQL 數據查詢語言 select
DML 數據操作語言 insert、update、delete
DDL 數據界說語言 create、alter
DCL 數據控制語言 grant許可權
2. mysql資料庫中的decimal類型(是數值型,不能存放字元串):
舉例:decimal(18,0) 常用於身份證號碼,但是帶x的不可以。
舉例:decimal(5,2)
狀況一:假設小數點前面是3位,後邊是2位,正常狀況。
狀況二:5指的是小數點前後不能超過5位,小數點後有必要是2位。
3. mysql中InnoDB和MyISAM引擎的差異:
innodb支撐:事務和主外鍵
myisam不支撐:事務和主外鍵
4. 【不需要背誦,選擇題考點】向mysql中,a向表中添加數據的幾種寫法,題目:id int 主鍵自增,name varchar(11)
不為空。
5. 操作mysql資料庫表有兩種方式,第一種:點八點吧;第二種:寫代碼。【不需要背誦,只需要了解,考試選擇題會出】
6. 在Java中,簡述面向對象三大特徵。
7. 在Java中,常用關鍵字:
1. 定義類的關鍵字是什麼? class
2. 繼承的關鍵字是什麼? extends
3. 定義介面的關鍵字是什麼? interface
4. 實現介面的關鍵字是什麼? implements
5. 抽象類的關鍵字是什麼? abstract
8. 在Java中,抽象類和介面的區別:
1. 抽象類中可以包含普通方法和抽象方法,介面中只能包含抽象方法
2. 抽象類中可以有構造方法,介面中沒有構造方法
3. 抽象類只能單繼承,可以實現多個介面
9. Java介面中有哪些成員?
1. 構造方法,沒有
2. 常量,默認訪問修飾符public static final,沒有變數
3. 抽象方法,默認訪問修飾符public abstract
10. 在Java中,抽象類和抽象方法的關系:
1. 抽象類中可以包含普通方法和抽象方法,抽象方法一定存在抽象類中。
2. 子類繼承抽象父類,必須實現|重寫抽象方法,除非子類也是抽象類。
3. 【判斷題】抽象類中必須包含抽象方法?【錯誤×】
4. 【判斷題】抽象方法一定存在抽象類中?【正確√】
11. Java重載的特點:
1. 在同一個類中
2. 方法名相同
3. 參數列表(個數、類型、順序)不同
4. 與返回值類型和訪問修飾符無關
12. Java重寫的特點:
1. 在父子類中
2. 方法名相同
3. 參數列表相同
4. 返回值類型相同,或是其子類
5. 訪問修飾符相同,或不能嚴於父類
13. 列舉幾種Java實現多態的形式:
1. 繼承的存在
2. 父類引用指向子類對象 | 向上轉型
3. 父類作為方法的返回值類型,父類作為方法的參數
14. Java介面的特性:單根性和傳遞性
15. 在Java中,throws和throw的區別:
1. throws 聲明異常,用在定義方法小括弧的後面
2. throw 拋出異常,寫在方法體內
以上就是小編今天給大家整理發送的關於大數據分析師面試必備:java與mysql解析的相關內容,希望對各位考生有所幫助,想知道更多關於數據分析師的基本要求有哪些,關注小編持續更新數據分析師崗位解析。
㈢ java操作MYSQL,高手來,怎麼同時執行兩條語句,如果table_1新插入「待發送」,如何同步
private Connection conn = null;
private PreparedStatement ps = null;
try {
conn.setAutoCommit(false); //將自動提交設置為false
ps.executeUpdate("修改SQL"); //執行修改操作
ps.executeQuery("查詢SQL"); //執行查詢操作
conn.commit(); //當兩個操作成功後手動提交
} catch (Exception e) {
conn.rollback(); //一旦其中一個操作出錯都將回滾,使兩個操作都不成功
e.printStackTrace();
}
事務的特性:
1) 原子性(atomicity):事務是資料庫的邏輯工作單位,而且是必須是原子工作單位,對於其數據修改,要麼全部執行,要麼全部不執行。
2) 一致性(consistency):事務在完成時,必須是所有的數據都保持一致狀態。在相關資料庫中,所有規則都必須應用於事務的修改,以保持所有數據的完整性。
3) 隔離性(isolation):一個事務的執行不能被其他事務所影響。
4) 持久性(rability):一個事務一旦提交,事物的操作便永久性的保存在DB中。即使此時再執行回滾操作也不能撤消所做的更改。
㈣ java程序中如何實現對mysql資料庫中表的鎖定
方法1:用mysql命令鎖住表.
publicvoidtest(){
Stringsql="locktablesaa1write";
//或Stringsql="locktablesaa1read";
//如果想鎖多個表locktablesaa1read,aa2write,.....
Stringsql1="select*fromaa1";
Stringsql2="unlocktables";
try{
this.pstmt=conn.prepareStatement(sql);
this.pstmt1=conn.prepareStatement(sql1);
this.pstmt2=conn.prepareStatement(sql2);
pstmt.executeQuery();
pstmt1.executeQuery();
pstmt2.executeQuery();
}catch(Exceptione){
System.out.println("異常"+e.getMessage());
}
}
對於read lock 和 write lock官方說明:
1.如果一個線程獲得一個表的READ鎖定,該線程(和所有其它線程)只能從該表中讀取。
如果一個線程獲得一個表的WRITE鎖定,只有保持鎖定的線程可以對表進行寫入。
其它的線程被阻止,直到鎖定被釋放時為止。
2.當您使用LOCK TABLES時,您必須鎖定您打算在查詢中使用的所有的表。
雖然使用LOCKTABLES語句獲得的鎖定仍然有效,但是您不能訪問沒有被此語句鎖定的任何的表。
同時,您不能在一次查詢中多次使用一個已鎖定的表——使用別名代替,
在此情況下,您必須分別獲得對每個別名的鎖定。
對與read lock 和 write lock個人說明:
1.read lock 和 write lock 是線程級(表級別).
2.在同一個會話中加了read lock鎖. 只能對這個表進行讀操作.對這個表以外的任何錶都無法進行增、刪、改、查的操作.
但是在不同會話中,只能對加了read lock的表進行讀操作.但可以對read lock以外的表進行增、刪、改、查的操作.
3.在同一個會話中加了write lock鎖.只能對這個表進行讀、寫操作.對這個表以外的任何錶都無法進行增、刪、改、查的操作.
但是在不同會話中,無法對加了write lock的表進行讀、寫操作.但可以對write lock以外的表進行增、刪、改、查的操作.
4.如果表中使用了別名.(SELECT * FROM aa1 AS byname_table)
在對aa1加鎖時,必須把別名加上去(lock tables aa1 as byname_table read)
在同一個會話中.必須使用別名進行查詢.
在不同的會話中.可以不需要使用別名進行查詢.
5.在多個會話中可以對同一個表進行lock read操作.但不能在多個會話中對同一個表進行lock write操作(這些鎖將等待已鎖的表釋放自身的線程鎖)
如果多個會話對同一個表進行lock read操作.那麼在這些會話中,也只能對以鎖的表進行讀操作.
6.如果要你鎖住了一個表,需要嵌套查詢.你必須使用別名,並且,要鎖定別名.
例如.lock table aa1 read ,aa1 as byname_table read;
select * from aa1 where id in (select * from aa1 as xxwhere id=2);
7.解鎖必須用unlock tables;
另:
在JAVA程序中,要想解鎖,需要調用 unlock tables來解鎖.
如果沒有調用unlock tables.
關閉connection 、程序結束 、調用GC 都能解鎖.
方法2:用記錄鎖鎖表.
publicvoidtest(){
Stringsql="select*fromaa1forupdate";
//select*fromaa1lockinsharemode;
try{
conn.setAutoCommit(false);
this.pstmt=conn.prepareStatement(sql);
pstmt.executeQuery();
}catch(Exceptione){
System.out.println("異常"+e.getMessage());
}
}
1.for update 與 lock in share mode 屬於行級鎖和頁級鎖
2.for update 排它鎖,lock in share mode 共享鎖
3.對於記錄鎖.必須開啟事務.
4.行級鎖定事實上是索引記錄的鎖定.只要是用索引掃描的行(或沒索引全表掃描的行),都將被鎖住.
5.在不同的隔離級別下還會使用next-key locking演算法.即所掃描的行之間的「間隙」也會也鎖住(在Repeatable read和Serializable隔離級別下有間隙鎖).
6.在mysql中共享鎖的含義是:在被共享鎖鎖住的行,即使內容被修改且並沒有提交.在另一個會話中依然看到最新修改的信息.
在同一會話中加上了共享鎖.可以對這個表以及這個表以外的所有表進行增、刪、改、查的操作.
在不同的會話中.可以查到共享鎖鎖住行的最新消息.但是在Read Uncommitted隔離級別下不能對鎖住的表進行刪,
改操作.(需要等待鎖釋放才能操作...)
在Read Committed隔離級別下不能對鎖住的表進行刪,改操作.(需要等待鎖釋放才能操作...)
在Repeatable read隔離級別下不能對鎖住行進行增、刪、改操作.(需要等待鎖釋放才能操作...)
在Serializable隔離級別下不能對鎖住行進行增、刪、改操作.(需要等待鎖釋放才能操作...)
7.在mysql中排他鎖的含義是:在被排它鎖鎖住的行,內容修改並沒提交,在另一個會話中不會看到最新修改的信息。
在不同的會話中.可以查到共享鎖鎖住行的最新消息.但是Read Uncommitted隔離級別下不能對鎖住的表進行刪,
改操作.(需要等待鎖釋放才能操作...)
在Read Committed隔離級別下不能對鎖住的表進行刪,改操作.(需要等待鎖釋放才能操作...)
在Repeatable read隔離級別下不能對鎖住行進行增、刪、改操作.(需要等待鎖釋放才能操作...)
在Serializable隔離級別下不能對鎖住行進行增、刪、改操作. (需要等待鎖釋放才能操作...)
8.在同一個會話中的可以疊加多個共享鎖和排他鎖.在多個會話中,需要等待鎖的釋放.
9.SQL中的update 與 for update是一樣的原理.
10.等待超時的參數設置:innodb_lock_wait_timeout=50 (單位秒).
11.任何可以觸發事務提交的命令,都可以關閉共享鎖和排它鎖.
㈤ java線程中使用mysql連接查詢資料庫
不建議這樣做,一般不符合開發規范,如果這樣的話,你想想在業務量多的情況下,多個線程如果不控制,資料庫連接會將資料庫伺服器爆掉的,會影響業務的
常規做法:資料庫連接池(rid了解一下),據某些統計哈,真正用來做查詢的資源不超過整個查詢資料庫的生命周期的30%,大部分時間都用開創建連接關閉連接等操作,如果這個時候建立資料庫連接池的話,可以有效的將這部分時間釋放掉
㈥ 如何用java開啟mysql事務,要求詳細
如何用java開啟mysql事務,要求詳細
看你是什麼事務,jdbc事務,還是分布式事務,還是容器事務
1,編程式事務管理(jdbc的事務是綁定在connection上的)
Connection conn = null;
try
{
Class.forName("com.mysql.jdbc.Driver");
conn = DriverManager.getConnection("jdbc:oracle:thin:@host:1521:SID","username","password");
conn.setAutoCommit(false); //取消自動提交
PreparedStatement ps = conn.prepareCall("update something");
ResultSet rs = ps.executeQuery();
conn.commit(); //手動提交
}
catch (Exception e)
{
conn.rollback();
e.printStackTrace();
}
finally
{
conn.close();
}
2,聲明式事務
先在工程的application.xml配置文件中添加如下代碼,開啟事務
<!-- 聲明式事務控制配置 -->
<tx:annotation-driven transaction-manager="txManager"/>
<bean id="txManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="datasource" ref="bassDataSource"></property>
</bean>
然後在你需要開啟事務的介面前面添加註解
@Transactional(rollbackFor = IOException.class)
public void add(String name) throws IOException
{
System.out.println("可以再類里和方法裡面添加事務註解0~0");
throw new IOException();
}
直接調用介面方法就好
分布式事務處理(mysql貌似在5.X之後才支持) 的話,
1.可以直接使用spring+atomikos框架進行管理
參考:http://blog.chinaunix.net/uid-21162795-id-3424973.html
就不貼測試代碼了,自己看著配置吧
2,使用JTA(Java Transaction API)進行分布式事務管理(測試代碼如下)
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import javax.naming.InitialContext;
import javax.sql.DataSource;
import javax.transaction.SystemException;
import javax.transaction.UserTransaction;
import com.mysql.jdbc.jdbc2.optional.MysqlDataSource;
//分布式事務處理
public class transferAccount
{
@SuppressWarnings("null")
public void testTransferAccount()
{
UserTransaction userts = null;
Connection connA = null;
PreparedStatement psA = null;
InitialContext context = null;
Connection connB = null;
PreparedStatement psB = null;
try
{
//獲得事務管理對象
userts = (UserTransaction) context.lookup("java:comp/UserTransaction");
//獲取兩個資料庫
connA = getDataSourceA().getConnection();
connB = getDataSourceB().getConnection();
//開啟事務
userts.begin();
//sql語句
psA = connA.prepareStatement("我加1");
psB = connB.prepareStatement("我減1");
//執行sql
psA.executeUpdate();
psB.executeUpdate();
//事務提交
userts.commit();
} catch (Exception e)
{
try
{
userts.rollback();
} catch (IllegalStateException | SecurityException
| SystemException e1)
{
e1.printStackTrace();
}
e.printStackTrace();
}
finally
{
try
{
psA.close();
psB.close();
connA.close();
connB.close();
} catch (SQLException e)
{
e.printStackTrace();
}
}
}
public DataSource getDataSourceA()
{
MysqlDataSource dataSource = new MysqlDataSource();
dataSource.setDatabaseName("mysql");
dataSource.setServerName("server");
dataSource.setPortNumber(1433);
dataSource.setUser("test");
dataSource.setPassword("test");
return dataSource;
}
public DataSource getDataSourceB()
{
MysqlDataSource dataSource = new MysqlDataSource();
dataSource.setDatabaseName("mysql");
dataSource.setServerName("server");
dataSource.setPortNumber(1435);
dataSource.setUser("test1");
dataSource.setPassword("test1");
return dataSource;
}
}
㈦ java怎麼連接mysql資料庫
這里介紹兩種方式:
一,jdbc鏈接MySQL資料庫:
1,如果你用jdbc方式,則按照下列方式進行連接:
A,注冊驅動
B,鏈接資料庫
C,執行sql
D,返回結果集
如下為一個基本完整流程:
packagecom.hu.demo;
importjava.sql.Connection;
importjava.sql.DriverManager;
importjava.sql.PreparedStatement;
importjava.sql.SQLException;
publicclassDBHelper{
publicstaticfinalStringurl="jdbc:mysql://127.0.0.1/student";
publicstaticfinalStringname="com.mysql.jdbc.Driver";
publicstaticfinalStringuser="root";
="root";
publicConnectionconn=null;
publicPreparedStatementpst=null;
publicDBHelper(Stringsql){
try{
Class.forName(name);//指定連接類型
conn=DriverManager.getConnection(url,user,password);//獲取連接
pst=conn.prepareStatement(sql);//准備執行語句
}catch(Exceptione){
e.printStackTrace();
}
}
publicvoidclose(){
try{
this.conn.close();
this.pst.close();
}catch(SQLExceptione){
e.printStackTrace();
}
}
}
2,將注冊,鏈接封裝好,執行sql語句,返回結果集,代碼如下:
packagecom.hu.demo;
importjava.sql.ResultSet;
importjava.sql.SQLException;
publicclassDemo{
staticStringsql=null;
staticDBHelperdb1=null;
staticResultSetret=null;
publicstaticvoidmain(String[]args){
sql="select*fromstuinfo";//SQL語句
db1=newDBHelper(sql);//創建DBHelper對象
try{
ret=db1.pst.executeQuery();//執行語句,得到結果集
while(ret.next()){
Stringuid=ret.getString(1);
Stringufname=ret.getString(2);
Stringulname=ret.getString(3);
Stringudate=ret.getString(4);
System.out.println(uid+" "+ufname+" "+ulname+" "+udate);
}//顯示數據
ret.close();
db1.close();//關閉連接
}catch(SQLExceptione){
e.printStackTrace();
}
}
}
3,查詢結果如下:
<?xmlversion="1.0"encoding="UTF-8"?>
<beansxmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.1.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-4.1.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-4.1.xsd">
<!--配置數據源-->
<beanname="dataSource"class="com.alibaba.druid.pool.DruidDataSource"
init-method="init"destroy-method="close">
<propertyname="url"value="${jdbc_url}"/>
<propertyname="username"value="${jdbc_username}"/>
<propertyname="password"value="${jdbc_password}"/>
<!--初始化連接大小-->
<propertyname="initialSize"value="0"/>
<!--連接池最大使用連接數量-->
<propertyname="maxActive"value="20"/>
<!--連接池最小空閑-->
<propertyname="minIdle"value="0"/>
<!--獲取連接最大等待時間-->
<propertyname="maxWait"value="60000"/>
<!--<propertyname="poolPreparedStatements"value="true"/><property
name=""value="33"/>-->
<propertyname="validationQuery"value="${validationQuery}"/>
<propertyname="testOnBorrow"value="false"/>
<propertyname="testOnReturn"value="false"/>
<propertyname="testWhileIdle"value="true"/>
<!--配置間隔多久才進行一次檢測,檢測需要關閉的空閑連接,單位是毫秒-->
<propertyname="timeBetweenEvictionRunsMillis"value="60000"/>
<!--配置一個連接在池中最小生存的時間,單位是毫秒-->
<propertyname="minEvictableIdleTimeMillis"value="25200000"/>
<!--打開removeAbandoned功能-->
<propertyname="removeAbandoned"value="true"/>
<!--1800秒,也就是30分鍾-->
<propertyname="removeAbandonedTimeout"value="1800"/>
<!--關閉abanded連接時輸出錯誤日誌-->
<propertyname="logAbandoned"value="true"/>
<!--監控資料庫-->
<!--<propertyname="filters"value="stat"/>-->
<propertyname="filters"value="mergeStat"/>
</bean>
<!--myBatis文件-->
<beanid="sqlSessionFactory"class="org.mybatis.spring.SqlSessionFactoryBean">
<propertyname="dataSource"ref="dataSource"/>
<!--自動掃描entity目錄,省掉Configuration.xml里的手工配置-->
<propertyname="mapperLocations"value="classpath:com/fourfaith/*/mapping/*.xml"/>
</bean>
<beanclass="org.mybatis.spring.mapper.MapperScannerConfigurer">
<propertyname="basePackage"value="com.fourfaith.**."/>
<propertyname="sqlSessionFactoryBeanName"value="sqlSessionFactory"/>
</bean>
<!--配置事務管理器-->
<beanid="transactionManager"
class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<propertyname="dataSource"ref="dataSource"/>
</bean>
<!--攔截器方式配置事物-->
<tx:adviceid="transactionAdvice"transaction-manager="transactionManager">
<tx:attributes>
<tx:methodname="add*"propagation="REQUIRED"/>
<tx:methodname="append*"propagation="REQUIRED"/>
<tx:methodname="insert*"propagation="REQUIRED"/>
<tx:methodname="save*"propagation="REQUIRED"/>
<tx:methodname="update*"propagation="REQUIRED"/>
<tx:methodname="modify*"propagation="REQUIRED"/>
<tx:methodname="edit*"propagation="REQUIRED"/>
<tx:methodname="delete*"propagation="REQUIRED"/>
<tx:methodname="remove*"propagation="REQUIRED"/>
<tx:methodname="repair"propagation="REQUIRED"/>
<tx:methodname="delAndRepair"propagation="REQUIRED"/>
<tx:methodname="import*"propagation="REQUIRED"read-only="false"
rollback-for="java.lang.Exception"/>
<tx:methodname="get*"propagation="SUPPORTS"/>
<tx:methodname="find*"propagation="SUPPORTS"/>
<tx:methodname="load*"propagation="SUPPORTS"/>
<tx:methodname="search*"propagation="SUPPORTS"/>
<tx:methodname="datagrid*"propagation="SUPPORTS"/>
<tx:methodname="*"propagation="SUPPORTS"/>
</tx:attributes>
</tx:advice>
<aop:config>
<aop:pointcutid="transactionPointcut"
expression="execution(*com...*.service..*Impl.*(..))"/>
<aop:advisorpointcut-ref="transactionPointcut"
advice-ref="transactionAdvice"/>
</aop:config>
<!--配置druid監控springjdbc-->
<beanid="druid-stat-interceptor"
class="com.alibaba.druid.support.spring.stat.DruidStatInterceptor">
</bean>
<beanid="druid-stat-pointcut"class="org.springframework.aop.support.JdkRegexpMethodPointcut"
scope="prototype">
<propertyname="patterns">
<list>
<value>com...*.service.*</value>
</list>
</property>
</bean>
<aop:config>
<aop:advisoradvice-ref="druid-stat-interceptor"
pointcut-ref="druid-stat-pointcut"/>
</aop:config>
</beans>
還有很多方式可以實現,這里就簡略的描述一番。
㈧ java mysql中有顯示事務和隱式事務嗎
我只想說一點
java是java
mysqlhi資料庫
關於事務
事務就是事務
哪有顯示和隱式的區分?
我知道到
java中的
顯示調用
和隱式調用
希望能幫到你
謝謝
㈨ java 調用mysql的事務如何傳入查詢參數
Statement換成preparedStatement,就有相應的set方法了。
或者既然addBatch裡面傳入的是String類型,那我們自己構造,
stmt.addBatch(「insertintousersvalues("+"values1"+","+"values2"+");");
或者既然是users類,我們可以根據users中屬性是否為初始值來自動生成inset語句,下面是我以前寫的代碼,僅供參考
注釋:
1、下面的User.NAME等就是user中name屬性在表中的列名
2、方法ConvertStr就是把插入的列的value加上單引號。
privatestaticStringConvertStr(Objectobject){
return"'"+object.toString()+"'";
}
3、其他
publicstaticfinalStringstrIns="insertintousers(";
publicstaticfinalStringstrVal=")values(";
publicstaticfinalStringstrEnd=");";
4、調用
stmt.addBatch(User.InsetStr(user));//這樣就不用考慮傳參了
代碼如下:
public static String InsetStr(User user) {
String StrCol = "";
String Values = "";
if (user.getName() != null && !user.getName().equals("")) {
if (!StrCol.equals("")) {
StrCol = StrCol + "," + User.NAME;
Values = Values + "," + ConvertStr(user.getName());
} else {
StrCol = StrCol + User.NAME;
Values = Values + ConvertStr(user.getName());
}
}
if (user.getNick() != null && !user.getNick().equals("")) {
if (!StrCol.equals("")) {
StrCol = StrCol + "," + User.NICK;
Values = Values + "," + ConvertStr(user.getNick());
} else {
StrCol = StrCol + User.NICK;
Values = Values + ConvertStr(user.getNick());
}
}
if (user.getStudentid() != 0) {
if (!StrCol.equals("")) {
StrCol = StrCol + "," + User.STUDENTID;
Values = Values + "," + ConvertStr(user.getStudentid());
} else {
StrCol = StrCol + User.STUDENTID;
Values = Values + ConvertStr(user.getStudentid());
}
}
if (user.getSex() != 'u0000') {
if (!StrCol.equals("")) {
StrCol = StrCol + "," + User.SEX;
Values = Values + "," + ConvertStr(user.getSex());
} else {
StrCol = StrCol + User.SEX;
Values = Values + ConvertStr(user.getSex());
}
}
if (user.getPassword() != null && !user.getPassword().equals("")) {
if (!StrCol.equals("")) {
StrCol = StrCol + "," + User.PASSWORD;
Values = Values + "," + ConvertStr(user.getPassword());
} else {
StrCol = StrCol + User.PASSWORD;
Values = Values + ConvertStr(user.getPassword());
}
}
if (user.getHash() != null && !user.getHash().equals("")) {
if (!StrCol.equals("")) {
StrCol = StrCol + "," + User.HASH;
Values = Values + "," + ConvertStr(user.getHash());
} else {
StrCol = StrCol + User.HASH;
Values = Values + ConvertStr(user.getHash());
}
}
if (user.getSchool() != null && !user.getSchool().equals("")) {
if (!StrCol.equals("")) {
StrCol = StrCol + "," + User.SCHOOL;
Values = Values + "," + ConvertStr(user.getSchool());
} else {
StrCol = StrCol + User.SCHOOL;
Values = Values + ConvertStr(user.getSchool());
}
}
if (user.getMajor() != null && !user.getMajor().equals("")) {
if (!StrCol.equals("")) {
StrCol = StrCol + "," + User.MAJOR;
Values = Values + "," + ConvertStr(user.getMajor());
} else {
StrCol = StrCol + User.MAJOR;
Values = Values + ConvertStr(user.getMajor());
}
}
if (user.getMobile() != 0) {
if (!StrCol.equals("")) {
StrCol = StrCol + "," + User.MOBILE;
Values = Values + "," + ConvertStr(user.getMobile());
} else {
StrCol = StrCol + User.MOBILE;
Values = Values + ConvertStr(user.getMobile());
}
}
if (user.getCollege() != null && !user.getCollege().equals("")) {
if (!StrCol.equals("")) {
StrCol = StrCol + "," + User.COLLEGE;
Values = Values + "," + ConvertStr(user.getCollege());
} else {
StrCol = StrCol + User.COLLEGE;
Values = Values + ConvertStr(user.getCollege());
}
}
if (user.getGrade() != 0) {
if (!StrCol.equals("")) {
StrCol = StrCol + "," + User.GRADE;
Values = Values + "," + ConvertStr(user.getGrade());
} else {
StrCol = StrCol + User.GRADE;
Values = Values + ConvertStr(user.getGrade());
}
}
if (user.getBclass() != null && !user.getBclass().equals("")) {
if (!StrCol.equals("")) {
StrCol = StrCol + "," + User.BCLASS;
Values = Values + "," + ConvertStr(user.getBclass());
} else {
StrCol = StrCol + User.BCLASS;
Values = Values + ConvertStr(user.getBclass());
}
}
if (user.getIdnum() != null && !user.getIdnum().equals("")) {
if (!StrCol.equals("")) {
StrCol = StrCol + "," + User.IDNUM;
Values = Values + "," + ConvertStr(user.getIdnum());
} else {
StrCol = StrCol + User.IDNUM;
Values = Values + ConvertStr(user.getIdnum());
}
}
if (user.getEmail() != null && !user.getEmail().equals("")) {
if (!StrCol.equals("")) {
StrCol = StrCol + "," + User.EMAIL;
Values = Values + "," + ConvertStr(user.getEmail());
} else {
StrCol = StrCol + User.EMAIL;
Values = Values + ConvertStr(user.getEmail());
}
}
if (user.getRegip() != null && !user.getRegip().equals("")) {
if (!StrCol.equals("")) {
StrCol = StrCol + "," + User.REGIP;
Values = Values + "," + ConvertStr(user.getRegip());
} else {
StrCol = StrCol + User.REGIP;
Values = Values + ConvertStr(user.getRegip());
}
}
if (user.getRegdate() != 0) {
if (!StrCol.equals("")) {
StrCol = StrCol + "," + User.REGDATE;
Values = Values + "," + ConvertStr(user.getRegdate());
} else {
StrCol = StrCol + User.REGDATE;
Values = Values + ConvertStr(user.getRegdate());
}
}
if (user.getUnit() != null && !user.getUnit().equals("")) {
if (!StrCol.equals("")) {
StrCol = StrCol + "," + User.UNIT;
Values = Values + "," + ConvertStr(user.getUnit());
} else {
StrCol = StrCol + User.UNIT;
Values = Values + ConvertStr(user.getUnit());
}
}
if (user.getRegion() != null && !user.getRegion().equals("")) {
if (!StrCol.equals("")) {
StrCol = StrCol + "," + User.REGION;
Values = Values + "," + ConvertStr(user.getRegion());
} else {
StrCol = StrCol + User.REGION;
Values = Values + ConvertStr(user.getRegion());
}
}
if (user.getDepartments() != null && !user.getDepartments().equals("")) {
if (!StrCol.equals("")) {
StrCol = StrCol + "," + User.DEPARTMENTS;
Values = Values + "," + ConvertStr(user.getDepartments());
} else {
StrCol = StrCol + User.DEPARTMENTS;
Values = Values + ConvertStr(user.getDepartments());
}
}
if (user.getTdcj() > 0) {
if (!StrCol.equals("")) {
StrCol = StrCol + "," + User.TDCJ;
Values = Values + "," + ConvertStr(user.getTdcj());
} else {
StrCol = StrCol + User.TDCJ;
Values = Values + ConvertStr(user.getTdcj());
}
}
if (user.getTzcj() > 0) {
if (!StrCol.equals("")) {
StrCol = StrCol + "," + User.TZCJ;
Values = Values + "," + ConvertStr(user.getTzcj());
} else {
StrCol = StrCol + User.TZCJ;
Values = Values + ConvertStr(user.getTzcj());
}
}
if (user.getAddress() != null && !user.getAddress().equals("")) {
if (!StrCol.equals("")) {
StrCol = StrCol + "," + User.ADDRESS;
Values = Values + "," + ConvertStr(user.getAddress());
} else {
StrCol = StrCol + User.ADDRESS;
Values = Values + ConvertStr(user.getAddress());
}
}
if (user.getPostcode() != 0) {
if (!StrCol.equals("")) {
StrCol = StrCol + "," + User.POSTCODE;
Values = Values + "," + ConvertStr(user.getPostcode());
} else {
StrCol = StrCol + User.POSTCODE;
Values = Values + ConvertStr(user.getPostcode());
}
}
if (user.getBankcard() != 0) {
if (!StrCol.equals("")) {
StrCol = StrCol + "," + User.BANKCARD;
Values = Values + "," + ConvertStr(user.getBankcard());
} else {
StrCol = StrCol + User.BANKCARD;
Values = Values + ConvertStr(user.getBankcard());
}
}
return Sql.strIns + StrCol + Sql.strVal + Values + Sql.strEnd;
}
㈩ 怎麼用java實現mysql的復制資料庫里所有的表跟數據
樓主要考慮的不僅僅是標題的需求。
1、復制資料庫里所有的表和數據的目的是什麼。
a、假設樓主是要做資料庫備份的話,且通過程序來做的話,可以使用程序來執行dos命令
如java:Runtime.getRuntime().exec("e:\\MySQL\\bin\\mysqlmp -h localhost -uroot -p123 db_name")
b、假設樓主是要做庫與庫之間的同步的話,可以使用第三方客戶端進行,比如navicat,sqlyong等
c、假設樓主是要做庫與庫之間的同步且用程序進行的話,可以使用mysql中提供操作資料庫的api來做相對應的讀取工作和對比工作,然後寫入工作