Creating Table in AWS Redshift with AWS Java SDK - java

I am trying to create a temporary table inside of AWS redshift using the java SDK.
// Redshift JDBC 4.1 driver: com.amazon.redshift.jdbc41.Driver
String command = "CREATE TABLE test (FirstName varchar(255));"
Class.forName("com.amazon.redshift.jdbc41.Driver");
conn = DriverManager.getConnection(dbURL, props);
stmt = conn.createStatement();
ResultSet rs = stmt.executeQuery(command);
When running this code the table is created successfully but on the return, an error is thrown
com.amazon.dsi.dataengine.impl.DSISimpleRowCountResult cannot be cast to com.amazon.dsi.dataengine.interfaces.IResultSet
Everything will work if I use a command that returns something, such as
"Select * FROM test;"
"SELECT * FROM test LIMIT 0;"
I didn't see any documentation for this problem in the AWS READMEs or other stack overflow questions for this problem. It seems to me that there is a special class in the driver for when nothing is returned from the statement that is not able to be cast to the ResultSet class.
The redshift driver version is 1.1.13.1013 .
Any help would be greatly appreciated!

I had the same problem. You can solve this easily. Instead of
stmt.executeQuery(command);
please try
stmt.execute(command);

Related

Can't connect my SQL Server base to Java app on NetBeans

I have installed NetBeans IDE 12.3 on Lubuntu and SQL Server thanks to the microsoft documentation.
I have created my database using the command line tool sqlcmd (I don't have a GUI for sql server like you have on windows).
In the netbeans interface, I connected my sql server base to my java project, I see the base, I see the tables. Using the SQL tool of netbeans I can request them, add/delete entries etc. no problem.
But I would like to make request in the java code itself, not in the sql tool of netbeans. The documentation says I should create a Connection object, but can't I just use the connection I created through the graphical netbeans interface ?
I get this error: java.sql.SQLException: No suitable driver found for jdbc:sqlserver://localhost\db:1433;databaseName=ConservatoireDB
Here is the code in main:
ResultSet resultSet = null;
try (Connection connection = DriverManager.getConnection("jdbc:sqlserver://localhost\\db:1433;databaseName=ConservatoireDB");
Statement statement = connection.createStatement();) {
// Create and execute a SELECT SQL statement.
String selectSql = "select * from salle";
resultSet = statement.executeQuery(selectSql);
// Print results from select statement
while (resultSet.next()) {
System.out.println(resultSet.getString(2) + " " + resultSet.getString(3));
}
}
catch (SQLException e) {
e.printStackTrace();
}
By the way, my project is a graphical app using Swing library.

Error or wrong type of code while creating connection & execute SQL query statements using mysql in java

I'm using MySQL 5.7 with Java in Eclipse, and the connection statement below code below is causing an error when I try to connect:
try
{
//1. Get a connection to database
jdbc:mysql://localhost:3306/databaseName?autoReconnect=true;useSSL=false;
// 2. Create a statement
Statement myStmt=myConn.createStatement();
// 3. Execute SQL query
ResultSet myRs=myStmt.executeQuery("select * from employee");
//4. Process the result set
while(myRs.next())
{
System.out.println(myRs.getString("last_name")+","+myRs.getString("first_name"));
}
}
catch(Exception exc){
exc.printStackTrace();
}
First things first.
Code will only be used to validate the error. So you must paste the error fired by your program.
Since we don't have enough information to the problem, I will just cover basic troubleshooting.
Basic trouble shooting:
Do you have the driver? if not, you can download it here.
Next, Do you have the driver on your project class path? If not yet, you must add it. see how here
Did you load the driver to the program? if not, Class.forName("com.mysql.jdbc.Driver"); // Load Driver like that before doing anything.
Did you establish the connection? if not, Connection con = DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/DATABASE","USERNAME","PASSWORD");//3306 or port number depends on you config, same with DATABASE, USERNAME, PASSWORD
After the connection were established, so you should create a statement object like Statement s = con.createStatement(); // Create Statement. This will be used to execute sql commands.
finally, you can execute the commands like s.execute("select * from employee"); // Execute Query NOTE that s here is the variable created on number 5.
If all of the above were properly done but still gets an error, check if your have the database server running. In you case, mysql. Make sure there were not other installation of mysql prior to your current mysql. Sometimes, it will mess up your database. Troubleshooting your mysql, see mysql official doc here
While possible error is the datatype of mysql to your java code or getting a column that does not exist on your query or worse the column does not exist on your table.
Hope that help you and other who needs it.

SQLite error or missing database (no such table) in Netbeans connection to SQLite

I am getting the above error when trying to do an insert(or select) to a SQLite file from Java in Netbeans. I have created the db file manually from SQLite Database Browser and put it in the source package. Below is the code and logs:
public void DBInsertServerConfig(ServerConfig serverconfig) throws SQLException {
Connection conn = DBConnect();
Statement statement = null;
conn.setAutoCommit(false);
statement = conn.createStatement();
String sql = "INSERT INTO serverconfig(ip,port,db_name,db_user,password,fcm_server_key) " +
"VALUES (?,?,?,?,?,?)"; //
try{
PreparedStatement pstm = conn.prepareStatement(sql);
pstm.setString(1,serverconfig.getIp());
pstm.setString(2,serverconfig.getPort());
pstm.setString(3,serverconfig.getDb_name());
pstm.setString(4,serverconfig.getDb_user());
pstm.setString(5,serverconfig.getPassword());
pstm.setString(6,serverconfig.getFcm_server_key());
//statement.execute(sql);
pstm.executeUpdate();
statement.close();
conn.commit();
conn.close();
The database is opened correctly but it seems it doesn't find the table although it exist.
compile:run:
Opened database successfully
org.sqlite.SQLiteException: [SQLITE_ERROR] SQL error or missing database (no such table: serverconfig)
BUILD SUCCESSFUL (total time: 9 seconds)
Attached is a screenshot of the db file from SQLite Database Browser :
I have seen and tried other posts like in here but I didn't get a solution.
Can anyone help me figuring out this?
I found the answer and I am posting if anyone run into the same problem/confusion.
I had put my db file under the src package while the url path was pointing into the project root folder(outside src). An empty db file was created by netbeans, and of course it hadn't any table in it. That's what happen when you follow tutorials that haven't been tested by their own creators. :D
The example that I used was
Connection conn = DriverManager.getConnection("jdbc:sqlite:C:\\Users\\Tolga\\Documents\\NetBeansProjects\\dpmlzmtkb\\depom.sqlite");
Which is wrong.
I changed depom.SQLite to depom.db and it worked. So as I understand if the path is correct NetBeans creating another empty SQLite database to the given path.
Please use the absolute path,like(on my Ubuntu)
private static String url ="jdbc:sqlite:/home/yourname/study/eclipse/hk/ss.db";
At first i get same error like yours,
i can link sql in ordinary class,but in the servlet/jsp can't

How to get the full query that a PreparedStatement is about to execute in DB2?

I'm working on a dynamic web project and using the PreparedStatement to execute the SQL queries against the DB2 database.
String myQuery = "select id from user where name = ?";
PreparedStatement stmt = connection.prepareStatement(myQuery);
stmt.setString(1, test);
ResultSet resultSet = statement.executeQuery();
How can I receive the full SQL query that is about to be executed on the DB2 server in the console?
If you are familiar with Debugging options in Eclipse. You may try the following:
Set a Breakpoint at ResultSet resultSet = statement.executeQuery();
Right click your application, say Debug As select Java Application (or Whatever applicable in your case i.e. may be SpringBoot App etc.
Perform step that gets you to code mentioned in the Question.
If you check Variables tab in Debug Perspective of Eclipse, you will find variables like myQuery , stmt (according to your code)
Whatever you see as value of stmt would be the full SQL query you need.
Also, if you don't want to keep looking at this variable always you may try Java Logging and Print your Full SQL query in Logs.

Creating a Scroll_Insensitive ResultSet using the SAP Hana JDBC Driver

I'm trying to create a Scroll_Insensitive ResultSet using/in the SAP Hana JDBC Driver. When I run the below code:
Class.forName("com.sap.db.jdbc.Driver");
connection = DriverManager.getConnection("jdbc:sap://10.32.86.10:30115/autocommit=false",username,password);
java.sql.Statement stmt = connection.createStatement(java.sql.ResultSet.TYPE_SCROLL_INSENSITIVE, java.sql.ResultSet.CONCUR_READ_ONLY);
java.sql.ResultSet resultSet = stmt.executeQuery("SELECT * FROM SAMPLE");
resultSet.next();
String hello = resultSet.getString(1);
I get the following exception:
com.sap.db.jdbc.exceptions.jdbc40.SQLDataException: Invalid argument resultSetType, use TYPE_FORWARD_ONLY.
If I replace the third line with:
java.sql.Statement stmt = connection.createStatement();
It works without a hitch. I need the ResultSet to be Scroll_Insensitive to be able to use methods such as
rs.previous(), rs.last(), rs.getRow(), etc.
The same code works perfectly for MySQL, Microsoft SQL, TeraData & Oracle. What might the problem with SAP Hana? Is there a workaround?
The answer simply is what the error message implies: SAP HANA's JDBC driver (currently) only provides you with a FORWARD ONLY cursor type.
Question here would be what specific characteristic of the TYPE_SCROLL_INSENSITIVE cursor type do you actually need here?
I also faced the same error where I wanted to check if the processed row was the last row of the result set and tried rs.last() method on resultset object. Here is what I found on SAP SCN:
Due to performance improvement when using forward only cursor we
changed the default for the resultset type from TYPE_SCROLL_SENSITIVE
to TYPE_FORWARD_ONLY starting with JDBC driver version 7.6. So I guess
the exception comes from a statement where you didn't set the
result set type while creating it. Please check if all of the
statements that you want to be scrollable have set the correct
resultset type.
here is the link to the thread:
The operation is not allowed for result set type FORWARD_ONLY

Categories

Resources