请问在java中这个自动化的Mysql DB连接可能有什么问题

时间:2014-12-03 11:08:14

标签: java mysql jdbc

我现在已经有很多问题了。

任何人都可以帮助我完成它。我一直在尝试自动连接到一个mysql数据库,从文件中加载所有配置。

但我无法让它发挥作用。

import com.mysql.jdbc.Statement;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;


public class dbManager {

public Connection connection=null;
public Statement statement=null;
public ResultSet resultset=null;

private String dburl;
private String username;
private String password;

public dbManager(String dburl,String username,String password){
   this.dburl=dburl;
   this.username=username;
   this.password=password;
}


public boolean acquireConnection() throws Exception{
    boolean connected=false;
    Class.forName("com.mysql.jdbc.Driver");

    connection=DriverManager.getConnection(dburl,username, password);
    connected=true;
    return connected;
}

}

我有这个类调用上面的类dbManager

import FileUtility;
import dbManager;
import java.awt.Dimension;
import java.awt.Toolkit;
import java.io.File;
import java.util.StringTokenizer;
import java.util.Vector;


public class appInitialiser {


configuration configPanel;
Vector configurationVector;
dbManager dbmanager;
Dimension dim=Toolkit.getDefaultToolkit().getScreenSize();
appInitialiser(){


}

public void initialiseApp(FaceRecognition faceFrame){
    try{
        String usertmp=System.getenv("TMP");
        String configdir=usertmp+"\\facerecognition";
        String configfile=usertmp+"\\facerecognition\\config.ini";
        File conf=new File(configfile);
        String content="";
        if(conf.exists()){
            content=FileUtility.readFile(configfile);
        }
        else{
          callConfig(faceFrame);
        }
        while(content.trim().isEmpty()){
            content=FileUtility.readFile(configfile);
        }
        getConfiguration(content);
        for(int i=0;i<3;i++){
            System.out.println(configurationVector.elementAt(i).toString());
        }
        String dburl=configurationVector.elementAt(0).toString();
        String username=configurationVector.elementAt(1).toString();
        String password=configurationVector.elementAt(2).toString();
        System.out.println("db="+dburl+" "+username+" "+password);
        dbmanager=new dbManager(dburl,username,password);
        boolean acquireconnection=dbmanager.acquireConnection();
        if(acquireconnection){
            System.out.println("connected");
        }
        else{
            System.out.println("failed to connected");
        }
    }//end of try
    catch(Exception ex){
        ex.printStackTrace();
    }//end of catch
}

public void callConfig(FaceRecognition faceFrame){
    configPanel=new configuration(faceFrame);
    int width=520;
    int height=355;
    int xlocation=dim.width/2-width/2;
    int ylocation=dim.height/2-height/2;
    configPanel.setBounds(xlocation, ylocation, width, height);
    faceFrame.add(configPanel,new Integer(1),0);
    faceFrame.panel.remove(faceFrame.panel.statusLabel);
    faceFrame.repaint(); 
}

private void getConfiguration(String content){
    StringTokenizer tokens=new StringTokenizer(content,"\n");
    String tokenvalue="";
    String tokenkey="";
    String subtoken="";
    configurationVector=new Vector();
    configurationVector.removeAllElements();
    while (tokens.hasMoreTokens()){
        subtoken=tokens.nextToken();
        StringTokenizer substring=new StringTokenizer(subtoken,"=");
        tokenkey=substring.nextToken();
        tokenvalue=substring.nextToken();
        configurationVector.addElement(tokenvalue);
    }
}

}

但我似乎无法连接它。

任何人都可以帮忙。

1 个答案:

答案 0 :(得分:2)

如果您不使用密码,则需要确保传递null而不是空字符串""):

public dbManager(String dburl,String username,String password){
   this.dburl=dburl;
   this.username=username;
   if (password != null && password.length() != 0) {
       this.password=password;
   }
}