Derby Embedded create tabel inorder run on any pc - java

Hi below code form from my Derby embedded example...
when i run on my pc(developed)it run smoothly.
And then i export as jar file and run on another pc .it throws an exception table already exists.
How can i create table only once on any pc
public class Main {
public static void main(String[] args) throws SQLException {
final String driver="org.apache.derby.jdbc.EmbeddedDriver";
final String url="jdbc:derby:db/testdb";
try {
Class.forName(driver);
Connection connection=DriverManager.getConnection(url);
//connection.createStatement().execute("create table channels(channel varchar(20),topic varchar(20))");
// connection.createStatement().execute("insert into channels (channel,topic) values('hbo','action')");
// System.out.println("saved");
PreparedStatement preStmt=connection.prepareStatement("select * from channels");
ResultSet set=null;
set=preStmt.executeQuery();
while(set.next()){
System.out.print(set.getString(1));
System.out.println(set.getString(2));
}
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
}
Also how to configure same issue on hibernate.cfg.xml file...!!

You can check to see if the table already exists before you create it.
Use the DatabaseMetadata calls to examine the structure of the database after you connect to it.
See: http://docs.oracle.com/javase/6/docs/api/java/sql/DatabaseMetaData.html

You can check before every creation check it in DB metadata:
Methods looks like following:
ResultSet result = databaseMetaData.getTables(
catalog, schemaPattern, tableNamePattern, types );
Abd you can use it as following:
ResultSet result = databaseMetaData.getTables(
null, null, TABLE_NAME, null );

Related

Incorrect data getting fetched from select query

Can someone review my code
This query is fetching two values address_id and postcode from table1. Here
AddressID class has two variable postcode(string) and address_id(integer) :
#Select("SELECT address_id,postcode FROM table1 WHERE custom_field_1 = #{caseid}")
public List<AddressID> getAddressIdPostCodeList(String caseid);
Here is how AddressID looks AddressID.java
private int addressId;
private String postcode;
//getters and setters of Pstcode and addressId
#Override
public String toString() {
return "PostCode : " + this.postcode;
}
while executing this query I get value of address_id as 0 and required postcode. Although DB has values of address_id which is not zero. Where my code is failing?
This is where in my main method I am calling
List<AddressID> addresses = new ArrayList<>();
addresses = mainClassObject.getAddressIdPostCodeList(address.getcaseId());
Ideally addresses object should have both address_id and postcode. I am getting both values but address_id I am getting 0 and correct values for postcode.
Since I cant comment because I dont have 50 reputation, I had to write it here, its hard to tell from the code you posted, to give you an answer I need more detail on what getAddressIdPostCodeList() does, since you said the data in the DB has no ceros the error must be in the method getAddressIdPostCodeList() and/or in how you are handling the resultset of the Query
EDIT: Solution using Oracle JDBC Driver
Since I dont know how to use Mybatis, heres a solution using JDBC.
to connect using JDBC to your Oracle DB here's a simple tutorial:
1. First you need to download de JDBC driver from Oracle depending your DB version (11g,12c,10g), the driver Its called ojdbcX.jar where X is a number of the version of the driver
2. After you have downloaded the driver you need to add the .jar to your project, if you are using Netbeans IDE you can add it like this:
if you are using Eclipse you can use the following Link to see how to add the .JAR file: How to import a jar in Eclipse
3. After adding the .JAR its pretty simple, you just need to connect to the DB using your credentials, here is an example on how to do it:
Connection connection = null;
try {
Class.forName("oracle.jdbc.driver.OracleDriver");
connection = DriverManager.getConnection(
"jdbc:oracle:thin:#localhost:1521:xe", "system", "password"); } catch (Exception ex) {
ex.printStackTrace();
}
for more deatiled information on how to connect, you can check the oracle.jdbc
Class OracleDriver Documentation
4. After the connection has been made its pretty simple, Heres a short code example to get the result you want, you need to modify it with your connection details and as you see fit because im making a couple of assumptions, this code is just an example:
Main.Java
public class Main {
public static void main(String[] argv) {
List<AddressID> addresses;
SQLConnect conex= new SQLConnect();
String caseid="the id you want";
addresses=conex.getAddressIdPostCodeList(caseid);
}
AddressID.java
public class AddressID {
private String addressId;
private String postcode;
}
SQLConnect.Java
public class SQLConnect {
public static Connection connection;
public SQLConnect (){
createConnection();
}
public void CreateConnection(){
try {
Class.forName("oracle.jdbc.driver.OracleDriver");
connection = DriverManager.getConnection(
"jdbc:oracle:thin:#localhost:1521:xe", "system", "password")
} catch (Exception ex) {
ex.printStackTrace();
}
}
public void closeConnection(){
if(connection!=null){
try {
connection.close();
} catch (SQLException ex) {
ex.printStackTrace();
}
}
public ResultSet ExecuteQuery(String queryTXT) throws SQLException{
Statement query = connection.createStatement();
ResultSet table=query.executeQuery(queryTXT);
return table;
}
public List<AddressID> getAddressIdPostCodeList(String caseid) throws SQLException{
List<AddressID> addresses = new ArrayList <> ();
ResultSet table=ExecuteQuery("SELECT address_id,postcode FROM table1 WHERE custom_field_1 ='"+caseid+"';");
while (table.next()) {
AddressID aux;
aux.addressId=table.getString(1);
aux.postcode=table.getString(2);
addresses.add(aux);
}
return addresses;
}
}
I did forgot to wtite my result query which is actually binding data fetched
from select query to the AddressId class..
#Results(id = "result",
value = {
#Result(property = "addressId", column = "address_id"),
#Result(property = "postcode", column = "postcode")
}
)

Failed to create a table on in-memory sqlite database without any error message

I am trying to create a table on in-memory sqlite database. I have a method called createTable to create a table which initializes a connection to the database with getConnection method.
createTable:
private void createTable() {
try {
final Connection connection = getConnection();
final Statement statement = connection.createStatement();
statement.execute(
"CREATE TABLE videos (id INTEGER PRIMARY KEY, title VARCHAR(255), description TEXT, path TEXT, category VARCHAR(255), published INTEGER DEFAULT 0, created TIMESTAMP DEFAULT CURRENT_TIMESTAMP);");
statement.close();
connection.close();
} catch (final SQLException e) {
LOGGER.warn("Failed to create table.", e);
}
}
getConnection
private static Connection getConnection() {
try {
Class.forName("org.sqlite.JDBC");
return DriverManager.getConnection("jdbc:sqlite:");
} catch (final Exception e) {
LOGGER.warn("Failed to get connection.", e);
throw new RuntimeException(e);
}
}
To test the database I wrote a simple insert method:
public void insert() {
try {
final Connection connection = getConnection();
final Statement statement = connection.createStatement();
statement.executeUpdate(
"INSERT INTO videos (title, path, category) VALUES ('test title', 'test path', 'test category');");
statement.close();
connection.close();
} catch (final SQLException e) {
LOGGER.warn("Failed to insert.", e);
}
}
When I do
this.createTable();
this.insert();
I got the following error:
2017-09-17 22:38:02 WARN Database:128 - Failed to insert.
org.sqlite.SQLiteException: [SQLITE_ERROR] SQL error or missing database (no such table: videos)
at org.sqlite.core.DB.newSQLException(DB.java:909)
at org.sqlite.core.DB.newSQLException(DB.java:921)
at org.sqlite.core.DB.throwex(DB.java:886)
at org.sqlite.core.NativeDB._exec_utf8(Native Method)
at org.sqlite.core.NativeDB._exec(NativeDB.java:87)
at org.sqlite.jdbc3.JDBC3Statement.executeUpdate(JDBC3Statement.java:116)...
For connections I am using org.xerial:sqlite-jdbc's v3.20.0.
Any ideas why table is not getting created? I am not seeing any exception or anything. SQLFiddle for the example above seems to be fine.
If I read the documentation correctly, the database only exists within its database connection by default:
Instead, a new database is created purely in memory. The database ceases to exist as soon as the database connection is closed. Every :memory: database is distinct from every other. So, opening two database connections each with the filename ":memory:" will create two independent in-memory databases.
The easiest fix would be to not open a second connection, but keep using the connection that you used to create the table for the other database queries as well.
If you want to open multiple connections, you can set the ?cache=shared parameter on the database url.

Using database mysql in Java

I have started trying out some stuff so that I can use mysql database together with Java. First of all I have some questions about it.
I have used mysql a lot with PHP development but never with Java. Can I use the MySQL that MAMP brings or do I have to install it stand alone or something?
and second.. I have created this code with the help of a tutorial but the only output I get is
com.mysql.jdbc.Driver
The code that I have used for this you can find below:
package Databases;
import java.sql.*;
public class MysqlConnect{
/* These variable values are used to setup
the Connection object */
static final String URL = "jdbc:mysql://localhost:3306/test";
static final String USER = "root";
static final String PASSWORD = "root";
static final String DRIVER = "com.mysql.jdbc.Driver";
public Connection getConnection() throws SQLException {
Connection con = null;
try {
Class.forName(DRIVER);
con = DriverManager.getConnection(URL, USER, PASSWORD);
}
catch(ClassNotFoundException e) {
System.out.println(e.getMessage());
System.exit(-1);
}
return con;
}
public void getEmployees() {
ResultSet rs = null;
try {
Statement s = getConnection().createStatement();
rs = s.executeQuery("SELECT id, name, job_id, location FROM person");
System.out.format("%3s %-15s %-7s %-7s%n",
"ID", "NAME", "JOB ID",
"LOCATION");
System.out.format("%3s %15s %7s %7s%n",
"---", "---------------",
"-------", "--------");
while(rs.next()) {
long id = rs.getLong("id");
String name = rs.getString("name");
long job = rs.getLong("job_id");
String location = rs.getString("location");
System.out.format("%-3d %-15s %7d %5s%n",
id, name, job, location);
}
}
catch(SQLException e) {
System.out.println(e.getMessage());
System.exit(-1);
}
}
}
It's coming from the following block:
catch(ClassNotFoundException e) {
System.out.println(e.getMessage());
System.exit(-1);
}
That's a pretty poor way of handling exceptions. You're just printing the exception message. You have no clue what's going on. Rather just throw it (which will end up with a nice stacktrace), or print a more descriptive message along alone the exception message, e.g.
catch(ClassNotFoundException e) {
System.out.println("JDBC driver class not found in runtime classpath: " + e.getMessage());
System.exit(-1);
}
How to fix the particular exception is in turn actually a second question (with a pretty obvious answer: just put JAR file containing JDBC driver class in runtime classpath), but ala, you may find this mini-tutorial helpful then: Connect Java to a MySQL database.
Unrelated to the concrete problem, I'm not sure which tutorial you're reading there, but I'd take it with a grain of salt. Apart from poor exception handling, it's also leaking DB resources in getEmployees() method by never closing the result set, statement and connection. This is absolutely not a good practice either. How to do it is also already covered in the aforelinked mini-tutorial. See further also: How often should Connection, Statement and ResultSet be closed in JDBC?
Yes, you need to install MySQL server locally or remotely.
The code will be usable if you also downloaded jdbc Driver jar from MySQL download pages. and you configured your MySQL instance with the proper username and password.

Store database connection as separate Class - Java

Is it possible to store a database connection as a separate class, then call the database objects from a main code? ie;
public class main{
public static void main{
try{
Class.forName("com.jdbc.driver");
Database to = new Database(1,"SERVER1","DATABASE");
Database from = new Database(2,"SERVER2","DATABASE");
String QueryStr = String.format("SELECT * FROM TABLE WHERE Id = %i", to.id)
to.results = sql.executeQuery(QueryStr);
while (to.results.next()) {
String QueryStr = String.format("INSERT INTO Table (A,B) VALUES (%s,%s)",to.results.getString(1),to.results.getString(2));
from.sql.executeQuery("QueryStr");
}
to.connection.close()
from.connection.close()
} catch (Exception ex) {
ex.printStackTrace();
{ finally {
if (to.connection != null)
try {
to.connection.close();
} catch (SQLException x) {
}
if (from.connection != null)
try {
from.connection.close();
} catch (SQLException x) {
}
}
}
public static class Database {
public int id;
public String server;
public String database;
public Connection connection;
public ResultSet results;
public Statement sql;
public Database(int _id, String _server, String _database) {
id = _id;
server = _server;
database = _database;
String connectStr = String.format("jdbc:driver://SERVER=%s;port=6322;DATABASE=%s",server,database);
connection = DriverManager.getConnection(connectStr);
sql = connection.createStatement;
}
}
}
I keep getting a "Connection object is closed" error when I call to.results = sql.executeQuery("SELECT * FROM TABLE"); like the connection closes as soon as the Database is done initializing.
The reason I ask is I have multiple databases that are all about the same that I am dumping into a master database. I thought it would be nice to setup a loop to go through each from database and insert into each to database using the same class. Is this not possible? Database will also contain more methods than shown as well. I am pretty new to java, so hopefully this makes sense...
Also, my code is probably riddled with syntax errors as is, so try not to focus on that.
Connection object is closed doesn't mean that the connection is closed, but that the object relative to the connection is closed (it could be a Statement or a ResultSet).
It's difficult to see from your example, since it has been trimmed/re-arranged, but it looks like you may be trying to use a ResultSet after having re-used its corresponding Statement. See the documentation:
By default, only one ResultSet object per Statement object can be open
at the same time. Therefore, if the reading of one ResultSet object is
interleaved with the reading of another, each must have been generated
by different Statement objects. All execution methods in the Statement
interface implicitly close a statment's current ResultSet object if an
open one exists.
In your example, it may be because autoCommit is set to true by default. You can override this on the java.sql.Connection class. Better yet is to use a transaction framework if you're updating multiple tables.

Copy image to BLOB from client pc aka Java function in Oracle

I've been stuck with this for past two days. I've go java function stored in Oracle system which is supposed to copy image from local drive do remote database and store it in BLOB - it's called CopyBLOB and looks like this:
import java.sql.*;
import oracle.sql.*;
import java.io.*;
public class CopyBLOB
{
static int id;
static String fileName = null;
static Connection conn = null;
public CopyBLOB(int idz, String f)
{
id = idz;
fileName = f;
}
public static void copy(int ident, String path) throws SQLException, FileNotFoundException
{
CopyBLOB cpB = new CopyBLOB(ident, path);
cpB.getConnection();
cpB.callUpdate(id, fileName);
}
public void getConnection() throws SQLException
{
DriverManager.registerDriver (new oracle.jdbc.OracleDriver());
try
{
conn = DriverManager.getConnection("jdbc:oracle:thin:#oraserv.ms.mff.cuni.cz:1521:db", "xxx", "xxx");
}
catch (SQLException sqlex)
{
System.out.println("SQLException while getting db connection: "+sqlex);
if (conn != null) conn.close();
}
catch (Exception ex)
{
System.out.println("Exception while getting db connection: "+ex);
if (conn != null) conn.close();
}
}
public void callUpdate(int id, String file ) throws SQLException, FileNotFoundException
{
CallableStatement cs = null;
try
{
conn.setAutoCommit(false);
File f = new File(file);
FileInputStream fin = new FileInputStream(f);
cs = (CallableStatement) conn.prepareCall( "begin add_image(?,?); end;" );
cs.setInt(1, id );
cs.setBinaryStream(2, fin, (int) f.length());
cs.execute();
conn.setAutoCommit(true);
}
catch ( SQLException sqlex )
{
System.out.println("SQLException in callUpdateUsingStream method of given status : " + sqlex.getMessage() );
}
catch ( FileNotFoundException fnex )
{
System.out.println("FileNotFoundException in callUpdateUsingStream method of given status : " + fnex.getMessage() );
}
finally
{
try
{
if (cs != null) cs.close();
if (conn != null) conn.close();
}
catch ( Exception ex )
{
System.out.println("Some exception in callUpdateUsingStream method of given status : " + ex.getMessage( ) );
}
}
}
}
The wrapper function is defined in package "MyPackage" as folows:
procedure image_adder( id varchar2, path varchar2 )
AS
language java name 'CopyBLOB.copy(java.lang.String, java.lang.String)';
And the inserting function called image_add is as simple as this:
procedure add_image( id numeric(10), pic blob)
AS
BEGIN
insert into pictures values (seq_pic.nextval, id, pic);
END add_image;
Now the problem: When I type
call MyPackage.image_adder(1, 'd:\samples\img.jpg');
I get the ORA-29531 Error: No method copy in class CopyBLOB.
Can you help me, please?
The method in your class has this signature:
public static void copy(int ident, String path)
But in your Java Stored Procedure you have specified this signature:
'CopyBLOB.copy(java.lang.String, java.lang.String)'
I think if you change the first argument to java,lang.Integer your problem should resolve itself. You should probably change the datatype of the ID parameter in the IMAGE_ADDER() procedure as well.
edit
"Any ideas how to upload local files?"
Not unreasonably, the database can only interact with files which are visible to its server. Generally that limits matters to files and directories which are physically on the same box, unless the network admin has mapped some remote drives.
Transferring files from a local PC drive to a server is really a client i.e. application issue, it isn't the sort of thing the database should really get involved with.
I know that isn't what you were hoping to hear. If you really want to drive the file uploading from teh database, then the mechanism remains the same whenever we want to transfer files across a network: FTP. Tim Hall has published a PL/SQL implementation for FTP on his Oracle-Base site. Find out more.
"long as the file is smaler than 2000B
(WTF?)"
That is suspiciously close to the BINARY CHAR limit (2000). In older versions of Oracle we had to use a two-step process: insert a placeholder and then issue an update. Something like this:
procedure add_image( id numeric(10), pic blob)
AS
BEGIN
insert into pictures
values (seq_pic.nextval, id, empty_blob());
update pictures
set col_pic = pic
where id = seq_pic.currval;
END add_image;

Categories

Resources