This question already has answers here:
Import CSV into SQLite in Java
(2 answers)
Closed 2 years ago.
I am new to SQLite and I want to import data from an external file like csv into this embedded database rather than adding the data manually. For example in this case i added John McNeil and Paul Smith manually..is there a way to not add in manually by reading from a csv file with columns filled with first name and last name.
public class SQLiteTest {
private static Connection con;
private static boolean hasData = false;
private void getConnection() throws ClassNotFoundException, SQLException {
// sqlite driver
Class.forName("org.sqlite.JDBC");
// database path, if it's new database, it will be created in the project folder
con = DriverManager.getConnection("jdbc:sqlite:SQLiteTest1.db");
initialise();
}
public void addUser(String firstname, String lastname) throws ClassNotFoundException, SQLException {
if(con == null) {
// get connection
getConnection();
}
PreparedStatement prep = con
.prepareStatement("insert into user values(?,?,?);");
prep.setString(2, firstname);
prep.setString(3, lastname);
prep.execute();
}
public ResultSet displayUsers() throws SQLException, ClassNotFoundException {
if(con == null) {
// get connection
getConnection();
}
Statement state = con.createStatement();
ResultSet res = state.executeQuery("select fname, lname from user");
return res;
}
private void initialise() throws SQLException {
if( !hasData ) {
hasData = true;
// check for database table
Statement state = con.createStatement();
ResultSet res = state.executeQuery("SELECT name FROM sqlite_master WHERE type='table' AND name='user'");
if( !res.next()) {
System.out.println("Building the User table with prepopulated values.");
// need to build the table
Statement state2 = con.createStatement();
state2.executeUpdate("create table user(id integer,"
+ "fName varchar(60)," + "lname varchar(60)," + "primary key (id));");
// inserting some sample data
PreparedStatement prep = con.prepareStatement("insert into user values(?,?,?);");
prep.setString(2, "John");
prep.setString(3, "McNeil");
prep.execute();
PreparedStatement prep2 = con.prepareStatement("insert into user values(?,?,?);");
prep2.setString(2, "Paul");
prep2.setString(3, "Smith");
prep2.execute();
}
}
}
}
In enterprise applications, they use database migration tools like liquibase to do that.
It's well integrated with spring boot using auto configuration. but if you are not using spring you can configure it yourself.
You will have a XML file like that.
<?xml version="1.0" encoding="utf-8"?>
<databaseChangeLog
xmlns="http://www.liquibase.org/xml/ns/dbchangelog"
xmlns:ext="http://www.liquibase.org/xml/ns/dbchangelog-ext"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.liquibase.org/xml/ns/dbchangelog
http://www.liquibase.org/xml/ns/dbchangelog/dbchangelog-3.6.xsd
http://www.liquibase.org/xml/ns/dbchangelog-ext
http://www.liquibase.org/xml/ns/dbchangelog/dbchangelog-ext.xsd">
<changeSet id="11111" author="me">
<loadData
file="data/users.csv"
separator=";"
tableName="user"/>
</changeSet>
</databaseChangeLog>
and you can have your data written in CSV file like that
id;login;first_name;last_name;email
1;John;John;John;John#localhost
I am trying store JGeometry to Oracle database with the following code:
#PersistenceContext
private EntityManager entityManager;
...
Session session = entityManager.unwrap(Session.class);
session.doWork(new Work() {
#Override
public void execute(Connection connection) throws SQLException {
PreparedStatement ps = connection.prepareStatement("UPDATE SAMPLE_AREA SET GEOMETRY=? WHERE ID = " + sampleAreaId + " AND SAMPLE_ID = " + sampleId);
System.out.println(connection); // prints: org.hibernate.engine.jdbc.internal.proxy.ConnectionProxyHandler#321ca777[valid=true]
System.out.println(connection.getClass().getName()); // prints: com.sun.proxy.$Proxy125
STRUCT obj = JGeometry.store(jGeometry, connection);
ps.setObject(1, obj);
ps.execute();
}
});
I am getting java.lang.ClassCastException: com.sun.proxy.$Proxy125 cannot be cast to oracle.jdbc.OracleConnection when calling JGeometry.store.
How can I get OracleConnection?
I am using "hibernate.dialect = org.hibernate.dialect.Oracle10gDialect"
I figured out the solution. Instead of using Connection as parameter to JGeometry.store one needs to use OracleConnection. That can be unwrapped from Connection. I think I tried this before, but most likely I had wrong import, correct is oracle.jdbc.OracleConnection.
import oracle.jdbc.OracleConnection;
...
OracleConnection oracleConnection = connection.unwrap( OracleConnection.class );
STRUCT obj = JGeometry.store(jGeometry, oracleConnection);
I am using MS SQL SERVER 2008 database in which i create all objects like procedure,functions,views etc. Now create a sample program get all objects definition(ddl), Here i found that some of objects definition is null i used following sql query to get definition of all procedures:
Select object_definition(object_id) from sys.objects where type = 'P'
Here are my sample code:
public class sam {
String userName;
String dbURL;
String password;
public sam(String dbURL, String userName, String password) {
this.dbURL = dbURL;
this.userName = userName;
this.password = password;
}
public void createSQLFile() throws IOException, SQLException, ClassNotFoundException {
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
Connection con = DriverManager.getConnection(dbURL, userName, password);
Statement statement = con.createStatement();
String queryString = "Select object_definition(object_id) from sys.objects where type = 'P'";
ResultSet rs = statement.executeQuery(queryString);
while (rs.next()) {
System.out.println(rs.getString(1));
}
}
public static void main(String[] args) throws SQLException, IOException, ClassNotFoundException {
String dbURl = "jdbc:sqlserver://localhost:1433;DatabaseName=db";
String userName = "u";
String password = "P";
new sam(dbURl, userName, password).createSQLFile();
}
} Also i have attach a screen shots of my database where procedure listed (Note: The highlighted procedure deification are null).
Can anyone suggest- How to get these object defination as they are encrypted?
MSDN states
The definition of user objects is visible to the object owner or grantees that have any one of the following permissions:
ALTER,
CONTROL,
TAKE OWNERSHIP,
or VIEW DEFINITION.
New to Java and MySQL.
Am using a DAO object to query a table, running via Eclipse. MySQL edited via Workbench. table exists and Getting the following exceptions:
SELECT movie_name, release_dd, release_mm, release_yyyy, duration, language, director, genre, actor_1, actor_2 FROM movie_details_table WHERE movie_name = 'Piku'
java.sql.SQLException: Before start of result set
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:998)
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:937)
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:926)
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:872)
at com.mysql.jdbc.ResultSetImpl.checkRowPos(ResultSetImpl.java:787)
at com.mysql.jdbc.ResultSetImpl.getStringInternal(ResultSetImpl.java:5244)
at com.mysql.jdbc.ResultSetImpl.getString(ResultSetImpl.java:5167)
at com.mysql.jdbc.ResultSetImpl.getString(ResultSetImpl.java:5206)
at com.library.model.MovieDAO.getMovieDetails(MovieDAO.java:41)
at com.library.model.MovieDetTest.main(MovieDetTest.java:18)
MovieDAO class:
package com.library.model;
import java.util.*;
import java.sql.*;
import java.io.*;
import com.library.model.beans.*;
public class MovieDAO {
private static final String DB_URL =
"jdbc:mysql://localhost/planner";
// Database credentials
private static final String USER = "Sudipto";
private static final String PASS = "sudi85";
public MovieDetails getMovieDetails(String inputMov) throws
SQLException {
MovieDetails movieDetails = new MovieDetails();
try {
//Open a connection
Connection conn = DriverManager.getConnection
(DB_URL,USER,PASS);
//Create and execute query
String queryString = "SELECT movie_name, release_dd, release_mm, release_yyyy, duration, language, director, genre, actor_1, actor_2 FROM movie_details_table WHERE movie_name = '" + inputMov + "'";
System.out.println(queryString);
PreparedStatement statement = conn.prepareStatement
(queryString);
ResultSet rsMovieDetails = statement.executeQuery();
movieDetails.setMovieName(rsMovieDetails.getString
("movie_name"));
movieDetails.setReleaseDate
(rsMovieDetails.getInt ("release_dd"), rsMovieDetails.getInt ("release_mm"), rsMovieDetails.getInt ("release_yyyy"));
movieDetails.setDuration(rsMovieDetails.getInt
("duration"));
movieDetails.setLanguage(rsMovieDetails.getString
("language"));
movieDetails.setDirector(rsMovieDetails.getString
("director"));
movieDetails.setGenre(rsMovieDetails.getString
("genre"));
movieDetails.setActor1(rsMovieDetails.getString
("actor_1"));
movieDetails.setActor2(rsMovieDetails.getString
("actor_2"));
}
catch (SQLException e) {
e.printStackTrace();
}
return movieDetails;
}
}
Have the following error log in MySQL workbench:
2015-05-31T15:04:36, 27, Note, Aborted connection 27 to db: 'planner' user: 'Sudipto' host: 'localhost' (Got an error reading communication packets)
Can anyone please suggest how and what I need to fix?
Use rsMovieDetails.next() to retrive details. Like rs.next() is used in https://docs.oracle.com/javase/tutorial/jdbc/basics/retrieving.html.
rs.next() shifts the cursor to the next row of the result set from the database and returns true if there is any row, otherwise returns false. If row is present then u should retrieve the data
I am new to apache derby and I cant seem to make work
CREATE TABLE IF NOT EXISTS table1 ...
as can be achieved in MySql etc. I am getting a 'Syntax error: Encountered "NOT" at line 1, column 17.', when I try to run this SQL statement in my Java program.
I checked in the documentation page for Derby Db Create Statements, but couldn't find such an alternative.
Create the table, catch the SQLException and check SQL status code.
The full list of error codes can be found here but I couldn't find Table <value> already exists; it's probably X0Y68. The code you need is X0Y32.
Just run the code once and print the error code. Don't forget to add a test to make sure the code works; this way, you can catch changes in the error code (should not happen ...).
In my projects, I usually add a helper class with static methods so I can write:
} catch( SQLException e ) {
if( DerbyHelper.tableAlreadyExists( e ) ) {
return; // That's OK
}
throw e;
}
Another option is to run a SELECT against the table and check the status code (which should be 42X05). But that's a second command you need to send and it doesn't offer any additional information.
What's worse, it can fail for other reasons than "Table doesn't exist", so the "create-and-ignore-error" is better IMO.
Derby does not support that sql-statement.
In my program I parse all the Tables from the Database into a Set and check if the table exists there.
Like this:
private Set<String> getDBTables(Connection targetDBConn) throws SQLException
{
Set<String> set = new HashSet<String>();
DatabaseMetaData dbmeta = targetDBConn.getMetaData();
readDBTable(set, dbmeta, "TABLE", null);
readDBTable(set, dbmeta, "VIEW", null);
return set;
}
private void readDBTable(Set<String> set, DatabaseMetaData dbmeta, String searchCriteria, String schema)
throws SQLException
{
ResultSet rs = dbmeta.getTables(null, schema, null, new String[]
{ searchCriteria });
while (rs.next())
{
set.add(rs.getString("TABLE_NAME").toLowerCase());
}
}
the query you are executing does not supported by Derby db. Instead, if you know the name of the table you can find if table exists or not quite easily.
public boolean isTableExist(String sTablename) throws SQLException{
if(connection!=null)
{
DatabaseMetaData dbmd = connection.getMetaData();
ResultSet rs = dbmd.getTables(null, null, sTablename.toUpperCase(),null);
if(rs.next())
{
System.out.println("Table "+rs.getString("TABLE_NAME")+"already exists !!");
}
else
{
System.out.println("Write your create table function here !!!");
}
return true;
}
return false;
}
Catch is to specify name of the table in Uppercase else you won't be able to find table name in metadata.
to check if table is exist :
Connection con = DriverManager.getConnection(url);
ResultSet res = con.getMetaData().getTables(null, Schema_Name, table_name.toUpperCase(), null);//Default schema name is "APP"
if(res.next())
{
//do some thing;
}else{
JOptionPane.showMessageDialog(null, table_name +" not exist");
}
to show all tables name :
Connection con = DriverManager.getConnection(url);
ResultSet res = con.getMetaData().getTables(null, Schema_Name, "%", null);//Default schema name is "APP"
while(res.next())
{
JOptionPane.showMessageDialog(null, res.getString(3) + " is exist");//Show table name
}else{
JOptionPane.showMessageDialog(null, table_name +" not exist");
}
Following Aaron Digulla's lead with a DerbyUtils class to check if the table exists, this is the solution I came up with :
Calling class
public void createTable(String name) {
Connection connection = null;
PreparedStatement preparedStatement = null;
try {
connection = daoFactory.getConnection();
String sql = String.format(SQL_CREATE_TABLE, name);
preparedStatement = connection.prepareStatement(sql, Statement.NO_GENERATED_KEYS);
preparedStatement.execute();
} catch (SQLException e) {
if(DerbyUtils.tableAlreadyExists(e)) { //check if the exception is because of pre-existing table.
logger.info("Talbe " + name + " already exists. No need to recreate");
} else {
logger.error(e.getMessage() + " : " + e.getStackTrace());
}
} finally {
close(connection, preparedStatement); //DAOUtils silently closes
}
}
DerbyUtils
public class DerbyUtils {
public DerbyUtils() {
//empty constructor -- helper class
}
public static boolean tableAlreadyExists(SQLException e) {
boolean exists;
if(e.getSQLState().equals("X0Y32")) {
exists = true;
} else {
exists = false;
}
return exists;
}
}
See also
https://db.apache.org/derby/docs/10.2/ref/rrefexcept71493.html
I know this was marked with an answer but in case anyone wanted another way of checking I wanted to post anyway. Here I check the table metadata with a method that returns a boolean, true if exists, false if it doesn't. Hope it helps others if they are looking.
private static Connection conn = null;
private static Statement st = null;
private static ResultSet rs = null;
private static DatabaseMetaData dmd;
public Boolean firstTime()
{
try
{
dmd = conn.getMetaData();
rs = dmd.getTables(null, "APP", "LOGIN", null);
return !rs.next();
} catch (SQLException ex)
{
Logger.getLogger(Database.class.getName()).log(Level.SEVERE, null, ex);
return false;
}
}
Another solution with 2 conditions:
Willing to drop table before creating each time, with the same being present in a .sql file
Are using Spring and hence willing to use spring-test as a Maven dependency, your life can become much simpler with it's #Sql annotation
So, firstly adding this as a dependency to your pom:
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>4.2.5.RELEASE</version>
<scope>test</scope>
</dependency>
Secondly, assuming you have an sql that drops, creates table a in a file
rectangle.sql:
DROP TABLE rectangles;
CREATE TABLE rectangles (
id INTEGER NOT NULL PRIMARY KEY,
width INTEGER NOT NULL,
height INTEGER NOT NULL
);
And you have a test class BlahTest that should run this sql before doing whatever test it is to run, simply add the following #Sql annotation to your class:
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.jdbc.Sql;
import org.springframework.test.context.jdbc.SqlConfig;
import org.springframework.test.context.jdbc.SqlConfig.ErrorMode;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
#RunWith(SpringJUnit4ClassRunner.class)
#ContextConfiguration(classes=XyzClientConfig.class)
#Sql(scripts="/sql/ddl/rectangle.sql", config=#SqlConfig (errorMode=ErrorMode.IGNORE_FAILED_DROPS))
public class BlahTest {
...
}
The specified config attribute value's #SqlConfig has the magic that makes it skip the drop statement errors in case the table doesn't exist. I believe it's been written to specifically target these types of databases that don't support IF EXISTS for dropping / table creation (which derby really should, even if it's not part of the SQL standard at the moment)
This answer is way late, but it might be helpful for someone.
The following Java (standard JDBC) code can be used to check whether a table exists or not, and if it does then it can be created;
String query = "SELECT TRUE FROM SYS.SYSTABLES WHERE TABLENAME = ? AND TABLETYPE = 'T'"; // Leave TABLETYPE out if you don't care about it
PreparedStatement ps = connection.prepareStatement(query);
ps.setString(1, "TABLE_NAME"); // Must be in capitals
ResultSet rs = ps.executeQuery();
if ( rs.next() && rs.getBoolean(1) )
{
// Table exists
}
else
{
// Table does NOT exist ... create it
}
Here is a solution that will you can script in SQL.
Create a Class like the following:
package user.fenris.spring.extensions;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.datasource.SingleConnectionDataSource;
public class SqlCreateIfNotExists {
private static Log log = LogFactory.getLog(SqlCreateIfNotExists.class);
public static void createTableIfNotExists(String tablename, String ddl) throws SQLException {
Connection conn = DriverManager.getConnection("jdbc:default:connection");
if (conn != null) {
JdbcTemplate template = new JdbcTemplate(new SingleConnectionDataSource(conn, true));
int count = template.queryForInt("select count(*) from SYS.SYSTABLES where TABLENAME = ?", tablename);
log.debug("Count: " + count);
if (count == 0) {
log.debug("Executing sql statement: " + ddl);
template.execute(sql);
} else {
log.debug("Table exists. Skipping sql execution...");
}
}
}
}
Note: you don't have to use spring, you can write it in straight JDBC, but then you have to know how to do it correctly. (Left as an exercise for the reader). Also, you could rewrite this to parse out the table name from the ddl parameter. Another thing would be to do proper error handling.
Make sure the class is compiled and placed in the classpath of the VM the database will be running in.
Write your SQL script:
-- 2K for ddl statement should be enough. You want more? Seriously?
create procedure CreateTableIfNotExists(in tablename varchar(128), in ddl varchar(2048))
PARAMETER STYLE JAVA
MODIFIES SQL DATA
language java
external name 'user.fenris.spring.extensions.SqlCreateIfNotExists.createTableIfNotExists';
call CreateTableIfNotExists('TABLE_NAME_MUST_BE_ALL_CAPS',
'create table TABLE_NAME_MUST_BE_ALL_CAPS
(entry_id int generated always as identity not null,
entry_timestamp timestamp,
username varchar(128) not null,
note varchar(1024) not null,
primary key (entry_id))');
-- you don't have to drop this, but you would have to create a similar
-- procedure to create the CreateTableIfNotExists procedure,
-- (i.e. CreateProcedureIfNotExists) but then it's turtles all the way down
drop procedure CreateIfNotExists;
???
profit
try {
connection.createStatement().execute("create table channels(channel varchar(20),topic varchar(20))");
} catch (Exception e) {
// TODO Auto-generated catch block
// e.printStackTrace();
}
Surround the create statement by try-catch.and make sure comment the e.printstacktace();
if it is already exists it does not show error ,otherwise it create table..!!