定时备份oralce数据库

config.properties

backpath =D\:\\backupme\\back
backconfig=D\:\\backupme\\bak_all.txt

PropertyReader.java
import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;

 

public  class PropertyReader extends Properties{
/**
*
*/
private static final long serialVersionUID = 1L;
private  String pqPath = “config.properties”;
private String configPath ;
public  PropertyReader(){
loadproperties();
}
public  PropertyReader(String pqPath) {
this.setPqPath(pqPath);
loadproperties();
}

/**
* 加载配置
*/
private void loadproperties() {
InputStream in;
try {
//URL path = PropertyReader.class.getResource(“”);
//String filepath = path.getFile();
//int len = filepath.length()-12;
//  String rootpath = filepath.substring(0,len);

String pqFilePath = Thread.currentThread().getContextClassLoader().getResource(“”) + getPqPath();
pqFilePath = pqFilePath.substring(6,pqFilePath.length());
in = new BufferedInputStream(new FileInputStream(pqFilePath));
File f = new File(this.getClass().getResource(“/”).getPath());
System.out.println(f);

try {
this.load(in);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}

public  String getPropertyByName(String name) {
String prop = this.getProperty(name);
return prop;
}
private String getPqPath() {
return pqPath;
}

private void setPqPath(String pqPath) {
this.pqPath = pqPath;
}
}

Backup.java

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.text.SimpleDateFormat;
import java.util.Date;

public class Backup {
public static void main(String args[]) throws Exception {
PropertyReader pr = new PropertyReader();
String backconfig = pr.getPropertyByName(“backconfig”);
String backpath = pr.getPropertyByName(“backpath”);
File file = new File(backconfig);// Text文件
BufferedReader br = new BufferedReader(new FileReader(file));// 构造一个BufferedReader类来读取文件
String s = null;
Date now = new Date();
SimpleDateFormat sdf = new SimpleDateFormat(“yyyy-MM-dd-hh-mm-ss”);
String nows = sdf.format(now);
while ((s = br.readLine()) != null) {// 使用readLine方法,一次读一行
s = s.trim();
String ss[] = s.split(“,”);
//ss 0 用户名
//ss 1 密码
//ss 2 ip
//ss 3 实例
String backName =ss[0]+”_”+ss[1]+”_”+ss[2]+”_”+ss[3]+”_”+nows+”.dmp'”;
String cmd = “exp “+ss[0]+”/”+ss[1]+”@”+ss[2]+”/”+ss[3]+” file='”+backpath+”\\”+backName+”;”;
Runtime runtime=Runtime.getRuntime();
runtime.exec(“cmd /c start “+cmd);
System.out.println(cmd);
}
br.close();
}
}

backup.bat

@echo off
rem 这个符号表示该行是注释。
rem 进入f盘,需要先切换盘符,成功后才能进入该盘下的目录
d:
cd \javawork\bakproj\bin
java Backup

backup_all.txt

scott,tiger,192.168.11.11,orcl
username,password,192.168.11.12,instance

把备份的数据库压缩

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;

public class Zip {
public static void main(String[] args) {
PropertyReader pr = new PropertyReader();
String backpath = pr.getPropertyByName(“backpath”);
String zippath = pr.getPropertyByName(“zippath”);
// backpath = backpath.replaceAll(“\\”,”/”);
SimpleDateFormat sdf = new SimpleDateFormat(“yyyy-MM-dd-HH-mm-ss”);
Date now = new Date();
String nows = sdf.format(now);
String backname = zippath+”\\”+”back” + nows + “.zip”;
ZipMultiFile(backpath, backname);
System.out.print(“zip end”);
deleteFile(backpath);
System.out.print(“delete end”);
}
private static void deleteFile(String dir){
File f = new File(dir);
if(f.isDirectory()){
File[] tempList = f.listFiles();
System.out.println(“该目录下对象个数:”+tempList.length);
for (int i = 0; i < tempList.length; i++) {
if(!tempList[i].isDirectory()){
tempList[i].delete();
}
}
}

}
public static void ZipMultiFile(String filepath, String zippath) {
try {
File file = new File(filepath);// 要被压缩的文件夹
File zipFile = new File(zippath);
InputStream input = null;
ZipOutputStream zipOut = new ZipOutputStream(new FileOutputStream(
zipFile));
if (file.isDirectory()) {
File[] files = file.listFiles();
for (int i = 0; i < files.length; ++i) {
input = new FileInputStream(files[i]);
zipOut.putNextEntry(new ZipEntry(file.getName()
+ File.separator + files[i].getName()));
int temp = 0;
while ((temp = input.read()) != -1) {
zipOut.write(temp);
}
input.close();
}
}
zipOut.close();
} catch (Exception e) {
e.printStackTrace();
}
}

public static void ZipFile(String filepath, String zippath) {
try {
File file = new File(filepath);
File zipFile = new File(zippath);
InputStream input = new FileInputStream(file);
ZipOutputStream zipOut = new ZipOutputStream(new FileOutputStream(
zipFile));
zipOut.putNextEntry(new ZipEntry(file.getName()));
int temp = 0;
while ((temp = input.read()) != -1) {
zipOut.write(temp);
}
input.close();
zipOut.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}

查找项目内最近修改的文件

package dto;

import java.util.Comparator;

public class CompareFileInfo implements Comparator{

@Override
public int compare(Object o1, Object o2) {
FileInfo f1=(FileInfo)o1;
FileInfo f2=(FileInfo)o2;
int flag=f1.getModifyTime().compareTo(f2.getModifyTime());
if(flag==0){
return f1.getFileName().compareTo(f2.getFileName());
}else {
return flag;
}
}

}

 

package dto;

import java.util.Date;

public class FileInfo  {
private String fileName;
private Date modifyTime;

public String getFileName() {
return fileName;
}

public void setFileName(String fileName) {
this.fileName = fileName;
}

public Date getModifyTime() {
return modifyTime;
}

public void setModifyTime(Date modifyTime) {
this.modifyTime = modifyTime;
}
}

 

package util;
import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.util.Properties;

 

public  class PropertyReader extends Properties{
/**
*
*/
private static final long serialVersionUID = 1L;
private  String pqPath = “config.properties”;
private String configPath ;
public  PropertyReader(){
loadproperties();
}
public  PropertyReader(String pqPath) {
this.setPqPath(pqPath);
loadproperties();
}

/**
* 加载配置
*/
private void loadproperties() {
InputStream in;
try {
//URL path = PropertyReader.class.getResource(“”);
//String filepath = path.getFile();
//int len = filepath.length()-12;
//  String rootpath = filepath.substring(0,len);

String pqFilePath = Thread.currentThread().getContextClassLoader().getResource(“”) + getPqPath();
pqFilePath = pqFilePath.substring(6,pqFilePath.length());
in = new BufferedInputStream(new FileInputStream(pqFilePath));
File f = new File(this.getClass().getResource(“/”).getPath());
System.out.println(f);

try {
this.load(in);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}

public  String getPropertyByName(String name) {
String prop = this.getProperty(name);
return prop;
}
private String getPqPath() {
return pqPath;
}

private void setPqPath(String pqPath) {
this.pqPath = pqPath;
}
}

 

package util;

import java.io.File;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Collections;
import java.util.Date;
import java.util.List;

import dto.CompareFileInfo;
import dto.FileInfo;

public class RecentModify {

public static void main(String[] args) throws Exception {
// 递归显示C盘下所有文件夹及其中文件
PropertyReader pr = new PropertyReader();
String fp = pr.getPropertyByName(“filepath”);
File root = new File(fp);

List<FileInfo> files = new ArrayList<FileInfo>();
showAllFiles(root,files);
CompareFileInfo comparator=new CompareFileInfo();
Collections.sort(files,comparator);

for(FileInfo fi:files){
Date d = fi.getModifyTime();
SimpleDateFormat sdf = new SimpleDateFormat(“yyyy-MM-dd HH:mm:ss”);
String dd = sdf.format(d);
System.out.println(fi.getFileName() + “,\n modify time:”
+ dd);
System.out.println(“—————————————————“);
}
System.out.println(“文件总数:”+files.size());
}

final static void showAllFiles(File dir,List<FileInfo> files) throws Exception {
File[] fs = dir.listFiles();

for (int i = 0; i < fs.length; i++) {
//if(fs[i].getName().indexOf(“svn”)>0) continue;
long time = fs[i].lastModified();
Calendar cal = Calendar.getInstance();
cal.setTimeInMillis(time);
Date modifyTime = cal.getTime() ;

FileInfo fi = new FileInfo();
fi.setFileName(fs[i].getAbsolutePath());
fi.setModifyTime(modifyTime);
files.add(fi);
if (fs[i].isDirectory()&&fs[i].getPath().indexOf(“.svn”)<0) {
try {
showAllFiles(fs[i],files);
} catch (Exception e) {
}
}
}
}
}

config.properties

filepath =D\:\\javawork\\myproj

recentmodify.bat

@echo off
rem 这个符号表示该行是注释。
rem 进入f盘,需要先切换盘符,成功后才能进入该盘下的目录
d:
cd \javawork\fileproj\bin
java util.RecentModify
pause

java日志实现

日志接口

package com.netsign.util.log.inf;

public interface LogInf {
public void log(String logMsg);
}

日志实现有文件日志,控制台日志,两者组合

package com.netsign.util.log;

import java.text.SimpleDateFormat;
import java.util.Date;

import com.netsign.util.log.inf.LogInf;

public class ConsoleLog implements LogInf{

public void log(String logMsg) {
// TODO Auto-generated method stub
SimpleDateFormat df = new SimpleDateFormat(“yyyy-MM-dd HH:mm:ss:SSS”);// 设置日期格式
String datestr = df.format(new Date());// new Date()为获取当前系统时间
String txt = datestr+”[“+logMsg+”]”+”\n”;
System.out.println(txt);
}

}

package com.netsign.util.log;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;

import com.netsign.util.PropertyReader;
import com.netsign.util.log.inf.LogInf;

public final class FileLog implements LogInf {
public void log(String logMsg) {
PropertyReader pr = new PropertyReader();
String fileName = pr.getPropertyByName(“logfile”);
File file = new File(fileName);
createIfNotExist(file);
FileWriter fw = createFileWriter(file);
SimpleDateFormat df = new SimpleDateFormat(“yyyy-MM-dd HH:mm:ss:SSS”);// 设置日期格式
String datestr = df.format(new Date());// new Date()为获取当前系统时间
String txt = datestr+”[“+logMsg+”]”+”\n”;
try {
fw.append(txt);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
fw.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

}

/**
* @param file
* @return
*/
private static FileWriter createFileWriter(File file) {
FileWriter fw = null;
try {
fw = new FileWriter(file, true);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return fw;
}

/**
* @param file
*/
private static void createIfNotExist(File file) {
if (!file.exists()) {
try {
file.createNewFile();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}

package com.netsign.util.log;

import com.netsign.util.log.inf.LogInf;

public class CompositeLog implements LogInf {
LogInf consoleLog = new ConsoleLog();
LogInf fileLog = new FileLog();
public void log(String logMsg) {
// TODO Auto-generated method stub
consoleLog.log(logMsg);
fileLog.log(logMsg);
}

}

配置文件读取

package com.netsign.util;
import java.io.BufferedInputStream;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.util.Properties;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

public class PropertyReader extends Properties{
/**
*
*/
private static final long serialVersionUID = 1L;
private String pqPath = “pq.properties”;
private Log log = LogFactory.getLog(PropertyReader.class);
public PropertyReader(){
loadproperties();
}
public PropertyReader(String pqPath) {
this.setPqPath(pqPath);
loadproperties();
}

/**
* 加载配置文件
*/
private void loadproperties() {
InputStream in;
try {
//String classpath = PropertyReader.class.getClassLoader()
// .getResource(“”).getPath();
// URL path = PropertyReader.class.getResource(“”);
// String rootpath = path.getFile().substring(0,path.getFile().length()-17);
// String pqFilePath = rootpath + getPqPath();
// log.info(“snsFilePath:”+snsFilePath);
URL xmlpath = this.getClass().getClassLoader().getResource(“/”);
String pqFilePath = xmlpath.getPath() + getPqPath();
in = new BufferedInputStream(new FileInputStream(pqFilePath));
try {
this.load(in);
log.info(“property loaded”);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
log.error(“property not loaded”);
}
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
log.error(“property’s file not be found”);
}
}

public String getPropertyByName(String name) {
// init();
String prop = this.getProperty(name);
prop = prop.trim();
log.info(“property’s value is ok”);
return prop;
}
public String getPqPath() {
return pqPath;
}

public void setPqPath(String pqPath) {
this.pqPath = pqPath;
}
}

pq.properties

logfile=d\:\\logs\\cxf_server_log.log
consolelog = 1
filelog =1

日志工厂

package com.netsign.util.log;

import com.netsign.util.PropertyReader;
import com.netsign.util.log.inf.LogInf;

public class LogFactory {
LogInf log ;
public LogInf createLog(){
PropertyReader pr = new PropertyReader();
String consolelog = pr.getPropertyByName(“consolelog”);
String filelog = pr.getPropertyByName(“filelog”);
if(“1″.equals(consolelog)&&!”1″.equals(filelog)){
log = new ConsoleLog();
}else if(!”1″.equals(consolelog)&&”1”.equals(filelog)){
log = new FileLog();
}else if(“1″.equals(consolelog)&&”1”.equals(filelog)){
log = new CompositeLog();
}else{
log = new ConsoleLog();
}
return log;
}
}

 

 

java写日志

PropertyReader.java

package combiz.util;
import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.util.Properties;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

 

public  class PropertyReader extends Properties{
/**
*
*/
private static final long serialVersionUID = 1L;
private  String pqPath = “config.properties”;
private  Log log = LogFactory.getLog(PropertyReader.class);
public  PropertyReader(){
loadproperties();
}
public  PropertyReader(String pqPath) {
this.setPqPath(pqPath);
loadproperties();
}

/**
* 加载配置
*/
private void loadproperties() {
InputStream in;
try {
//URL path = PropertyReader.class.getResource(“”);
//String filepath = path.getFile();
//int len = filepath.length()-12;
//  String rootpath = filepath.substring(0,len);

URL xmlpath = this.getClass().getClassLoader().getResource(“/”);
String pqFilePath = xmlpath.getPath() + getPqPath();
log.info(xmlpath );
System.out.println(xmlpath);
in = new BufferedInputStream(new FileInputStream(pqFilePath));
File f = new File(this.getClass().getResource(“/”).getPath());
System.out.println(f);

try {
this.load(in);
log.info(“property loaded”);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
log.error(“property not loaded”);
}
} catch (FileNotFoundException e) {
e.printStackTrace();
log.error(“property’s file not be found”);
}
}

public  String getPropertyByName(String name) {
String prop = this.getProperty(name);
log.info(“property’s value is ok”);
return prop;
}
private String getPqPath() {
return pqPath;
}

private void setPqPath(String pqPath) {
this.pqPath = pqPath;
}
}

Mylog.java

package combiz.util;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;

public final class Mylog {
public static void log(String logMsg) {
PropertyReader pr = new PropertyReader();
String fileName = pr.getPropertyByName(“logfile”);
File file = new File(fileName);
createIfNotExist(file);
FileWriter fw = createFileWriter(file);
SimpleDateFormat df = new SimpleDateFormat(“yyyy-MM-dd HH:mm:ss:SSS”);// 设置日期格式
String datestr = df.format(new Date());// new Date()为获取当前系统时间
String txt = datestr+”[“+logMsg+”]”+”\n”;
try {
fw.append(txt);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
fw.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

}

/**
* @param file
* @return
*/
private static FileWriter createFileWriter(File file) {
FileWriter fw = null;
try {
fw = new FileWriter(file, true);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return fw;
}

/**
* @param file
*/
private static void createIfNotExist(File file) {
if (!file.exists()) {
try {
file.createNewFile();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}

config.properties

location=\u90AF\u90F8\u5E02
propertyRightUrl=http\://192.168.1.102\:8086/CXF_Server_CQ
fundSupervisionDep=\u90AF\u90F8\u5E02\u4F4F\u623F\u4FDD\u969C\u548C\u623F\u4EA7\u7BA1\u7406\u5C40\u623F\u4EA7\u4EA4\u6613\u5E02\u573A\u76D1\u7BA1\u5904
wstimeout=600000
ispr=1
logfile=d:\\logs\\mylog.log

ninject解耦service和dao层

三层框架,dao,service,web层,web调用service,service调用dao,此文为示例代码。

从底层dao开始,

dao接口

using System;
using System.Collections.Generic;
using System.Linq;
using System.Security.Cryptography.X509Certificates;
using System.Text;

namespace dao
{
public interface IUserDao
{
bool add(String username);
}
}

dao实现

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace dao
{
public class UserDao:IUserDao
{
public bool add(string username)
{
Console.WriteLine(“dao,added username:”+username);
return true;
}
}
}

service接口

using System;
using System.Collections.Generic;
using System.Linq;
using System.Security.Cryptography.X509Certificates;
using System.Text;

namespace service
{
public interface IUserService
{
bool add(String username);
}
}

service实现

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using dao;
using Ninject;

namespace service
{
public class UserService:IUserService
{
private IUserDao _userDao;
[Inject]
public UserService(IUserDao userDao)
{
_userDao = userDao;
}
public bool add(string username)
{

Console.WriteLine(“service,added username:” + username);
_userDao.add(username);
return true;
}
}
}

module 关联接口和实现类,更改接口实现就在这里

using dao;
using Ninject.Modules;
using service;

namespace container
{
public class ContainerModule : NinjectModule
{

public override void Load()
{
Bind<IUserDao>().To<UserDao>();
Bind<IUserService>().To<UserService>();
}
}
}

页面调用

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using container;
using dao;
using Ninject;
using service;

namespace ninjectdemo1
{
public partial class WebForm1 : System.Web.UI.Page
{
private static IKernel kernel = new StandardKernel(new ContainerModule());
protected void Page_Load(object sender, EventArgs e)
{
IUserDao userDao = kernel.Get<IUserDao>();
IUserService userService = kernel.Get<IUserService>();

userService.add(“abc”);
Console.WriteLine(“continues..”);
Console.Read();
}
}
}

配置文件读取和数据库操作类

小应用就没必要使用框架了,用这个操作数据库,棒棒哒!

PropertyReader

package com.netsign.util;
import java.io.BufferedInputStream;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.util.Properties;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

 

public  class PropertyReader extends Properties{
/**
*
*/
private static final long serialVersionUID = 1L;
private  String pqPath = “pq.properties”;
private  Log log = LogFactory.getLog(PropertyReader.class);
public  PropertyReader(){
loadproperties();
}
public  PropertyReader(String pqPath) {
this.setPqPath(pqPath);
loadproperties();
}

/**
* 加载配置文件
*/
private void loadproperties() {
InputStream in;
try {
//String classpath = PropertyReader.class.getClassLoader()
//  .getResource(“”).getPath();
//   URL path = PropertyReader.class.getResource(“”);
//     String rootpath = path.getFile().substring(0,path.getFile().length()-17);
//   String pqFilePath = rootpath + getPqPath();
// log.info(“snsFilePath:”+snsFilePath);
URL xmlpath = this.getClass().getClassLoader().getResource(“/”);
String pqFilePath = xmlpath.getPath() + getPqPath();
in = new BufferedInputStream(new FileInputStream(pqFilePath));
try {
this.load(in);
log.info(“property loaded”);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
log.error(“property not loaded”);
}
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
log.error(“property’s file not be found”);
}
}

public  String getPropertyByName(String name) {
// init();
String prop = this.getProperty(name);
log.info(“property’s value is ok”);
return prop;
}
public String getPqPath() {
return pqPath;
}

public void setPqPath(String pqPath) {
this.pqPath = pqPath;
}
}

DbTools

package com.netsign.util;

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 java.util.Map;

import javax.servlet.jsp.jstl.sql.Result;
import javax.servlet.jsp.jstl.sql.ResultSupport;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
/**
*
* @author sun
*
*/
public class DbTools {
private  String DriverName;// = “com.microsoft.sqlserver.jdbc.SQLServerDriver”;
private  String ConnectUrl ;//= “jdbc:sqlserver://cyq-PC:1433;databaseName=R_Dremis”;
private  String UserName ;//= pr.getPropertyByName(“dbUserName”);
private  String Password;// = PropertyReader.getPropertyByName(“dbPassWord”);
private  String server;//=PropertyReader.getPropertyByName(“server”);
private  String port ;//= PropertyReader.getPropertyByName(“port”);
private  String instance;// = PropertyReader.getPropertyByName(“instance”);
private  String url ;
private  Connection conn = null;
private  PreparedStatement ps = null;
private  ResultSet rs = null;
private static Log log = LogFactory.getLog(DbTools.class);
private String pqPath;
private PropertyReader pr = null;
/**
*
* @param db 数据库名
*/
public DbTools(){
pr = new PropertyReader();
setDbProperties();
//setConnectUrl(“jdbc:oracle:thin://”+getServer()+”:”+getPort()+”;instanceName=”+getInstance()+”;databaseName=”+db);
//setConnectUrl(“”)
}
/**
*
*/
private void setDbProperties() {
//setUrl(pr.getPropertyByName(“url”));
setConnectUrl(pr.getPropertyByName(“url”));
setDriverName(pr.getPropertyByName(“DriverName”));
setUserName(pr.getPropertyByName(“dbUserName”));
setPassword(pr.getPropertyByName(“dbPassWord”));
setServer(pr.getPropertyByName(“server”));
setPort(pr.getPropertyByName(“port”));
setInstance(pr.getPropertyByName(“instance”));
}
/**
*
* @param db 数据库名
* @param pqPath 配置文件名称
*/
public DbTools(String db,String pqPath){
pr = new PropertyReader(pqPath);
setDbProperties();
setConnectUrl(“jdbc:sqlserver://”+getServer()+”:”+getPort()+”;instanceName=”+getInstance()+”;databaseName=”+db);

}
private Connection newConn(){
try {
Class.forName(getDriverName());
String connstr = getConnectUrl();
conn = DriverManager.getConnection(connstr, getUserName(), getPassword());
} catch (Exception e) {
conn = null;
e.printStackTrace();
log.error(e);
}
return conn;
}
/**
* 注册驱动并获取连
*
* @Method GetConn
*/
public  Connection GetConn() {
//  if(conn==null){
//   conn = newConn();
//   return conn;
//  }else{
//   return conn;
//  }
//  JdbcPool jdbcPool = new JdbcPool();
//  try {
//   conn = jdbcPool.getConnection();
//  } catch (SQLException e) {
//   // TODO Auto-generated catch block
//   e.printStackTrace();
//  }
//  return conn;
try {
Class.forName(getDriverName());
String connstr = getConnectUrl();
conn = DriverManager.getConnection(connstr, getUserName(), getPassword());
} catch (Exception e) {
conn = null;
e.printStackTrace();
log.error(e);
}
return conn;
}
public  Map[] QueryMap(String sql) {
conn = GetConn();
Map[] map = null;

try {
ps = conn.prepareStatement(sql);

Result rs = ResultSupport.toResult(ps.executeQuery());
map = rs.getRows();
} catch (Exception e) {
map = null;
e.printStackTrace();
log.error(e);
} finally {
Free();
}
return map;
}
/**
* 通用查询
*
* @Method QueryMap
* @param sql
* @param values
*/
public  Map[] QueryMap(String sql, Object[] values) {
conn = GetConn();
Map[] map = null;

try {
ps = conn.prepareStatement(sql);
if (values != null && values.length != 0) {
for (int i = 0; i < values.length; i++) {
ps.setObject((i + 1), values[i]);
}
}
Result rs = ResultSupport.toResult(ps.executeQuery());
map = rs.getRows();
} catch (Exception e) {
map = null;
e.printStackTrace();
log.error(e);
} finally {
Free();
}
return map;
}
public int[] ExecuteBatchBySql(String[] sqlarr){
conn = GetConn();
Statement stmt = null;
try {
stmt = conn.createStatement();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
for(int i=0;i<sqlarr.length;i++){
try {
stmt.addBatch(sqlarr[i]);
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
int[] rtnint = null;
try {
rtnint = stmt.executeBatch();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally {
Free();
}
return rtnint;
}
public  int ExcuteBySql(String sql) {
conn = GetConn();
int result = 0;
try {

ps = conn.prepareStatement(sql);
result  = ps.executeUpdate();
} catch (Exception e) {
result = 0;
e.printStackTrace();
log.error(e);
} finally {
Free();
}
return result;
}
/**
* 通用增
*
* @Method ExcuteBySql
* @param sql
* @param values
*/
public  int ExcuteBySql(String sql, Object[] values) {
conn = GetConn();
int result = 0;

try {
ps = conn.prepareStatement(sql);
if (values != null && values.length != 0) {
for (int i = 0; i < values.length; i++) {
ps.setObject((i + 1), values[i]);
}
}
result = ps.executeUpdate(sql);
} catch (Exception e) {
result = 0;
e.printStackTrace();
log.error(e);
} finally {
Free();
}
return result;
}

/**
* 释放资源
*
* @Method Free
*/
private  void Free() {
try {
if (rs != null) {
rs.close();
}
if (ps != null) {
ps.close();
}
if (conn != null) {
conn.close();
}
} catch (Exception e) {
e.printStackTrace();
log.error(e);
}
}
public String getPqPath() {
return pqPath;
}
public void setPqPath(String pqPath) {
this.pqPath = pqPath;
}
public String getConnectUrl() {
return ConnectUrl;
}
public void setConnectUrl(String connectUrl) {
ConnectUrl = connectUrl;
}
public String getUserName() {
return UserName;
}
public void setUserName(String userName) {
UserName = userName;
}
public String getPassword() {
return Password;
}
public void setPassword(String password) {
Password = password;
}
public String getServer() {
return server;
}
public void setServer(String server) {
this.server = server;
}
public String getPort() {
return port;
}
public void setPort(String port) {
this.port = port;
}
public String getInstance() {
return instance;
}
public void setInstance(String instance) {
this.instance = instance;
}
public String getDriverName() {
return DriverName;
}
public void setDriverName(String driverName) {
DriverName = driverName;
}
public String getUrl() {
return url;
}
public void setUrl(String url) {
this.url = url;
}

}

 

jdbc批量更新

对于程序频繁获取数据库连接,批量插入数据,一种是建立数据连接池,减少数据库建立连接的开销。另一种是索性就用一个数据库连接把事情都做了。

下面的方法就是批量插入数据库的方法,sqlarr是传过去的sql数组

public int[] ExecuteBatchBySql(String[] sqlarr){
conn = GetConn();
Statement stmt = null;
try {
stmt = conn.createStatement();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
for(int i=0;i<sqlarr.length;i++){
try {
stmt.addBatch(sqlarr[i]);
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
int[] rtnint = null;
try {
rtnint = stmt.executeBatch();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally {
Free();
}
return rtnint;
}

重新搭建这个博客,没事写写心得也不错。

最近忙完工作那些事,有些闲鱼。内心空唠唠的。除啦工作还是工作。想在网络上发展一下,无奈工作太忙也许是太懒,没有恒心。这不闲了,把以前的备份找出来,恢复一下。

虽然咱以前写的文章水平有限,但是我旧时喜欢老的,再丢脸也不嫌磕碜。看看以前写的,好像又回到了从前。

目前正在整理中,把以前的博文恢复一下,把图片上传一下。至于分类,网站设置,插件什么的,就自己设置一下。稍稍打扮一下,重新开张了。

market samurai 更新到 to 0.92.58

0.92.58_planner Changes:

Keyword Research

  • Bug Fix: Some accounts require accepting new AdWords Terms and Conditions. If required, instructions on how to do so will be provided.

0.92.57 Changes:

Keyword Research

  • Google keyword planner compatibility – adwords accounts in other languages.