Olá pessoal do guj, estou desenvolvendo uma pequena aplicação e estou testando o sqlite.
O meu interesse a princípio é o simples fato de abrir um txt e inserir um informaçãoes em uma tabela que possui os campos: cod, desc, valor e foto.
Como disse a princípio estou apenas testando o SQLite implementando o código abaixo:
package sqlite;
import java.sql.*;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
public class InsereLista
{
public static void main(String[] args) throws Exception
{
Class.forName("org.sqlite.JDBC");
Connection conn = DriverManager.getConnection("jdbc:sqlite:test3.db");
Statement stat = conn.createStatement();
stat.executeUpdate("drop table if exists produtos;");
stat.executeUpdate("CREATE TABLE produtos ( cod INTEGER NOT NULL PRIMARY KEY DEFAULT '\"0\"' , DESC CHAR ( 255 ) NOT NULL , valor DECIMAL ( 10 , 2 ) NOT NULL DEFAULT '''\"0 . 0\"''' , foto BLOB ) ; ");
PreparedStatement prep = conn.prepareStatement("insert into produtos values (?,?,?,?);");
File file = new File("c:\\foto.jpg");
FileInputStream fis = null;
//TXT
String filePath = "c:\\lista_2.txt";
try {
BufferedReader in = new BufferedReader(new FileReader(filePath));
String str;
fis = new FileInputStream(file);
while ((str = in.readLine()) != null)
{
String data[] = str.split("\t");
prep.setString(3, data[2]); //valor
prep.setString(2, data[1]); //descrição
prep.setBinaryStream(4, fis, (int) file.length()); //arquivo de foto em binário
prep.addBatch();
}
in.close();
fis.close();
}
catch (Exception e)
{
e.printStackTrace();
System.out.println("Erro ao ler arquivo de texto " + e.toString());
}
conn.setAutoCommit(false);
prep.executeBatch();
conn.setAutoCommit(true);
ResultSet rs = stat.executeQuery("select * from produtos;");
while (rs.next()) {
System.out.println(rs.getString("DESC")+" - " + rs.getString("VALOR"));
}
rs.close();
conn.close();
}
}
o erro retornado:
init:
deps-jar:
compile-single:
run-single:
java.sql.SQLException: not implemented by SQLite JDBC driver
Erro ao ler arquivo de texto java.sql.SQLException: not implemented by SQLite JDBC driver
at org.sqlite.Unused.unused(Unused.java:29)
at org.sqlite.Unused.setBinaryStream(Unused.java:58)
at sqlite.InsereLista.main(InsereLista.java:51)
Antes de mais nada, se alguém tiver o link da documentação do sqlite para jdbc ficarei grato, pois o único que encontrei foi este:
http://www.zentus.com/sqlitejdbc/
Estudando como seria o mesmo procedimento no MySQL executei o seguinte código com sucesso:
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package sqlite;
/**
*
* @author Leonam
*/
/*
Defining the Table: Oracle and MySql
create table MyPictures (
id INT PRIMARY KEY,
name VARCHAR(0),
photo BLOB
);
*/
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;
public class ExemploMySql {
public static void main(String[] args) throws Exception, IOException, SQLException {
Class.forName("org.gjt.mm.mysql.Driver");
Connection conn = DriverManager.getConnection("jdbc:mysql://localhost/blobfoto", "root", "senha");
String INSERT_PICTURE = "insert into MyPictures(name, photo) values (?, ?)";
File file = new File("c:\\foto.jpg");
FileInputStream fis = null;
PreparedStatement ps = null;
try {
conn.setAutoCommit(false);
fis = new FileInputStream(file);
ps = conn.prepareStatement(INSERT_PICTURE);
ps.setString(1, "name");
ps.setBinaryStream(2, fis, (int) file.length());
ps.executeUpdate();
conn.commit();
}
finally
{
ps.close();
fis.close();
}
}
}
Caso alguém tenha alguma sugestão, ficarei muito grato!
Um grande abraço a todos!