* 连接操纵数据库
* @author:Scofi
* @date:March 06,2010
**/
import java.sql.*;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.Properties;
import java.util.Vector;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.IOException;
import java.io.PrintWriter;
public class DBProcess{
private static String url = null;
private static String sql = null;
private static Connection conn = null;
private static PreparedStatement pstmt = null;
private static Statement stmt = null;
private static ResultSet rs = null;
private static String db_user = null,db_pass = null;
private static String driver = null;
private static boolean success = true;
private static ArrayList<String> al;
private static int j,k,m,type;
private static long result_length,param_length;
private static String param = "",temp,condition="";
private static String filepath,error ;
public static void main(String args[]){
filepath = "C:\\Documents and Settings\\Administrator\\桌面\\database.ini";
driver = ProfileString.profileString(filepath,"database","driver",error);
url = ProfileString.profileString(filepath,"database","url",error);;
db_user = ProfileString.profileString(filepath,"database","logid",error);;
db_pass = ProfileString.profileString(filepath,"database","password",error);;
//sql = "SELECT YGXM,YGDM,PYDM,YGZW FROM GY_YGDM WHERE YGDM in (?)";
sql = "DELETE FROM GY_YGDM WHERE YGDM in (?)";
DBProcess dbp = new DBProcess();
if(!dbp.init(url,db_user,db_pass))
return;
//conn = dbp.getConnection();
for(String s :args){
param += s+",";
}
if(param == null || param == ""){
System.out.println("请输入查询条件(请用逗号隔开):");
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
try{
param = br.readLine();
}catch(IOException e){
System.out.println("接受输入参数失败,原因如下:"+e.getMessage());
}
if(param == null || param == ""){
System.out.println("未输入查询条件,程序将退出...");
return;
}
}else{
param = param.substring(0,param.length() - 1).trim();
}
//System.out.println(param);
setType(2);
setlength(4);
dbp.executeSQL(conn,sql,param,2);
dbp.disConnectDB(conn);
dbp.printResult(al);
}
public Boolean init(String url,String name,String pass){
try{
Class.forName(driver);
Properties pro = new Properties();
pro.put("user",name);
pro.put("password",pass);
pro.put("charSet","gb2312");
conn = DriverManager.getConnection(url,pro);
success = true;
}catch(ClassNotFoundException ce){
success = false;
System.out.println("数据库连接失败,原因如下:"+ce.getMessage());
}catch(SQLException se){
success = false;
System.out.println("数据库连接失败,原因如下:"+se.getMessage());
}finally{
if(success){
System.out.println("数据库连接成功!");
}
return success;
}
}
public Connection getConnection(){
return conn;
}
public void executeSQL(Connection conn,String sql,String value,int type){
try{
for(int i=1;i <= getNumber(value,",");i++){
if(i > 1)
condition += ",";
condition += "?" ;
}
sql = sql.replaceAll("\\?",condition);
System.out.println("sql = " +sql);
pstmt = conn.prepareStatement(sql);
String s[] = value.split(",");
for(int i = 0;i<s.length;i++){
pstmt.setString(i+1,s[i]);
}
if(type == 1){ //查询操作
rs = pstmt.executeQuery();
al = new ArrayList<String>();
while(rs.next()){
for(int j = 1;j<= result_length;j++){
al.add(m++,rs.getString(j));
}
}
}else if(type == 2){ //更新操作
conn.setAutoCommit(false);
pstmt.executeUpdate();
conn.commit();
success = true;
}else{
}
}catch(SQLException se){
success = false;
try{
conn.rollback();
conn.setAutoCommit(true);
}catch(SQLException e){
System.out.println(e.getMessage());
}
System.out.println("SQL语句执行失败,原因如下:"+se.getMessage());
}finally{
if(success){
if(type == 1){
System.out.println("查询成功!");
}else if(type == 2){
System.out.println("更新成功!");
}
}
}
}
public void disConnectDB(Connection conn){
try{
if(conn != null){
conn.close();
success = true;
}
}catch(SQLException e){
success = false;
System.out.println("断开连接失败,原因如下:"+e.getMessage());
}finally{
if(success){
System.out.println("数据库断开连接成功!");
}
}
}
public void printResult(ArrayList al){
if(type == 1){
Iterator it = al.iterator();
System.out.println("打印结果:");
System.out.println("--------------------------------------------------");
while(it.hasNext()){
System.out.print(it.next());
System.out.print(" ");
++k;
if(k%result_length == 0){
System.out.println();
}
}
}
}
public static void setType(int i){
type = i;
}
public static void setlength(int i){
result_length = i;
}
public static int getNumber(String s,String sep){
String s2[] = s.split(sep);
return s2.length;
}
}
/**
* 配置文件读取
**/
import java.io.*;
public class ProfileString{
private static String str;
private static String rf,error;
/*public static void main(String args[]){
System.out.println(profileString("C:\\Documents and Settings\\Administrator\\桌面\\database.ini","database","url",error));
}*/
public static String profileString(String filename,String section,String key,String error ){
try{
Boolean bl = false;
File f = new File(filename);
if(!f.exists()){
System.out.println("file is not found");
error = "file is not found!";
}
BufferedReader br = new BufferedReader(new FileReader(f));
while((str = br.readLine())!=null){
if(!bl){
int start = str.indexOf("[");
int end = str.indexOf("]");
if(start >= 0 && end >=0 && end >= start){
if(str.substring(start+1,end).equals(section)){
bl = !bl;
continue;
}else{
error = "not found the matching infomation: ["+section+"]";
}
}
}
if(bl){
if(str.indexOf(key) >= 0){
if(str.indexOf("=") >= 0){
rf = str.substring(str.indexOf("=")+1,str.length()).trim();
break;
}
}
}
}
br.close();
}catch(IOException e){
System.out.println(e.getMessage());
}finally{
}
return rf;
}
}
配置文件内容如下:
[database]
driver=com.microsoft.sqlserver.jdbc.SQLServerDriver
url=jdbc:sqlserver://localhost:1433;DatabaseName=bsrun_lj
logid=sa
password=
4 个解决方案
#1
filepath = "C:\\Documents and Settings\\Administrator\\桌面\\database.ini";
最好不要有中文路径
#2
#3
出什么问题了。
如果有异常,把异常贴出来。。。。。
如果有异常,把异常贴出来。。。。。
#4
#1
filepath = "C:\\Documents and Settings\\Administrator\\桌面\\database.ini";
最好不要有中文路径
#2
http://www.java3z.com/cwbwebhome/article/article1a/183.html?id=410
-
http://hi.baidu.com/wp0930/blog/item/35f16044e50b3e36869473e5.html
-
楼主 可以参考上面的方法
-
http://hi.baidu.com/wp0930/blog/item/35f16044e50b3e36869473e5.html
-
楼主 可以参考上面的方法
#3
出什么问题了。
如果有异常,把异常贴出来。。。。。
如果有异常,把异常贴出来。。。。。