导航:首页 > 编程语言 > javamysql事务

javamysql事务

发布时间:2022-08-04 06:03:50

㈠ 如何开启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来做相对应的读取工作和对比工作,然后写入工作

阅读全文

与javamysql事务相关的资料

热点内容
闽政通无法请求服务器是什么 浏览:48
怎么做积木解压神器 浏览:203
王者荣耀解压玩具抽奖 浏览:49
12位是由啥加密的 浏览:868
程序员编迷你世界代码 浏览:895
php取现在时间 浏览:246
单片机高吸收 浏览:427
怎么区分五代头是不是加密喷头 浏览:244
hunt测试服务器是什么意思 浏览:510
2013程序员考试 浏览:641
毕业论文是pdf 浏览:736
服务器跑网心云划算吗 浏览:471
单片机定时器计数初值的计算公式 浏览:801
win7控制台命令 浏览:567
猫咪成年app怎么升级 浏览:692
360有没有加密软件 浏览:315
清除cisco交换机配置命令 浏览:751
华为删除交换机配置命令 浏览:473
shell打包命令 浏览:827
加密狗插上输不了密码 浏览:187