淘宝(阿里百川)手机客户端开发日记第十三篇 mysql的连接
首先,我建立了一个包,里面存放了三个类文件,这三个文件是我从网络中找的,经过自己的整理。(我刚才查找想把这三个文件传上去,可能是自己对cnblogs的博客不太熟悉吧,没有找到,我只好粘贴代码了)

三个类的源文件:
ConnectionPool.java
package com.yushengbo.db.util;
/*
这个例子是根据POSTGRESQL数据库写的,
请用的时候根据实际的数据库调整。
调用方法如下:
① ConnectionPool connPool = new ConnectionPool("com.microsoft.jdbc.sqlserver.SQLServerDriver",
"jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=MyDataForTest"
,"Username"
,"Password");
② connPool.createPool();
Connection conn = connPool .getConnection();
connPool.returnConnection(conn);
connPool.refreshConnections();
connPool.closeConnectionPool();
*/
import java.sql.Connection;
import java.sql.DatabaseMetaData;
import java.sql.Driver;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Enumeration;
import java.util.Vector;
public class ConnectionPool {
private String jdbcDriver = "org.gjt.mm.mysql.Driver"; // 数据库驱动
private String dbUrl = ""; // 数据 URL
private String dbUsername = ""; // 数据库用户名
private String dbPassword = ""; // 数据库用户密码
private String testTable = ""; // 测试连接是否可用的测试表名,默认没有测试表
private int initialConnections = 10; // 连接池的初始大小
private int incrementalConnections = 5;// 连接池自动增加的大小
private int maxConnections = 50; // 连接池最大的大小
private Vector connections = null; // 存放连接池中数据库连接的向量 , 初始时为 null
/*
* jdbcDriver :JDBC 驱动类串
* dbUrl 数据库 URL
* dbUsername 连接数据库用户名
* dbPassword 连接数据库用户的密码
*/
public ConnectionPool(String jdbcDriver, String dbUrl, String dbUsername,
String dbPassword) {
this.jdbcDriver = jdbcDriver;
this.dbUrl = dbUrl;
this.dbUsername = dbUsername;
this.dbPassword = dbPassword;
}
/**
* 返回连接池的初始大小
*
* @return 初始连接池中可获得的连接数量
*/
public int getInitialConnections() {
return this.initialConnections;
}
/**
* 设置连接池的初始大小
*
* @param 用于设置初始连接池中连接的数量
*/
public void setInitialConnections(int initialConnections) {
this.initialConnections = initialConnections;
}
/**
* 返回连接池自动增加的大小 、
*
* @return 连接池自动增加的大小
*/
public int getIncrementalConnections() {
return this.incrementalConnections;
}
/**
* 设置连接池自动增加的大小
*
* @param 连接池自动增加的大小
*/
public void setIncrementalConnections(int incrementalConnections) {
this.incrementalConnections = incrementalConnections;
}
/**
* 返回连接池中最大的可用连接数量
*
* @return 连接池中最大的可用连接数量
*/
public int getMaxConnections() {
return this.maxConnections;
}
/**
* 设置连接池中最大可用的连接数量
*
* @param 设置连接池中最大可用的连接数量值
*/
public void setMaxConnections(int maxConnections) {
this.maxConnections = maxConnections;
}
/**
* 获取测试数据库表的名字
*
* @return 测试数据库表的名字
*/
public String getTestTable() {
return this.testTable;
}
/**
* 设置测试表的名字
*
* @param testTable
* String 测试表的名字
*/
public void setTestTable(String testTable) {
this.testTable = testTable;
}
/**
*
* 创建一个数据库连接池,连接池中的可用连接的数量采用类成员 initialConnections 中设置的值
*/
public synchronized void createPool() throws Exception {
// 确保连接池没有创建
// 如果连接池己经创建了,保存连接的向量 connections 不会为空
if (connections != null) {
return; // 如果己经创建,则返回
}
// 实例化 JDBC Driver 中指定的驱动类实例
Driver driver = (Driver) (Class.forName(this.jdbcDriver).newInstance());
DriverManager.registerDriver(driver); // 注册 JDBC 驱动程序
// 创建保存连接的向量 , 初始时有 0 个元素
connections = new Vector();
// 根据 initialConnections 中设置的值,创建连接。
createConnections(this.initialConnections);
// System.out.println(" 数据库连接池创建成功! ");
}
/**
* 创建由 numConnections 指定数目的数据库连接 , 并把这些连接 放入 connections 向量中
*
* @param numConnections
* 要创建的数据库连接的数目
*/
@SuppressWarnings("unchecked")
private void createConnections(int numConnections) throws SQLException {
// 循环创建指定数目的数据库连接
for (int x = 0; x < numConnections; x++) {
// 是否连接池中的数据库连接的数量己经达到最大?最大值由类成员 maxConnections
// 指出,如果 maxConnections 为 0 或负数,表示连接数量没有限制。
// 如果连接数己经达到最大,即退出。
if (this.maxConnections > 0
&& this.connections.size() >= this.maxConnections) {
break;
}
// add a new PooledConnection object to connections vector
// 增加一个连接到连接池中(向量 connections 中)
try {
connections.addElement(new PooledConnection(newConnection()));
} catch (SQLException e) {
System.out.println(" 创建数据库连接失败! " + e.getMessage());
throw new SQLException();
}
// System.out.println(" 数据库连接己创建 ......");
}
}
/**
* 创建一个新的数据库连接并返回它
*
* @return 返回一个新创建的数据库连接
*/
private Connection newConnection() throws SQLException {
// 创建一个数据库连接
Connection conn = DriverManager.getConnection(dbUrl, dbUsername,
dbPassword);
// 如果这是第一次创建数据库连接,即检查数据库,获得此数据库允许支持的
// 最大客户连接数目
// connections.size()==0 表示目前没有连接己被创建
if (connections.size() == 0) {
DatabaseMetaData metaData = conn.getMetaData();
int driverMaxConnections = metaData.getMaxConnections();
// 数据库返回的 driverMaxConnections 若为 0 ,表示此数据库没有最大
// 连接限制,或数据库的最大连接限制不知道
// driverMaxConnections 为返回的一个整数,表示此数据库允许客户连接的数目
// 如果连接池中设置的最大连接数量大于数据库允许的连接数目 , 则置连接池的最大
// 连接数目为数据库允许的最大数目
if (driverMaxConnections > 0
&& this.maxConnections > driverMaxConnections) {
this.maxConnections = driverMaxConnections;
}
}
return conn; // 返回创建的新的数据库连接
}
/**
* 通过调用 getFreeConnection() 函数返回一个可用的数据库连接 , 如果当前没有可用的数据库连接,并且更多的数据库连接不能创
* 建(如连接池大小的限制),此函数等待一会再尝试获取。
*
* @return 返回一个可用的数据库连接对象
*/
public synchronized Connection getConnection() throws SQLException {
// 确保连接池己被创建
if (connections == null) {
return null; // 连接池还没创建,则返回 null
}
Connection conn = getFreeConnection(); // 获得一个可用的数据库连接
// 如果目前没有可以使用的连接,即所有的连接都在使用中
while (conn == null) {
// 等一会再试
// System.out.println("Wait");
wait(250);
conn = getFreeConnection(); // 重新再试,直到获得可用的连接,如果
// getFreeConnection() 返回的为 null
// 则表明创建一批连接后也不可获得可用连接
}
return conn;// 返回获得的可用的连接
}
/**
* 本函数从连接池向量 connections 中返回一个可用的的数据库连接,如果 当前没有可用的数据库连接,本函数则根据
* incrementalConnections 设置 的值创建几个数据库连接,并放入连接池中。 如果创建后,所有的连接仍都在使用中,则返回 null
*
* @return 返回一个可用的数据库连接
*/
private Connection getFreeConnection() throws SQLException {
// 从连接池中获得一个可用的数据库连接
Connection conn = findFreeConnection();
if (conn == null) {
// 如果目前连接池中没有可用的连接
// 创建一些连接
createConnections(incrementalConnections);
// 重新从池中查找是否有可用连接
conn = findFreeConnection();
if (conn == null) {
// 如果创建连接后仍获得不到可用的连接,则返回 null
return null;
}
}
return conn;
}
/**
* 查找连接池中所有的连接,查找一个可用的数据库连接, 如果没有可用的连接,返回 null
*
* @return 返回一个可用的数据库连接
*/
private Connection findFreeConnection() throws SQLException {
Connection conn = null;
PooledConnection pConn = null;
// 获得连接池向量中所有的对象
Enumeration enumerate = connections.elements();
// 遍历所有的对象,看是否有可用的连接
while (enumerate.hasMoreElements()) {
pConn = (PooledConnection) enumerate.nextElement();
if (!pConn.isBusy()) {
// 如果此对象不忙,则获得它的数据库连接并把它设为忙
conn = pConn.getConnection();
pConn.setBusy(true);
// 测试此连接是否可用
if (!testConnection(conn)) {
// 如果此连接不可再用了,则创建一个新的连接,
// 并替换此不可用的连接对象,如果创建失败,返回 null
try {
conn = newConnection();
} catch (SQLException e) {
System.out.println(" 创建数据库连接失败! " + e.getMessage());
return null;
}
pConn.setConnection(conn);
}
break; // 己经找到一个可用的连接,退出
}
}
return conn;// 返回找到到的可用连接
}
/**
* 测试一个连接是否可用,如果不可用,关掉它并返回 false 否则可用返回 true
*
* @param conn
* 需要测试的数据库连接
* @return 返回 true 表示此连接可用, false 表示不可用
*/
private boolean testConnection(Connection conn) {
try {
// 判断测试表是否存在
if (testTable.equals("")) {
// 如果测试表为空,试着使用此连接的 setAutoCommit() 方法
// 来判断连接否可用(此方法只在部分数据库可用,如果不可用 ,
// 抛出异常)。注意:使用测试表的方法更可靠
conn.setAutoCommit(true);
} else {// 有测试表的时候使用测试表测试
// check if this connection is valid
Statement stmt = conn.createStatement();
stmt.execute("select count(*) from " + testTable);
}
} catch (SQLException e) {
// 上面抛出异常,此连接己不可用,关闭它,并返回 false;
closeConnection(conn);
return false;
}
// 连接可用,返回 true
return true;
}
/**
* 此函数返回一个数据库连接到连接池中,并把此连接置为空闲。 所有使用连接池获得的数据库连接均应在不使用此连接时返回它。
*
* @param 需返回到连接池中的连接对象
*/
public void returnConnection(Connection conn) {
// 确保连接池存在,如果连接没有创建(不存在),直接返回
if (connections == null) {
System.out.println(" 连接池不存在,无法返回此连接到连接池中 !");
return;
}
PooledConnection pConn = null;
Enumeration enumerate = connections.elements();
// 遍历连接池中的所有连接,找到这个要返回的连接对象
while (enumerate.hasMoreElements()) {
pConn = (PooledConnection) enumerate.nextElement();
// 先找到连接池中的要返回的连接对象
if (conn == pConn.getConnection()) {
// 找到了 , 设置此连接为空闲状态
pConn.setBusy(false);
break;
}
}
}
/**
* 刷新连接池中所有的连接对象
*
*/
public synchronized void refreshConnections() throws SQLException {
// 确保连接池己创新存在
if (connections == null) {
System.out.println(" 连接池不存在,无法刷新 !");
return;
}
PooledConnection pConn = null;
Enumeration enumerate = connections.elements();
while (enumerate.hasMoreElements()) {
// 获得一个连接对象
pConn = (PooledConnection) enumerate.nextElement();
// 如果对象忙则等 5 秒 ,5 秒后直接刷新
if (pConn.isBusy()) {
wait(5000); // 等 5 秒
}
// 关闭此连接,用一个新的连接代替它。
closeConnection(pConn.getConnection());
pConn.setConnection(newConnection());
pConn.setBusy(false);
}
}
/**
* 关闭连接池中所有的连接,并清空连接池。
*/
public synchronized void closeConnectionPool() throws SQLException {
// 确保连接池存在,如果不存在,返回
if (connections == null) {
System.out.println(" 连接池不存在,无法关闭 !");
return;
}
PooledConnection pConn = null;
Enumeration enumerate = connections.elements();
while (enumerate.hasMoreElements()) {
pConn = (PooledConnection) enumerate.nextElement();
// 如果忙,等 5 秒
if (pConn.isBusy()) {
wait(5000); // 等 5 秒
}
// 5 秒后直接关闭它
closeConnection(pConn.getConnection());
// 从连接池向量中删除它
connections.removeElement(pConn);
}
// 置连接池为空
connections = null;
}
/**
* 关闭一个数据库连接
*
* @param 需要关闭的数据库连接
*/
private void closeConnection(Connection conn) {
try {
conn.close();
} catch (SQLException e) {
System.out.println(" 关闭数据库连接出错: " + e.getMessage());
}
}
/**
* 使程序等待给定的毫秒数
*
* @param 给定的毫秒数
*/
private void wait(int mSeconds) {
try {
Thread.sleep(mSeconds);
} catch (InterruptedException e) {
}
}
/**
*
* 内部使用的用于保存连接池中连接对象的类 此类中有两个成员,一个是数据库的连接,另一个是指示此连接是否 正在使用的标志。
*/
class PooledConnection {
Connection connection = null;// 数据库连接
boolean busy = false; // 此连接是否正在使用的标志,默认没有正在使用
// 构造函数,根据一个 Connection 构告一个 PooledConnection 对象
public PooledConnection(Connection connection) {
this.connection = connection;
}
// 返回此对象中的连接
public Connection getConnection() {
return connection;
}
// 设置此对象的,连接
public void setConnection(Connection connection) {
this.connection = connection;
}
// 获得对象连接是否忙
public boolean isBusy() {
return busy;
}
// 设置对象的连接正在忙
public void setBusy(boolean busy) {
this.busy = busy;
}
}
}
ConnectionPoolUtils.java
package com.yushengbo.db.util;
/*连接池工具类,返回唯一的一个数据库连接池对象*/
public class ConnectionPoolUtils {
private static ConnectionPool poolInstance = null;
public static ConnectionPool GetPoolInstance(){
if(poolInstance == null) {
poolInstance = new ConnectionPool(
//"com.microsoft.jdbc.sqlserver.SQLServerDriver",
"com.mysql.jdbc.Driver",
//"jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=SuperMarketInfo",
"jdbc:mysql://127.0.0.1:3306/appdb?user=root&password=123456&useUnicode=true&characterEncoding=gbk",
//"jdbc:sqlserver://localhost;database=SuperMarketInfo;IntegratedSecurity=false;",
"root", "123456");
try {
poolInstance.createPool();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
return poolInstance;
}
}
DbHelper.java
package com.yushengbo.db.util;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
public class DbHelper {
private Connection conn = null;
private Statement stmt = null;
ResultSet rs = null;
private ConnectionPool connPool = null; /* 数据库连接池对象 */
public DbHelper() {
connPool = ConnectionPoolUtils.GetPoolInstance();
}
public ResultSet executeQuery(String sql) throws Exception {
try {
conn = connPool.getConnection();
stmt = conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_UPDATABLE);
rs = stmt.executeQuery(sql);
} catch (SQLException e) {
throw e;
}
return rs;
}
// 执行Insert,Update语句
public int executeUpdate(String sql) throws Exception {
int result = 0;
try {
conn = connPool.getConnection();
stmt = conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,
ResultSet.CONCUR_UPDATABLE);
result = stmt.executeUpdate(sql);
} catch (SQLException ex) {
System.err.println("执行SQL语句出错: " + ex.getMessage());
}
return result;
}
/* 根据查询的sql语句得到符合条件的记录条数 */
public int getRecordCount(String sqlString) throws Exception {
int recordCount = 0;
try {
conn = connPool.getConnection();
stmt = conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,
ResultSet.CONCUR_UPDATABLE);
rs = stmt.executeQuery(sqlString);
rs.next();
recordCount = rs.getInt(1);
} catch (SQLException e) {
throw e;
}
return recordCount;
}
/* 根据查询sql语句得到第一行第一列的数据 */
public String GetScalarString(String sqlString) {
String result = "";
try {
conn = connPool.getConnection();
stmt = conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,
ResultSet.CONCUR_UPDATABLE);
rs = stmt.executeQuery(sqlString);
// if(rs.next())
if (rs.next())
result = rs.getString(1);
} catch (SQLException e) {
e.printStackTrace();
}
return result;
}
/* 执行事务操作:几条sql更新语句组成一个事务 */
public boolean excuteSqlStrings(String[] sqlStrings) {
Connection conn = null;
Statement stmt = null;
boolean flag = false;
try {
conn = connPool.getConnection();
stmt = conn.createStatement();
conn.setAutoCommit(false);
for(int i=0;i<sqlStrings.length;i++) {
stmt.addBatch(sqlStrings[i]);
}
stmt.executeBatch();
conn.commit();
flag = true;
} catch (SQLException e) {
try {
conn.rollback();
} catch (SQLException e1) {
}
e.printStackTrace();
} finally {
this.all_close();
}
return flag;
}
// 关闭stmt和关闭连接
public void all_close() {
try {
if (rs != null) {
rs.close();
rs = null;
}
if (stmt != null) {
stmt.close();
stmt = null;
}
if (conn != null) {
connPool.returnConnection(conn);
conn = null;
}
} catch (SQLException e) {
e.printStackTrace();
}
}
}
我建立了实体bean,以及数据访问层:

Category.java
package com.yushengbo.bean;
public class Category {
private int id; // 标识
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
private String name; // 标识
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
CategoryDao.java
package com.yushengbo.dao;
import java.sql.ResultSet;
import java.util.ArrayList;
import com.yushengbo.bean.Category;
import com.yushengbo.db.util.DbHelper;
public class CategoryDao {
/*得到所有的用户信息*/
public static ArrayList<Category> QueryAllAdminInfo() {
String sqlString = "select * from category";
ArrayList adminList = new ArrayList();
DbHelper db = new DbHelper();
ResultSet rs;
try {
rs = db.executeQuery(sqlString);
while (rs.next()) {
Category category = new Category();
category.setName(rs.getString("name"));
category.setId(rs.getInt("id"));
adminList.add(category);
}
} catch (Exception e) {
e.printStackTrace();
} finally {
db.all_close();
}
return adminList;
}
}
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%@ page import="com.yushengbo.dao.CategoryDao" %>
<%@ page import="com.yushengbo.bean.Category" %>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>My JSP 'index.jsp' starting page</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
</head>
<body>
-------------:
<%
ArrayList list = (ArrayList)CategoryDao.QueryAllAdminInfo();
for(int i=0;i< list.size();i++){
Category model = (Category)list.get(i);
%>
ID = <%= model.getId() %> , Name = <%= model.getName() %><br />
<%}%>
</body>
</html>
运行:

转载请注明http://www.cnblogs.com/yushengbo,否则将追究版权责任!
淘宝(阿里百川)手机客户端开发日记第十三篇 mysql的连接的更多相关文章
- 淘宝(阿里百川)手机客户端开发日记第十一篇 JSP+Servlet
由于本人从事.net开发已有多年经验,今天由于工作需要,我只能学习下JSP+Servlet,至于java web提供了更好的开发框架MVC,现在由于时间关系,我只好用JSP+Servlet来搭建服务器 ...
- 淘宝(阿里百川)手机客户端开发日记第五篇 SharedPreferences使用详解
我们知道,Android中数据存储技术由于如下几种 1 使用SharedPreferences存储数据 2 文件存储数据 3 SQLite数据库存储数据 4 使用ContentProvider存储数据 ...
- 淘宝(阿里百川)手机客户端开发日记第十篇 阿里百川服务器环境介绍之API文档的快速链接(四)
个人感觉比较重要的快速链接: http://open.taobao.com/doc/detail.htm?id=102513 http://open.taobao.com/doc/detail.htm ...
- 淘宝(阿里百川)手机客户端开发日记第八篇 Handler的使用方法
首先,我们先看下API文档的说明: A Handler allows you to send and process Message and Runnable objects associated w ...
- 淘宝(阿里百川)手机客户端开发日记第七篇 Service,Handler和Thread
现在我们已经已经知道android有Service,Handler和Thread这些内容了,但是我想应该还有很多人对此并不是很清楚他们之间的区别! (1)Service 是运行在后端的程序,不与UI直 ...
- 淘宝(阿里百川)手机客户端开发日记第六篇 Service详解(六)
Service和Thread的关系 不少初学者都可能会有这样的疑惑,Service和Thread到底有什么关系呢?什么时候应该用Service,什么时候又应该用Thread? 答案是Service和T ...
- 淘宝(阿里百川)手机客户端开发日记第六篇 Service详解(五)
我们现在对上一节中的DEMO进行改进,在服务中开启线程来执行. package com.example.service; import android.app.Service; import andr ...
- 淘宝(阿里百川)手机客户端开发日记第六篇 Service详解(四)
DEMO1:在Activity里声明一个回调方法,当service完成任务后,调用这个回调方法. 首先,我们先继承service,来创建服务,代码如下: package com.example.ser ...
- 淘宝(阿里百川)手机客户端开发日记第六篇 Service详解(三)
主题:Service与Activity交互通信 问题的引出:现在有个需求,如果我们有一个下载任务,下载时间耗时比较长,并且当下载完毕后,需要更新UI的内容,这时,service中的bindServic ...
随机推荐
- 关于 客户端发现响应内容类型为“text/html; charset=utf-8”,但应为“text/xml”的解决方法
http://www.cnblogs.com/jams742003/archive/2008/10/30/1322761.html 请求web服务时,会有如题的异常出现,解决方法如下: 1 检查web ...
- web项目中的跨域问题解决方法
一种是JSONP 一种是 CORS. 在客户端Javascript调用服务端接口的时候,如果需要支持跨域的话,需要服务端支持. JSONP的方式就是服务端对返回的值进行回调函数包装,他的优点是支持众多 ...
- 用LinkedList模拟栈数据结构的集合
用Eclipse软件进行操作 有2种方法,左边为第一种,右边为第二种 创建class为MyStack 代码实现: package cn_LinkedList; import java.uti ...
- IIS------配置错误:不能在此路径中使用此配置节
转载: http://wenda.so.com/q/1414673956725716 因为 IIS 采用了更安全的 web.config 管理机制,默认情况下会锁住配置项不允许更改.运行命令行 %wi ...
- beautifulsoup测试
import re from bs4 import BeautifulSoup html_doc = """ <html><head><ti ...
- JAVA 5.17习题
1.编写并测试一个代表地址的Address类,地址信息由国家.省份.城市.街道.邮编组成,并可以返回完整的地址信息. //======================================= ...
- notification的使用
示例: NotificationManager nm = (NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE); No ...
- python 常见排序实例
使用Python 基础排序算法设计,冒泡排序,插入排序,快速排序... 需求 对一组无序数据进行排序算法设计,要求如下: 输入:[1, 3, 5, 23, 75, 34, 456, 86, 22, 7 ...
- Effective Objective-C 2.0 — 第三条:多用字面量语法,少用与之等价的方法
第三条:多用字面量语法,少用与之等价的方法 几个类:NSString NSNumber NSArray NSDictionary 字面量语法是一种语法糖(syntactic sugar) NSS ...
- python 计算器的(正则匹配+递归)
经过2天的长时间的战斗,python计算器终于完成了. import re val="1-2*((60-30*(9-2*5/3+7/3*99/4*2998+10*568/14))-(-4*3 ...