I used following code for retrieve data from SQLite database. The 4th column of database stored string as BLOB. Now i need to get that string values back. But Blob blob = exe.getBlob("col_4"); gave me an exception not implemented by SQLite JDBC driver. How can i solve this ?
Connection c = null;
Statement stm = null;
String db = "C:\\Users\\Asus™\\Desktop\\New folder (2)\\tempo.db";
try {
Class.forName("org.sqlite.JDBC");
c = DriverManager.getConnection("jdbc:sqlite:" + db);
c.setAutoCommit(false);
System.out.println("DB opened successfully !");
stm = c.createStatement();
int rc = 0;
try (ResultSet exe = stm.executeQuery("SELECT * FROM tblTest;")) {
while (exe.next()) {
Blob blob = exe.getBlob("col_4");
String password = new String(blob.getBytes(1, (int) blob.length()));
System.out.println(password
+ exe.getString("col_1") + "\t"
+ exe.getString("col_2") + "\t"
+ exe.getString("col_3") + "\t"
+ password
);
rc++;
}
System.out.println(rc++);
}
stm.close();
} catch (ClassNotFoundException ex) {
Logger.getLogger(SQLiteBrowser.class.getName()).log(Level.SEVERE, null, ex);
} catch (SQLException ex) {
Logger.getLogger(SQLiteBrowser.class.getName()).log(Level.SEVERE, null, ex);
}
Exception :
java.sql.SQLException: not implemented by SQLite JDBC driver
at org.sqlite.jdbc4.JDBC4ResultSet.unused(JDBC4ResultSet.java:320)
at org.sqlite.jdbc4.JDBC4ResultSet.getBlob(JDBC4ResultSet.java:345)
at sqlite.SQLiteBrowser.jButton1ActionPerformed(SQLiteBrowser.java:256)
at sqlite.SQLiteBrowser.access$000(SQLiteBrowser.java:29)
at sqlite.SQLiteBrowser$1.actionPerformed(SQLiteBrowser.java:76)
at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:2022)
at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2346)
at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:402)
at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:259)
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:252)
at java.awt.Component.processMouseEvent(Component.java:6525)
at javax.swing.JComponent.processMouseEvent(JComponent.java:3321)
It seems, that ResultSet#getBlob method is not supported by this JDBC library implementation (due to sources, JDBC4ResultSet doesn't implement it). You may try to use ResultSet#getBytes instead:
byte[] bytes = exe.getBytes("col_4");
String password = new String(bytes);
getBlob() method is not supportedby SQLite ... but blobs are supported ... you need to use getBytes :
byte[] bytes = exe.getBytes("col_4");
Related
Unable to use copy command with jdbc Postgres. Whats wrong with the below code snippet sample.
public boolean loadReportToDB(String date) {
// TODO Auto-generated method stub
Connection connection = DBUtil.getConnection("POSTGRESS");
String fileName = "C:/_0STUFF/NSE_DATA/nseoi_" + date + ".csv";
String sql = "\\copy fno_oi FROM 'C:\\_0STUFF\\NSE_DATA\\nseoi_27102017.csv' DELIMITER ',' CSV header";
try {
PreparedStatement ps = connection.prepareStatement(sql);
System.out.println("query"+ps.toString());
int rowsaffected = ps.executeUpdate();
System.out.println("INT+" + rowsaffected);
return true;
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return false;
}
org.postgresql.util.PSQLException: ERROR: syntax error at or near "\"
Position: 1
at org.
if we use
String sql = "copy fno_oi FROM 'C:\\_0STUFF\\NSE_DATA\\nseoi_27102017.csv' DELIMITER ',' CSV header";
then no rows are updated
postgres version postgresql-10.0-1-windows-x64
This works for me:
try (Connection conn = DriverManager.getConnection(connUrl, myUid, myPwd)) {
long rowsInserted = new CopyManager((BaseConnection) conn)
.copyIn(
"COPY table1 FROM STDIN (FORMAT csv, HEADER)",
new BufferedReader(new FileReader("C:/Users/gord/Desktop/testdata.csv"))
);
System.out.printf("%d row(s) inserted%n", rowsInserted);
}
Using copyIn(String sql, Reader from) has the advantage of avoiding issues where the PostgreSQL server process is unable to read the file directly, either because it lacks permissions (like reading files on my Desktop) or because the file is not local to the machine where the PostgreSQL server is running.
As your input file is stored locally on the computer running your Java program you need to use the equivalent of copy ... from stdin in JDBC because copy can only access files on the server (where Postgres is running).
To do that use the CopyManager API provided by the JDBC driver.
Something along the lines:
Connection connection = DBUtil.getConnection("POSTGRES");
String fileName = "C:/_0STUFF/NSE_DATA/nseoi_" + date + ".csv";
String sql = "copy fno_oi FROM stdin DELIMITER ',' CSV header";
BaseConnection pgcon = (BaseConnection)conection;
CopyManager mgr = new CopyManager(pgcon);
try {
Reader in = new BufferedReader(new FileReader(new File(fileName)));
long rowsaffected = mgr.copyIn(sql, in);
System.out.println("Rows copied: " + rowsaffected);
} catch (SQLException e) {
e.printStackTrace();
}
I am reading some data from a SQLite table by JDBC. Where the table has following columns:
[Id:Integer], [parentId:Integer], [Name:String], [Type:Integer], [Data:BLOB]
Now from the BLOB data I need to create some Unique identifier, thus the same BLOB will generate the same identifier every time. As of now I am creating a byte array from the blob and then making a toString of it. Will it guarantees the uniqueness? And Is it CPU cycle efficient? As I have a lots of such records to process. Please suggest. Following is my code for the same.
public static void scanData(String dbName) {
String url = "jdbc:sqlite:C:/dbfolder/" + dbName;
try (Connection con = DriverManager.getConnection(url);
Statement st = con.createStatement();
ResultSet rs = st.executeQuery("select * from someTable");) {
while (rs.next()) {
Integer type = rs.getInt("Type");
if (type != null && type.equals(3)) {
Integer rowId = rs.getInt("Id");
Integer parentId = rs.getInt("ParentID");
String name = rs.getString("Name");
System.out.println("rowId = " + rowId);
System.out.println("parentId = " + parentId);
System.out.println("name = " + name);
System.out.println("type = " + type);
InputStream is = rs.getBinaryStream("Data");
if (is != null) {
byte[] arr = IOUtils.toByteArray(is);
if (arr != null) {
System.out.println("Data = " + arr.toString());
}
}
System.out.println("---------------------------");
}
}
} catch (SQLException e) {
System.out.println(e.getMessage());
} catch (IOException ioe) {
System.out.println(ioe.getMessage());
}
}
** IOUtils is used of : org.apache.commons.io.IOUtils
To generate a unique identifier you can use org.apache.commons.codec.digest.DigestUtils to generate sha256 so that it will be unique for every blob type
DigestUtils.sha256Hex(new FileInputStream(file));
Convert the Blob to inputStream
blob.getBinaryStream();
Unable to use copy command with jdbc Postgres. Whats wrong with the below code snippet sample.
public boolean loadReportToDB(String date) {
// TODO Auto-generated method stub
Connection connection = DBUtil.getConnection("POSTGRESS");
String fileName = "C:/_0STUFF/NSE_DATA/nseoi_" + date + ".csv";
String sql = "\\copy fno_oi FROM 'C:\\_0STUFF\\NSE_DATA\\nseoi_27102017.csv' DELIMITER ',' CSV header";
try {
PreparedStatement ps = connection.prepareStatement(sql);
System.out.println("query"+ps.toString());
int rowsaffected = ps.executeUpdate();
System.out.println("INT+" + rowsaffected);
return true;
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return false;
}
org.postgresql.util.PSQLException: ERROR: syntax error at or near "\"
Position: 1
at org.
if we use
String sql = "copy fno_oi FROM 'C:\\_0STUFF\\NSE_DATA\\nseoi_27102017.csv' DELIMITER ',' CSV header";
then no rows are updated
postgres version postgresql-10.0-1-windows-x64
This works for me:
try (Connection conn = DriverManager.getConnection(connUrl, myUid, myPwd)) {
long rowsInserted = new CopyManager((BaseConnection) conn)
.copyIn(
"COPY table1 FROM STDIN (FORMAT csv, HEADER)",
new BufferedReader(new FileReader("C:/Users/gord/Desktop/testdata.csv"))
);
System.out.printf("%d row(s) inserted%n", rowsInserted);
}
Using copyIn(String sql, Reader from) has the advantage of avoiding issues where the PostgreSQL server process is unable to read the file directly, either because it lacks permissions (like reading files on my Desktop) or because the file is not local to the machine where the PostgreSQL server is running.
As your input file is stored locally on the computer running your Java program you need to use the equivalent of copy ... from stdin in JDBC because copy can only access files on the server (where Postgres is running).
To do that use the CopyManager API provided by the JDBC driver.
Something along the lines:
Connection connection = DBUtil.getConnection("POSTGRES");
String fileName = "C:/_0STUFF/NSE_DATA/nseoi_" + date + ".csv";
String sql = "copy fno_oi FROM stdin DELIMITER ',' CSV header";
BaseConnection pgcon = (BaseConnection)conection;
CopyManager mgr = new CopyManager(pgcon);
try {
Reader in = new BufferedReader(new FileReader(new File(fileName)));
long rowsaffected = mgr.copyIn(sql, in);
System.out.println("Rows copied: " + rowsaffected);
} catch (SQLException e) {
e.printStackTrace();
}
I'm trying to insert BLob into Oracle database using vert.x, i get the upload File
for (FileUpload f : routingContext.fileUploads()){
System.out.println("file name " + f.fileName());
System.out.println("size name " + f.size());
System.out.println("Uploaded File " + f.uploadedFileName());
}
I have converted FileUpload to bytes Array by using :
Buffer fileUploaded = routingContext.vertx().fileSystem().readFileBlocking(f.uploadedFileName());
byte[] fileUploadedBytes = fileUploaded.getBytes();
Now I want to insert it directly to the Oracle database, i have tried to use updateWithParams, but i don't know how to add Blob into the query params.
thank you for your help
this is my implementation to resolve my problem , now I can insert file Blob into the Oracle dataBase, I hope that will help someone in the future.
ByteArrayInputStream finalBis = bis;
byte[] finalFileUploadedBytes = fileUploadedBytes;
DB.getConnection(connection -> {
if (connection.succeeded()) {
CallableStatement stmt = null;
try {
stmt = connection.result().getConnection().prepareCall(SQL.INSERT_DOCS_QUERY);
stmt.setBinaryStream(1, finalBis, finalFileUploadedBytes.length);
stmt.setString(2,desiDoc);
stmt.setString(3,sourDoc);
logger.debug(stmt);
stmt.execute();
finalBis.close();
} catch (SQLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
} else {
System.out.println("nooot ok");
}
});
I am using the JDBC driver to connect to a mysql database and using the "LOAD DATA INFILE" command in my java application to load(insert) a text file into the database. I am getting the following error: Data truncation: Data too long for column xxx at row 1.
However if I load the same text file manually by logging into the database and entering the SQL manually, the data loads fine.
Can someone pelase tell me what the error might be?
I am running this on Red Hat Enterprise Linux Server release 5.8 and the jdk version is 1.5.0_16 if that helps
This is the function used to load the data
public static void loaddata(Connection conn, String filename, String tablename)
{
try{
Statement stmt = null;
stmt = conn.createStatement();
File file = new File(filename);
file.getAbsolutePath().replace("\\", "\\\\");
String cmd = "LOAD DATA INFILE '"
+ file.getAbsolutePath().replace("\\", "\\\\")
+ "' INTO TABLE " + tablename + " FIELDS TERMINATED BY \'^\'";
stmt.executeUpdate(cmd);
System.out.println("cmd :" + cmd);
}
catch(SQLException sqle){
sqle.printStackTrace();
}
}
This is the function to create the JDBC connection:
public static Connection createConnection()
{
Connection conn=null;
try {
Class.forName("com.mysql.jdbc.Driver").newInstance();
}
catch(Exception e) {
e.printStackTrace();
}
try {
String url = ""; //URL mentioned in the actual code
conn = DriverManager.getConnection(url, user, pass);
} catch (SQLException sqe1) {
sqe1.printStackTrace();
}
return conn;
}