Skip to Main Content
  • Questions
  • Java Codes for inserting list of directories in database

Breadcrumb

Question and Answer

Connor McDonald

Thanks for the question, Justin Kent.

Asked: September 01, 2016 - 1:21 am UTC

Last updated: September 01, 2016 - 4:03 am UTC

Version: Oracle Database 12c

Viewed 1000+ times

You Asked

Hi Tom,

This is a java programming language and i'm going to insert the directory path and filename in DB but my problem is the directory path without filename doesnt insert in DB it supposed to be the column FILE_NAMES is already null but it doesnt insert at all. i am using JDeveloper. Can you help me regarding this matter?

here is my java code:

package filedirectory;

import java.io.File;
import java.io.IOException;

import java.io.InputStream;

import java.nio.file.Path;
import java.nio.file.Paths;

import java.sql.*;


public class filedir {
private static InputStream is;

public filedir() {
super();
}
public void listDirectory(String dirPath, int level) {
File dir = new File(dirPath);

File[] firstlevelFiles = dir.listFiles();
try {
if(firstlevelFiles != null && firstlevelFiles.length > 0) {
for(File aFile : firstlevelFiles) {
for(int i=0; i<level; i++) {
System.out.print("\t");
}
if(aFile.isDirectory()) {
System.out.println("Folder Name: [" + aFile.getName() + "]");
listDirectory(aFile.getAbsolutePath(),level + 1);
} else {

System.out.println(aFile.getName());
try {
Path path = Paths.get(aFile.getCanonicalPath());
Path p1 = Paths.get(aFile.getCanonicalPath()).getParent();
Path p2 = path.getFileName();


Class.forName("oracle.jdbc.OracleDriver");
Connection con = null;
Statement stmt = null;

con = DriverManager.getConnection("jdbc:oracle:thin:@172.26.171.41:1721:mmwdev","stgodidev","stgodidev");
stmt = con.createStatement();
ResultSet rs = stmt.executeQuery("insert into new_file_names values ('" + p1 + "','" + p2 + "')");
con.close();

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

}
} catch(Exception e) {
System.out.println(e);

}
}
public static void main(String[] args) {
filedir filedir = new filedir();
String dirToList = System.getProperty("user.home") + File.separator + "Documents";
filedir.listDirectory(dirToList, 0);

}
}

Thanks,
Justin

and Connor said...

Looks fine to me...(not withstanding standard comments about bind variables etc)

C:\temp>java filedir
Folder Name: [local]
        Folder Name: [include]
                FlexLexer.h
        Folder Name: [lib]
                libfl.a
                libfl.lib
        md5sum
        Folder Name: [share]
                bison.hairy
                bison.simple
        Folder Name: [wbin]
                agrep.exe
                ansi2knr.exe
                awk.exe
                basename.exe
                bash.exe
...
...

SQL> select * from new_file_names;

X                                                  Y
-------------------------------------------------- ----------------------------------------
C:\bin\usr\local\include                           FlexLexer.h
C:\bin\usr\local\lib                               libfl.a
C:\bin\usr\local\lib                               libfl.lib
C:\bin\usr\local                                   md5sum
C:\bin\usr\local\share                             bison.hairy
C:\bin\usr\local\share                             bison.simple
C:\bin\usr\local\wbin                              agrep.exe
C:\bin\usr\local\wbin                              ansi2knr.exe
C:\bin\usr\local\wbin                              awk.exe
C:\bin\usr\local\wbin                              basename.exe
C:\bin\usr\local\wbin                              bash.exe
C:\bin\usr\local\wbin                              bc.exe
C:\bin\usr\local\wbin                              bison.exe
C:\bin\usr\local\wbin                              bunzip2.exe
C:\bin\usr\local\wbin                              bzip2.exe
C:\bin\usr\local\wbin                              bzip2recover.exe
C:\bin\usr\local\wbin                              cat.exe
C:\bin\usr\local\wbin                              chgrp.exe
C:\bin\usr\local\wbin                              chmod.exe
C:\bin\usr\local\wbin                              chown.exe
C:\bin\usr\local\wbin                              cksum.exe
C:\bin\usr\local\wbin                              cmp.exe
C:\bin\usr\local\wbin                              comm.exe
C:\bin\usr\local\wbin                              compress.exe
C:\bin\usr\local\wbin                              cp.exe
C:\bin\usr\local\wbin                              cpp.exe
C:\bin\usr\local\wbin                              csplit.exe
C:\bin\usr\local\wbin                              cut.exe
C:\bin\usr\local\wbin                              cygwin1.dll
C:\bin\usr\local\wbin                              cygwinb19.dll



Is this answer out of date? If it is, please let us know via a Comment

More to Explore

PL/SQL demos

Check out more PL/SQL tutorials on our LiveSQL tool.

PL/SQL docs

PL/SQL reference manual from the Oracle documentation library