I need a java application that can export some data from a Oracle database and write it to a Excel file everyday. I am really new to JAVA so I am making this app step by step.
First to all I'm going to show the database schema (simplified version):
GLOBAL (allocated in bar.domain.es)
-DATABASE1:
TABLE A
TABLE B
TABLE C
-DATABASE2:
TABLE 1
TABLE 2
One part of my code is:
//Loading the driver
Class.forName("oracle.jdbc.OracleDriver");
System.out.println("Driver Loaded");
//Connecting to Oracle Database
java.sql.Connection con = DriverManager.getConnection(DBURL, DBUSER, DBPASS);
System.out.println("Connection Success");
//Creating statement
Statement stat = con.createStatement();
//Creating the query string
String query ="SELECT count(*) FROM TABLE2 WHERE DATE=150603 AND ID=238";
// Creating the statement to execute the Query
ResultSet rs = stat.executeQuery(query);
where DBURL is: "jdbc:oracle:thin:#bar.domain.es:1521:XE"
With this code I get the message Connection Success so my app is connected to the database schema. However, in this schema there are several databases with several tables on each so my problem comes when I try to launch the query. The program doesn't find TABLE2 which is a table of the DATABASE2. I think that I should specify in someway that I want to search this TABLE2 in DATABASE2 but I don't know how.
You can specify in the query what database the table is in
String query ="SELECT count(*) FROM DATABASE2.TABLE2 WHERE DATE=150603 AND ID=238";
Related
I want to export huge data from oracle to csv file. so i used simple JDBC select statement to get data in memory but and then write it to file, But data is very large of i am getting Out of memory exception. So i thought of using CallableStatement to call Stored Procedure which will return CURSOR with ResultSet as below :-
String getDBTableCursorSql = "{call getDBTableCursor(?,?)}";
callableStatement = dbConnection.prepareCall(getDBTableCursorSql);
callableStatement.setString(1, "test");
callableStatement.registerOutParameter(2, OracleTypes.CURSOR);
// execute getDBTableCursorSqlstore procedure
callableStatement.executeUpdate();
// get cursor and cast it to ResultSet
rs = (ResultSet) callableStatement.getObject(2);
// loop it like normal
while (rs.next()) {
String userid = rs.getString("ID");
String userName = rs.getString("NAME");
..
..
}
Oracle Proc :-
CREATE OR REPLACE PROCEDURE getDBTableCursor(
p_username IN DBUSER.USERNAME%TYPE,
c_dbuser OUT SYS_REFCURSOR)
IS
BEGIN
OPEN c_dbuser FOR
SELECT * FROM CUSTOMER WHERE USERNAME LIKE p_username || '%';
END;
Question 1 :-
does above ResultSet will fetch all the data in single shot ? or it will go to database for each rs.next(),
Question 2:-
is there any other approach which can deal with large data export to file in java using chunks so it wont get Out of memory issue?
I can't use pagination in this condition because of requirement.
Regarding your first question: the Oracle jdbc driver by default fetches 10 rows at a time. This can be verified or set to other value via standard jdbc:
https://docs.oracle.com/cd/E11882_01/java.112/e16548/resltset.htm#JJDBC28621
my first time in eclipse and im trying to get some test data from my sql server,now the problem is i have setup a sql connection using ms jdbc drivers and it seems like it works but when i run my query from eclipse,i get
com.microsoft.sqlserver.jdbc.SQLServerException: Invalid column name
'KategoriName'.
error.My query works fine in sql manager.What could be the problem?I'm adding the code below as well:
String connectionString = "jdbc:sqlserver://192.168.0.155;user=user;password=password";
Connection conn= DriverManager.getConnection(connectionString);
Statement stmt = conn.createStatement();
ResultSet rs;
String sqlconn="select [KategoriName] from [FINSAT6G9].[TBL_Test] whereID=493";
rs = stmt.executeQuery(sqlconn);
String aa = rs.getString("KategoriName");
System.out.println(aa);
Cheers.
Try including the database name in the connection string as so:
String connectionString = "jdbc:sqlserver://192.168.0.155;user=user;password=password;databaseName=FINSAT6G9";
Because, obviously, the error is telling you that the KategoriName column doesn't exist which can only mean 2 things: You have a typo or you are attempting to get the data from the wrong place, either the wrong database or the wrong table.
I am trying to retrieve data from MySQL database using eclipse.I am using the same code of JDBC as for a java application...but it does work.
You can do as follows:
Class.forName("com.mysql.jdbc.Driver"); // Setup the connection with the DB
connect = DriverManager.getConnection("jdbc:mysql://localhost/database_name", username,password);
// Statements allow to issue SQL queries to the database ; that's why we need to create one.
statement = connect.createStatement();
// Result set get the result of the SQL query
resultSet = statement.executeQuery("select * from TableName;");
while (resultSet.next()) { //retrieve data
String data = resultSet.getString("column_name");
...
}
Please make sure that your database connection is created successfully then apply queries to retrieve data from database. If your connection is not created then you need to check you driver setting or mysql password setting.
Class.forName("com.mysql.jdbc.Driver");
Connection con=DriverManager.getConnection("jdbc:mysql://localhost/database_name",username,password");
Connection class having method like 'createstatement()', with help of this we can query from database.
Statement statement = con.createstatement();
resultSet=statement.executeQuery("select * from tablename");
And finally retrive the data from the 'resultSet' object.
I'm creating an applicaation on Netbeans 7! I'd like my application to have a little code in main so that it can create a Java DB connection checking to see if the database and the associate tables exist, if not create the database and the tables in it. If you could provide a sample code, it'd be just as great! I have already looked at http://java.sun.com/developer/technicalArticles/J2SE/Desktop/javadb/ but I'm still not sure how to check for an existing database before creating it!
I'd like my application to have a little code in main so that it can create a Java DB connection checking to see if the database and the associate tables exist, if not create the database and the tables in it.
You can add the create=true property, in the JDBC URL. This creates a Derby database instance if the database specified by the databaseName does not exist at the time of connection. A warning is issued if the database already exists, but as far as I know, no SQLException will be thrown.
As far as creation of the tables is concerned, this is best done on application startup before you access the database for typical transactional activity. You will need to query the SYSTABLES system table in Derby/JavaDB to ascertain whether your tables exist.
Connection conn;
try
{
String[] tableNames = {"tableA", "tableB"};
String[] createTableStmts = ... // read the CREATE TABLE SQL statements from a file into this String array. First statement is for the tableA, and so on.
conn = DriverManager.getConnection("jdbc:derby:sampleDB;create=true");
for(int ctr =0 ; ctr < tableNames.length; ctr++)
{
PreparedStatement pStmt = conn.prepareStatement("SELECT t.tablename FROM sys.systables t WHERE t.tablename = ?");
pStmt.setString(1, tableNames[ctr]);
ResultSet rs = pStmt.executeQuery();
if(!rs.next())
{
// Create the table
Statement stmt = conn.createStatement();
stmt.executeUpdate(createTableStmts[ctr]);
stmt.close();
}
rs.close();
pStmt.close();
}
}
catch (SQLException e)
{
throw new RuntimeException("Problem starting the app...", e);
}
Any non-existent tables may then be created. This is of course, not a good practice, if your application has multiple versions, and the schema varies from one version of the application to another. If you must handle such a scenario, you should store the version of the application in a distinct table (that will usually not change across versions), and then apply database delta scripts specific to the newer version, to migrate your database from the older version. Using database change management tools like DbDeploy or LiquiBase is recommended. Under the hood, the tools perform the same operation by storing the version number of the application in a table, and execute delta scripts having versions greater than the one in the database.
On a final note, there is no significant difference between JavaDB and Apache Derby.
I don't know how much Oracle changed Derby before rebranding it, but if they didn't change too much then you might be helped by Delete all tables in Derby DB. The answers to that question list several ways to check what tables exist within a database.
You will specify the database when you create your DB connection; otherwise the connection will not be created successfully. (The exact syntax of this is up to how you are connecting to your db, but the logic of it is the same as in shree's answer.)
The create=true property will create a new database if it is not exists. You may use DatabaseMetadata.getTables() method to check the existence of Tables.
Connection cn=DriverManager.getConnection("jdbc:derby://localhost:1527/testdb3;create=true", "testdb3", "testdb3");
ResultSet mrs=cn.getMetaData().getTables(null, null, null, new String[]{"TABLE"});
while(mrs.next())
{
if(!"EMP".equals(mrs.getString("TABLE_NAME")))
{
Statement st=cn.createStatement();
st.executeUpdate("create table emp (eno int primary key, ename varchar(30))");
st.close();;
}
}
mrs.close();
cn.close();
Connection conn = getMySqlConnection();
System.out.println("Got Connection.");
Statement st = conn.createStatement();
String tableName = ur table name ;
String query = ur query;
Statement stmt = null;
ResultSet rs = null;
try {
stmt = conn.createStatement();
rs = stmt.executeQuery(query);
System.out.println("Exist");;
}
catch (Exception e ) {
// table does not exist or some other problem
//e.printStackTrace();
System.out.println("Not Exist");
}
st.close();
conn.close();
I'm trying to connect netbeans to my postgresql database. The connection seems to have worked as I don't get any errors or exceptions when just connecting, methods such as getCatalog() also return the correct answers.
But when I try to run a simple SQL statement I get the error "ERROR: relation "TABLE_NAME" does not exist", where TABLE_NAME is any one of my tables which DO exist in the database. Here's my code:
Statement stmt = con.createStatement();
ResultSet rs;
String query = "SELECT * FROM clients";
rs = stmt.executeQuery(query);
I was thinking that netbeans might not be finding the tables because it's not looking in the default schema (public), is there a way of setting the schema in java?
EDIT: My connection code. The database name is Cinemax, when I leave out the statement code, I get no errors.
String url = "jdbc:postgresql://localhost:5432/Cinemax";
try{
try {
Class.forName("org.postgresql.Driver");
} catch (ClassNotFoundException cnfe) {
System.err.println("Couldn't find driver class:");
cnfe.printStackTrace();
}
Connection con = DriverManager.getConnection( url,"postgres","desertrose147");
I suspect you created the table using double quotes using e.g. "Clients" or some other combination of upper/lowercase characters and therefor the table name is case sensitive now.
What does the statement
SELECT table_schema, table_name
FROM information_schema.tables
WHERE lower(table_name) = 'clients'
return?
If the table name that is returned is not lowercase you have to use double quotes when referring to it, something like this:
String query = "SELECT * FROM \"Clients\"";
You could check these possibilities:
String query = "SELECT * FROM clients";
String query = "SELECT * FROM CLIENTS";
String query = "SELECT * FROM \"clients\"";
String query = "SELECT * FROM \"CLIENTS\"";
String query = "SELECT * FROM Clients";
Maybe one of those would work.
Besides CoolBeans' suggestion, you may also be connecting to the db as a different user who does not have permission on the relevant db or schema. Can you show the connection string?
Funny thing is i was experiencing the same thing as i had just started on netbeans and postgressql db, and the error was fixed after noting that the issue was that my tables in postgressql had capital letters in my naming convention which me and my jdbc query statement for INSERT was failing to find the table. But after renaming my tables in the db and fixing the column names as well am good to go. Hope it helps.