澳门金沙vip 2

澳门金沙vipJDBC在springMvc等框架中使用的方式

生龙活虎、剖断数据库表是或不是存在:
首先要获得数据库连接conn,调用DatabaseMetaData dbmd =
conn.getDataMeta(卡塔尔国;之后调用如下方法: 复制代码 代码如下: /** *
依照表名,判别数据库表是不是存在 * @param tableName * @return
true:存在该表,false:不设有该表 */ public boolean hasTable(String
tableNameState of Qatar { Init(卡塔尔(قطر‎; boolean result = false; //剖断某二个表是或不是留存 try{
ResultSet set = dbmd.getTables (null, null, tableName, null卡塔尔国;
//获取查找结果 while (set.next(卡塔尔(قطر‎卡塔尔国 { //假诺查找结果不为空,则印证存在该表
result = true; //将再次来到结果置为true } }catch(Exception e卡塔尔(قطر‎{
e.printStackTrace(State of Qatar; } return result; } 二、校订表名:
首先照旧要获得数据库连接conn和数据库描述对象dbmd以至Statement对象st,之后调用如下方法
复制代码 代码如下: /** * 更改表名 *
@param srcTableName 源表名 * @param newTableName 新表名 * @return
true:改革表名成功,false:改良表名退步 */ public boolean
renameTable(String srcTableName,String newTableNameState of Qatar{ Init(卡塔尔国; boolean
result = false; StringBuffer sql = new StringBuffer(State of Qatar; try{ String
dataBaseType = dbmd.getDatabaseProductName(卡塔尔; //获取数据库类型
if((“Microsoft SQL Server”卡塔尔(قطر‎.equals(dataBaseType卡塔尔(قطر‎State of Qatar{ //sqlServer try{
sql.append(“EXEC sp_rename”+”
“+srcTableName卡塔尔.append(“,”卡塔尔(قطر‎.append(newTableName卡塔尔(قطر‎; int temp = 0; temp =
st.executeUpdate(sql.toString(State of Qatar卡塔尔; //试行更新操作,再次回到结果 if(1==temp卡塔尔国{
result = true; //将重返值设为true } }catch(Exception e卡塔尔国{
e.printStackTrace(State of Qatar; } }else if((“HSQL Database
Engine”卡塔尔国.equals(dataBaseType卡塔尔(قطر‎||(“MySQL”卡塔尔.equals(dataBaseType卡塔尔国卡塔尔国{
//hsql和mysql try{ sql.append(“ALTE奥迪Q5 TABLE”+” “+srcTableName+” “+”RENAME
TO”+” “+newTableName卡塔尔; int temp = 1; temp =
st.executeUpdate(sql.toString(卡塔尔(قطر‎卡塔尔; //履行更新操作,重返结果 if(0==temp卡塔尔(قطر‎{
result = true; //将返回值设为true } }catch(Exception e卡塔尔{
e.printStackTrace(State of Qatar; } }else{ //还没完结对oracle和db2判定 }
}catch(Exception eState of Qatar{ e.printStackTrace(卡塔尔; }
//System.out.println(result卡塔尔(قطر‎; return result; }

连接池jar:c3p0

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.UnsupportedEncodingException;
import java.net.HttpURLConnection;
import java.net.InetSocketAddress;
import java.net.Proxy;
import java.net.URL;
import java.net.URLConnection;
import java.util.List;
import java.util.Map;

澳门金沙vip 1

/**
* Http哀告工具类
* @author snowfigure
* @since 2014-8-24 13:30:56
* @version v1.0.1
*/
public class HttpRequestUtil {
static boolean proxySet = false;
static String proxyHost = “127.0.0.1”;
static int proxyPort = 8087;
/**
* 编码
* @param source
* @return
*/
public static String urlEncode(String source,String encode) {
String result = source;
try {
result = java.net.URLEncoder.encode(source,encode);
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
return “0”;
}
return result;
}
public static String urlEncodeGBK(String source) {
String result = source;
try {
result = java.net.URLEncoder.encode(source,”GBK”);
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
return “0”;
}
return result;
}
/**
* 发起http要求获取再次来到结果
* @param req_url 央求地址
* @return
*/
public static String httpRequest(String req_url) {
StringBuffer buffer = new StringBuffer();
try {
URL url = new URL(req_url);
HttpURLConnection httpUrlConn = (HttpURLConnection)
url.openConnection();

代码布局

httpUrlConn.setDoOutput(false);
httpUrlConn.setDoInput(true);
httpUrlConn.setUseCaches(false);

澳门金沙vip 2

httpUrlConn.setRequestMethod(“GET”);
httpUrlConn.connect();

 

// 将再次来到的输入流转变来字符串
InputStream inputStream = httpUrlConn.getInputStream();
InputStreamReader inputStreamReader = new InputStreamReader(inputStream,
“utf-8”);
BufferedReader bufferedReader = new BufferedReader(inputStreamReader);


String str = null;
while ((str = bufferedReader.readLine()) != null) {
buffer.append(str);
}
bufferedReader.close();
inputStreamReader.close();
// 释放财富
inputStream.close();
inputStream = null;
httpUrlConn.disconnect();

布署文件  config.properties

} catch (Exception e) {
System.out.println(e.getStackTrace());
}
return buffer.toString();
}

#hibernate.dialect=sy.util.Oracle10gDialect
#driverClassName=oracle.jdbc.driver.OracleDriver
#validationQuery=SELECT 1 FROM DUAL
#jdbc_url=jdbc:oracle:thin:@localhost:1521:orcl
#jdbc_username=bjlyjoa
#jdbc_password=bjlyjoa

/**
* 发送http供给得到再次回到的输入流
* @param requestUrl 必要地址
* @return InputStream
*/
public static InputStream httpRequestIO(String requestUrl) {
InputStream inputStream = null;
try {
URL url = new URL(requestUrl);
HttpURLConnection httpUrlConn = (HttpURLConnection)
url.openConnection();
httpUrlConn.setDoInput(true);
httpUrlConn.setRequestMethod(“GET”);
httpUrlConn.connect();
// 得到重回的输入流
inputStream = httpUrlConn.getInputStream();
} catch (Exception e) {
e.printStackTrace();
}
return inputStream;
}

hibernate.dialect=org.hibernate.dialect.MySQL5InnoDBDialect
driverClass=com.mysql.jdbc.Driver
validationQuery=SELECT 1
jdbcUrl=jdbc\:mysql\://192.168.232.88\:3306/xlzj_sh?useUnicode\=true&characterEncoding\=UTF-8&zeroDateTimeBehavior\=convertToNull
user=root
password=123456

/**
* 向钦定U库罗德L发送GET方法的央求
*
* @param url
* 发送央浼的UEvoraL
* @param param
* 诉求参数,乞请参数应该是 name1=value1&name2=value2 的花样。
* @return UEscortL 所代表远程财富的响应结果
*/
public static String sendGet(String url, String param) {
String result = “”;
BufferedReader in = null;
try {
String urlNameString = url + “?” + param;
URL realUrl = new URL(urlNameString);
// 展开和U帕杰罗L之间的连接
URLConnection connection = realUrl.openConnection();
// 设置通用的央求属性
connection.setRequestProperty(“accept”, “*/*”);
connection.setRequestProperty(“connection”, “Keep-Alive”);
connection.setRequestProperty(“user-agent”,
“Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)”);
// 创建实际的总是
connection.connect();
// 获取具备响应头字段
Map<String, List<String>> map =
connection.getHeaderFields();
// 遍历全体的响应头字段
for (String key : map.keySet()) {
System.out.println(key + “—>” + map.get(key));
}
// 定义 BufferedReader输入流来读取U途乐L的响应
in = new BufferedReader(new InputStreamReader(
connection.getInputStream()));
String line;
while ((line = in.readLine()) != null) {
result += line;
}
} catch (Exception e) {
System.out.println(“发送GET哀告现身分外!” + e卡塔尔;
e.printStackTrace();
}
// 使用finally块来关闭输入流
finally {
try {
if (in != null) {
in.close();
}
} catch (Exception e2) {
e2.printStackTrace();
}
}
return result;
}

#hibernate.dialect=org.hibernate.dialect.SQLServer2008Dialect
#driverClassName=net.sourceforge.jtds.jdbc.Driver
#validationQuery=SELECT 1
#jdbc_url=jdbc:jtds:sqlserver://127.0.0.1:1433/qx
#jdbc_username=sa
#jdbc_password=123456

/**
* 向钦点 UXC60L 发送POST方法的乞求
*
* @param url
* 发送恳求的 U途胜L
* @param param
* 央求参数,央求参数应该是 name1=value1&name2=value2 的花样。
* @param isproxy
* 是不是接纳代理形式
* @return 所代表远程财富的响应结果
*/
public static String sendPost(String url, String param,boolean isproxy)
{
OutputStreamWriter out = null;
BufferedReader in = null;
String result = “”;
try {
URL realUrl = new URL(url);
HttpURLConnection conn = null;
if(isproxy卡塔尔{//使用代理方式
@SuppressWarnings(“static-access”)
Proxy proxy = new Proxy(Proxy.Type.DIRECT.HTTP, new
InetSocketAddress(proxyHost, proxyPort));
conn = (HttpURLConnection) realUrl.openConnection(proxy);
}else{
conn = (HttpURLConnection) realUrl.openConnection();
}
// 展开和U安德拉L之间的接连

hibernate.hbm2ddl.auto=none
hibernate.show_sql=false
hibernate.format_sql=false
hibernate.use_sql_comments=false

// 发送POST伏乞必需设置如下两行
conn.setDoOutput(true);
conn.setDoInput(true);
conn.setRequestMethod(“POST”); // POST方法

 

// 设置通用的乞求属性


conn.setRequestProperty(“accept”, “*/*”);
conn.setRequestProperty(“connection”, “Keep-Alive”);
conn.setRequestProperty(“user-agent”,
“Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)”);
conn.setRequestProperty(“Content-Type”,
“application/x-www-form-urlencoded”);

数据库连接 文件

conn.connect();

package light.mvc.framework.connectUtil;

// 获取UHighlanderLConnection对象对应的出口流
out = new OutputStreamWriter(conn.getOutputStream(), “UTF-8”);
// 发送乞请参数
out.write(param);
// flush输出流的缓冲
out.flush();
// 定义Buffered里德r输入流来读取U路虎极光L的响应
in = new BufferedReader(
new InputStreamReader(conn.getInputStream()));
String line;
while ((line = in.readLine()) != null) {
result += line;
}
} catch (Exception e) {
System.out.println(“发送 POST 要求出现相当!”+e卡塔尔(قطر‎;
e.printStackTrace();
}
//使用finally块来关闭输出流、输入流
finally{
try{
if(out!=null){
out.close();
}
if(in!=null){
in.close();
}
}
catch(IOException ex){
ex.printStackTrace();
}
}
return result;
}

import java.sql.Connection;
import java.sql.SQLException;
import java.util.Properties;

public static void main(String[] args) {
//demo:代理访谈
String url = “”;
String para =
“key=youkeyid&youuid=uid&advert_type=int&domain=adf.ly&url=”;

import javax.sql.DataSource;

String sr=HttpRequestUtil.sendPost(url,para,true);
System.out.println(sr);
}

import com.mchange.v2.c3p0.DataSources;

}

/**
*
*    
* 项目名称:s4h4s2s   
* 类名称:DataSourceConnUtil   
* 类描述: c3p0连接池管理类  
* 创建人:刘军/jun liu   
* 创设时间:二零一六-12-19 早上11:40:35   
* 修改人:刘军/shall_liu   
* 纠正时间:二零一六-12-19 中午11:40:35   
* 改过备注:   
* @version    
*    
*/
public class DataSourceConnUtil {

/**
* 发送Http post请求
*
* @param xmlInfo
* json转形成的字符串
* @param URL
* 请求url
* @return 重返消息
*/
public static String doHttpPost(String xmlInfo, String URL) {
System.out.println(“发起的数目:” + xmlInfoState of Qatar;
byte[] xmlData = xmlInfo.getBytes();
InputStream instr = null;
java.io.ByteArrayOutputStream out = null;
try {
URL url = new URL(URL);
URLConnection urlCon = url.openConnection();
urlCon.setDoOutput(true);
urlCon.setDoInput(true);
urlCon.setUseCaches(false);
urlCon.setRequestProperty(“content-Type”, “application/json”);
urlCon.setRequestProperty(“charset”, “utf-8”);
urlCon.setRequestProperty(“Content-length”,
String.valueOf(xmlData.length));
System.out.println(String.valueOf(xmlData.length));
DataOutputStream printout = new DataOutputStream(
urlCon.getOutputStream());
printout.write(xmlData);
printout.flush();
printout.close();
instr = urlCon.getInputStream();
byte[] bis = IOUtils.toByteArray(instr);
String ResponseString = new String(bis, “UTF-8”);
if ((ResponseString == null) || (“”.equals(ResponseString.trim()))) {
System.out.println(“返回空”);
}
System.out.println(“重返数据为:” + ResponseString卡塔尔;
return ResponseString;

private static final String JDBC_DRIVER = “driverClass”;
private static final String JDBC_URL = “jdbcUrl”;

} catch (Exception e) {
e.printStackTrace();
return “0”;
} finally {
try {
out.close();
instr.close();

private static DataSource ds;
/**
* 开端化连接池代码块
*/
static{
initDBSource();
}

} catch (Exception ex) {
return “0”;
}
}
}

/**
* 初始化c3p0连接池
*/
private static final void initDBSource(){
Properties c3p0Pro = new Properties();
try {
//加载配置文件
c3p0Pro.load(DataSourceConnUtil.class.getResourceAsStream(“/config.properties”));
//c3p0Pro.load(new
FileInputStream(PathUtil.getPath(“dabase.properties”)));
} catch (Exception e) {
e.printStackTrace();
}

String drverClass = c3p0Pro.getProperty(JDBC_DRIVER);
if(drverClass != null){
try {
//加载驱动类
Class.forName(drverClass);
} catch (ClassNotFoundException e) {
e.printStackTrace();
}

}

Properties jdbcpropes = new Properties();
Properties c3propes = new Properties();
for(Object key:c3p0Pro.keySet()){
String skey = (String)key;
if(skey.startsWith(“c3p0.”)){
c3propes.put(skey, c3p0Pro.getProperty(skey));
}else{
jdbcpropes.put(skey, c3p0Pro.getProperty(skey));
}
}

try {
//创立连接池
DataSource unPooled =
DataSources.unpooledDataSource(c3p0Pro.getProperty(JDBC_URL),jdbcpropes);
ds = DataSources.pooledDataSource(unPooled,c3propes);

} catch (SQLException e) {
e.printStackTrace();
}
}

/**
* 获取数据库连接对象
* @return 数据连接对象
* @throws SQLException
*/
public static synchronized Connection getConnection() {
Connection conn = null;
try {
conn = ds.getConnection();
conn.setTransactionIsolation(Connection.TRANSACTION_READ_COMMITTED);
conn.setAutoCommit(false卡塔尔(قطر‎;//撤销 事务处理:事务提交机制
} catch (SQLException e) {
e.printStackTrace();
}
return conn;
}
}


package light.mvc.framework.connectUtil;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.Properties;

import javax.sql.DataSource;

import com.mchange.v2.c3p0.DataSources;

/**
*
*    
* 项目名称:s4h4s2s   
* 类名称:DataSourceConnUtil   
* 类描述: c3p0连接池管理类  
* 创建人:刘军/jun liu   
* 创制时间:2014-12-19 深夜11:40:35   
* 修改人:刘军/shall_liu   
* 改善时间:二〇一四-12-19 上午11:40:35   
* 改进备注:   
* @version    
*    
*/
public class DataSourceConnUtil {

private static final String JDBC_DRIVER = “driverClass”;
private static final String JDBC_URL = “jdbcUrl”;

private static DataSource ds;
/**
* 起始化连接池代码块
*/
static{
initDBSource();
}

/**
* 初始化c3p0连接池
*/
private static final void initDBSource(){
Properties c3p0Pro = new Properties();
try {
//加载配置文件
c3p0Pro.load(DataSourceConnUtil.class.getResourceAsStream(“/config.properties”));
//c3p0Pro.load(new
FileInputStream(PathUtil.getPath(“dabase.properties”)));
} catch (Exception e) {
e.printStackTrace();
}

String drverClass = c3p0Pro.getProperty(JDBC_DRIVER);
if(drverClass != null){
try {
//加载驱动类
Class.forName(drverClass);
} catch (ClassNotFoundException e) {
e.printStackTrace();
}

}

Properties jdbcpropes = new Properties();
Properties c3propes = new Properties();
for(Object key:c3p0Pro.keySet()){
String skey = (String)key;
if(skey.startsWith(“c3p0.”)){
c3propes.put(skey, c3p0Pro.getProperty(skey));
}else{
jdbcpropes.put(skey, c3p0Pro.getProperty(skey));
}
}

try {
澳门金沙vip,//创立连接池
DataSource unPooled =
DataSources.unpooledDataSource(c3p0Pro.getProperty(JDBC_URL),jdbcpropes);
ds = DataSources.pooledDataSource(unPooled,c3propes);

} catch (SQLException e) {
e.printStackTrace();
}
}

/**
* 获取数据库连接对象
* @return 数据连接对象
* @throws SQLException
*/
public static synchronized Connection getConnection() {
Connection conn = null;
try {
conn = ds.getConnection();
conn.setTransactionIsolation(Connection.TRANSACTION_READ_COMMITTED);
conn.setAutoCommit(false卡塔尔(قطر‎;//废除 事务管理:事务提交机制
} catch (SQLException e) {
e.printStackTrace();
}
return conn;
}
}


package light.mvc.framework.connectUtil.jdbcUtl;
/**
*
* @Title: SqlParameter.java 
* @Package light.mvc.framework.connectUtil.jdbcUtl 
* @Description: TODO(存款和储蓄进程参数类型卡塔尔 
* @author 刘军
* @date 2016-3-19 下午2:47:02 
* @version V1.0  
*/
public class SqlParameter {

/**
* 参数名称
*/
public String Name;
/**
* 参数值
*/
public Object Value;
/**
* true代表参数为出口类型
*/
public boolean OutPut;
/**
* 参数类型
*/
public int Type;
/**
* 输入类型参数的布局函数
* @param name 存储进程 输入类型 参数名称
* @param value 存款和储蓄进程 输入类型 参数值
*/
public SqlParameter(String name,Object value){
this.Name = name;
this.Value= value;
}
/**
* 输出类型参数的构造函数
* @param type 存款和储蓄进程 输出类型 参数类型
* @param name 存储进度 输出类型 参数名称
*/
public SqlParameter(int type,String name){
this.Name = name;
this.OutPut = true;
this.Type = type;
}
/**
* 再次回到类型参数的布局函数
* @param type 存款和储蓄进度 再次回到类型
*/
public SqlParameter(int type){
this.Name = “”;
this.OutPut = true;
this.Type = type;
}
}