英文:
How to make a file path compatible with an embedded database? (Apache Derby Embedded)
问题
最近我一直在尝试使用 JFileChooser 来选择数据库将被创建的位置;然而,我遇到的问题是,我从 JFileChooser 得到的文件路径中有反斜杠而不是正斜杠,我认为这就是不允许我创建数据库的原因。这是我的代码,以及解决这个问题的尝试。
private void openconnection() {
try {
// 尝试连接数据库
DriverManager.registerDriver(new org.apache.derby.jdbc.EmbeddedDriver());
databaseconnection = DriverManager.getConnection("jdbc:derby:" + formattedfolderpath + ";");
databaseconnection.setAutoCommit(false);
currentdb = true;
} catch (SQLException EX) {
try {
// 如果数据库不存在,就创建它
DriverManager.registerDriver(new org.apache.derby.jdbc.EmbeddedDriver());
databaseconnection = DriverManager.getConnection("jdbc:derby:" + formattedfolderpath + ";create=true");
databaseconnection.setAutoCommit(false);
currentdb = true;
} catch (SQLException EX2) {
//infoBox("OH MY LAWD", "Error");
}
}
}
JButton open = new JButton();
JFileChooser fc = new JFileChooser();
fc.setCurrentDirectory(new java.io.File("C:/Users/1jenningst/Desktop"));
fc.setDialogTitle("PDF Manager");
fc.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
if (fc.showOpenDialog(open) == JFileChooser.APPROVE_OPTION){
//
}
String folderpath = fc.getSelectedFile().getAbsolutePath();
try{
formattedfolderpath = new BufferedReader(new FileReader(folderpath));
} catch (Exception e){
//
}
selecting();
有人有任何想法,我如何使用变量来完成使用 JFileChooser 的文件路径吗?
谢谢,
Michael
英文:
Recently I've been trying to use a JFileChooser to select where a database will be created; however, the problem I've run into is that the file path that I got from the JFileChooser has it has backslashes instead of forward slashes, and I think that this is what isn't allowing me to create the database. Here is my code, and attempt at solving the problem. <br/>
try {
// Try to connect to the database
DriverManager.registerDriver(new org.apache.derby.jdbc.EmbeddedDriver());
databaseconnection = DriverManager.getConnection("jdbc:derby:"+formattedfolderpath+";");
databaseconnection.setAutoCommit(false);
currentdb = true;
} catch (SQLException EX) {
try {
// Create the DB if it doesn't exist yet
DriverManager.registerDriver(new org.apache.derby.jdbc.EmbeddedDriver());
databaseconnection = DriverManager.getConnection("jdbc:derby:"+formattedfolderpath+";create=true");
databaseconnection.setAutoCommit(false);
currentdb = true;
} catch (SQLException EX2) {
//infoBox("OH MY LAWD", "Error");
}
<br/> and <br/>
JButton open = new JButton();
JFileChooser fc = new JFileChooser();
fc.setCurrentDirectory(new java.io.File("C:/Users/1jenningst/Desktop"));
fc.setDialogTitle("PDF Manager");
fc.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
if (fc.showOpenDialog(open) == JFileChooser.APPROVE_OPTION){
//
}
String folderpath = fc.getSelectedFile().getAbsolutePath();
try{
formattedfolderpath = new BufferedReader(new FileReader(folderpath));
} catch (Exception e){
//
}
selecting();
}
Anyone have any ideas on how I could use a variable to complete the file path using a JFileChooser? <br/>
Thanks, <br/>
Michael
答案1
得分: 0
Ok, <br/>
我只需要在文件路径中添加两个反斜杠,而不是一个:<br/>
C\users\missouri\desktop\123
变成
C\\users\\missouri\\desktop\\123
希望这能帮助,<br/>
Trevor
英文:
Ok, <br/>
I just needed to add two backslashes to the file path, instead of one: <br/>
C\users\missouri\desktop\123
becomes
C\\users\\missouri\\desktop\\123
Hope this helps, <br/>
Trevor
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论