NullPointerException from Connection.preparedStatement for SQLite JDBC - java

what I'm trying to do is get a ResultSet from a SQL statement from which then I can derive certain values. What I seem to be having troubles with is the preparing of the prepared statement which occurs at this line.
String sql = "SELECT " + dataState + " FROM " + table + whereState + ";";
java.sql.PreparedStatement prep = conn.prepareStatement(sql); // Error here
I've already checked that the variable conn is not null. All the values in the statement are not null and the stack trace shows this.
java.lang.NullPointerException
at org.sqlite.PrepStmt.(PrepStmt.java:37)
at org.sqlite.Conn.prepareStatement(Conn.java:231)
at org.sqlite.Conn.prepareStatement(Conn.java:224)
at org.sqlite.Conn.prepareStatement(Conn.java:213)
at org.utilities.storagemethods.SQLiteMethod.load(SQLiteMethod.java:223)
at org.utilities.DataManager.load(DataManager.java:97)
at org.utilities.Test.main(Test.java:21)
A print of the sql statement shows that it gives this.
SELECT testString, testBool, testObj FROM testTable WHERE testInt=?;
You can ascertain the values from the given string.
What I can't figure out is what I'm doing wrong to cause this, if you're curious about what sqlite system I'm using, you can find the home page here: http://www.zentus.com/sqlitejdbc/
Thanks in advance for any help on this. I'm completely stumped.
Note: In an attempt to save resources, I was caching my Connection's, and re-using them later. When I try re-getting the connection, it works perfectly. Is there a way I can cache them or do I need to load them every time I want to use it?

I was getting this error, and it was caused because I had closed the connection. The connection wasn't null, it was just closed. ARRRGGG! Like the OP hinted at, I had started with the sample code at Zentus (Xerial now, since Zentus seems to have shut down). The sample code has a finally block where he closes the connection. Once I took out the finally block, my error disappeared. Just remember to close the connection before you exit the program.
To see if you have the same problem, right before the line that gives the error, try adding
System.out.println("isClosed = " + conn.isClosed());

If not wrong, I think it is because of ';' inside your SQL statement. Please remove and try again ?
Regards,
Code Behind

Related

No database selected, despite code testing seeming to work

I'm at a complete loss, I don't understand what is wrong here. I'm writing a Java program to take a few databases and put them into a mySQL database. I've got the JConnector in my build path:
Build path screenshot
try {
String driver = "com.mysql.cj.jdbc.Driver";
String address = "jdbc:mysql://localhost:3306/?user=root/Exercise";
Class.forName(driver);
con = DriverManager.getConnection(address, username, password);
System.out.println("Connection Success");
Statement st = con.createStatement();
int c =st.executeUpdate("CREATE TABLE test (Name VARCHAR(30))");
System.out.println("Table have been created.");
System.out.println(c+" Row(s) have been affected");
return con;
} catch (Exception e) { System.out.println(e);}
When I run this code, the output is:
Connection Success
java.sql.SQLException: No database selected
When I delete the "?user=root" part of the address, it will instead give me:
java.sql.SQLNonTransientConnectionException: Cannot load connection class because of underlying exception: com.mysql.cj.exceptions.WrongArgumentException: Malformed database URL, failed to parse the main URL sections.
This implies that, if I can't connect to a database, that it'll throw the exception, so apparently it is connecting, but then it's saying no database is selected despite that I'm literally connecting to it just a few lines back. In fact, the Statement line to the return line are code I took from another question's solution about this specific issue to test it, and their code seemed to be almost exactly the same as my own. What in the world am I doing wrong here?
Edit: I just tried running it again without the "?user=root" because of g00se's answer, and I somehow got A DIFFERENT error message than the one I already posted that I was getting.
java.sql.SQLSyntaxErrorException: Unknown database 'exercise'
EDIT 2:
I just had a thought, Eclipse is on an external harddrive, and I have no idea where the SQL database is stored, but could that be the issue? They're on different drives?
For unknown reasons, the exercise database (as well as others I had) went missing or were deleted. Thank you to g00se for suggesting I look at the databases and see what was actually there, readding it fixed the problem.

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.

Oracle & JDBC: AbstractMethodError when working with Clobs [duplicate]

This question already has answers here:
Why do I get java.lang.AbstractMethodError: oracle.jdbc.driver.OracleConnection error?
(2 answers)
Closed 6 years ago.
So I'm having a big issue with CLOBs and Oracle. Up until now, the database my company has been using for this one client has just been passed simple Strings through a PreparedStatement. This has been fine, because those Strings have all been less than 4,000 characters. We just discovered that limit exists. Not 100% sure why, believe it's something related to how CLOBs behave. Anyway, I have been assigned to go into the code and fix this.
So, at the moment, the PreparedStatement has its parameters assigned through a very simple process:
((PreparedStatement) stmt).setObject(fieldIndex++, fieldInfo.value);
This has worked well enough for now, but obviously not so much going forward.
Anyway, so my first thought was to try and use some of the PreparedStatement methods related to CLOBs. fieldInfo.value is a declared type of Object, with its actual type set dynamically. Up until now, it's been kept as a String, as I said, so I decided I would just change its type and then use one of the PreparedStatement methods to assign it.
I've tried the following value types & PreparedStatement methods:
if(fieldInfo.value instanceof InputStream){
((PreparedStatement) stmt).setBinaryStream(fieldIndex++, (InputStream) fieldInfo.value);
}
if(fieldInfo.value instanceof Reader){
((PreparedStatement) stmt).setCharacterStream(fieldIndex++, (Reader) fieldInfo.value);
}
if(fieldInfo.value instanceof Clob){
((PreparedStatement) stmt).setClob(fieldIndex++, (Clob) fieldInfo.value);
}
I'm obviously not calling all of those in a row, those are just examples of what I've tried separately.
Every one of those has the same result: AbstractMethodError. Upon researching that exception, I saw that the issue is most likely my JDBC driver. Except... as far as I can tell, I'm on the right one. I've tried this with ojdbc6 & ojdbc7, same error. I've scoured my classpath, and every directory in the project, for any indication that an older jar is hiding there, and I can't seem to find one.
Does anyone have any idea what could be happening?
Wow, what a weird issue. There was an old oracle driver buried inside another jar that had been sitting around for years. Ah, the wonders of legacy applications that haven't had a good code rebasing ever.
Anyway, any mods that see this can close this question.
I don't know what a command do you want to execute in your code, is it INSERT, UPDATE or some other command ?
However I simply followed the manual "JDBC Developer's Guide", a topic "Working with LOBs and BFILEs" ==> http://docs.oracle.com/cd/E11882_01/java.112/e16548/oralob.htm#JJDBC28535
and everything worked fine.
They wrote in the documentation that:
In Oracle Database 11g release 2 (11.2), the setBytes,
setBinaryStream, setString, setCharacterStream, and setAsciiStream
methods of PreparedStatement are extended to enhance the ability to
work with BLOB, CLOB, and NCLOB target columns.
They say that setString must work with strings > 4000 char, then I've created this simple test case, I used ojdbc6 driver (for Oracle 11.2), see below:
CREATE TABLE myclob(
id int,
myclob clob
);
================================
public static void main(String ...x) throws SQLException{
OracleDataSource ods = new OracleDataSource();
ods.setURL("jdbc:oracle:thin:#//localhost:1521/orcl");
ods.setUser("test");
ods.setPassword("test");
Connection con = ods.getConnection();
PreparedStatement stmt = con.prepareStatement("INSERT INTO myclob( id, myclob) VALUES (?, ?)");
StringBuffer str = new StringBuffer();
for(int i = 0; i<1000; i++)
str.append("Very long line number = " + i);
System.out.println( "The length is: " + str.length());
stmt.setInt(1, 2);
stmt.setString(2, str.toString());
stmt.executeUpdate();
con.commit();
stmt.close();
con.close();
}
================================
select length( m.myclob), m.*
from myclob m;
LENGTH(M.MYCLOB) ID MYCLOB
---------------- ---------- --------------------------------------------------------------------------------
26890 2 Very long line number = 0Very long line number = 1Very long line number = 2Very

SQLite Exception: Insert statement does not return a Statement

I'm using SQLiteJDBC as the wrapper for an embedded database for my small Java app. Everytime I execute an INSERT statement, I get the following exception:
query does not return ResultSet
I am wondering if the JDBC Statement.executeStatement(String) method is looking for me to return a ResultSet, but that the SQLite driver knows that INSERT statements don't return anything; maybe SQLiteJDBC is throwing an error because it shouldn't be returning the ResultSet that Statement is asking for?!?
Here is the code that is producing the exception - I have made sure to setup and close all resources properly:
statement = con.createStatement();
String sql = "INSERT INTO widgets (widget_age) VALUES (27)";
statement.executeStatement(sql);
Any ideas?!?
When you are making a change and not asking for a result back, you need to call executeUpdate() instead of executeStatement().
EDIT
I can't even find a reference to executeStatement() anywhere. Were you using executeQuery()?
Sqlite always returns a message quen you execute a Query.
It's normal to have that warning/error, it's simply that no rows where returned so you can't use them in the callback if you defined one.
PD: I also think you meant executeQuery

How to add records to databse via sql in Java

I am working a Airsoft application.
I'm trying to add records to a MS Access Database via SQL in Java. I have established a link to the database, with the following:
try
{
//String Driver = "sun.java.odbc.JdbcOdbcDriver";
Class.forName("net.ucanaccess.jdbc.UcanaccessDriver");
Connection conn = DriverManager.getConnection("jdbc:ucanaccess://" + URL,"","");
Statement stmt = conn.createStatement();
System.out.println("Connection Established!");
ResultSet rs = stmt.executeQuery("SELECT * FROM AirsoftGunRentals");
tblRent.setModel(DbUtils.resultSetToTableModel(rs));
}
catch(Exception ex)
{
JOptionPane.showMessageDialog(null, "Error");
}
I am using Ucanaccess to access my MS database. It is reading the database and is displaying to a JTable. However, I need to create three JButtons to add, delete and update the table. I have tried to code the add button, and I have tried to add a record, but it crashes and gives me errors.
try
{
//String Driver = "sun.java.odbc.JdbcOdbcDriver";
Class.forName("net.ucanaccess.jdbc.UcanaccessDriver");
Connection conn = DriverManager.getConnection("jdbc:ucanaccess://" + URL,"","");
Statement stmt = conn.createStatement();
System.out.println("Connection Established!");
String Query= "INSERT INTO AirsoftGunRentals(NameOfGun, Brand, TypeOfGuns, NumberOfMagazines,Extras,NumberAvailable,UnitRent)"+
"VALUES('"+pName+"','"+pBrand+"','"+pTypeOfGun+"','"+pNumMags+"','"+pExtras+"','"+pNumberAvail+"','"+pRent+"');";
ResultSet rs = stmt.executeQuery(Query);
JOptionPane.showMessageDialog(null, "Success!");
}
catch(Exception ex)
{
JOptionPane.showMessageDialog(null, "Error");
}
I have attempted all three, hoping for a result. But am still getting big errors. The only difference between the buttons is that one adds, one deletes and one updates the table. Other then that, the code is the same, minus variables.
As Brahim mentionned it, you should use stmt.executeUpdate(Query) whenever you update / insert or delete data. Also with this particular query, given your String concatenation (see end of line), there is no space between the ")" and the "VALUES" which probably causes a malformed query.
However, I can see from your code that you are not very experienced with such use-cases, and I'd like to add some pointers before all hell breaks loose in your project :
Use PreparedStatement instead of Statement and replace variables by placeholders to prevent SQL Injection.
The code that you are using here is extremely prone to SQL injection - if any user has any control over any of the variables, this could lead to a full database dump (theft), destruction of data (vandalism), or even in machine takeover if other conditions are met.
A good advice is to never use the Statement class, better be safe than sorry :)
Respect Java Conventions (or be coherent).
In your example you define the String Query, while all the other variables start with lower-case (as in Java Conventions), instead of String query. Overtime, such little mistakes (that won't break a build) will lead to bugs due to mistaking variables with classnames etc :)
Good luck on your road to mastering this wonderful language ! :)
First add a space before the quotation marks like this :
String Query= "INSERT INTO AirsoftGunRentals(NameOfGun, Brand, TypeOfGuns, NumberOfMagazines,Extras,NumberAvailable,UnitRent) "+
" VALUES('"+pName+"','"+pBrand+"','"+pTypeOfGun+"','"+pNumMags+"','"+pExtras+"','"+pNumberAvail+"','"+pRent+"');";
And use stmt.executeUpdate(Query); instead of : stmt.executeQuery(Query);in your insert, update and delete queries. For select queries you can keep it.
I managed to find an answer on how to add, delete and update records to a MS Access DB. This is what I found, after I declared the connection, and the prepped statement. I will try to explain to the best I can. I had to add values individually using this:
(pstmt = Prepped Statement Variable)
pstmt.setWhatever(1,Variable);
And it works fine now. I use the same method to delete and update records.
This is the basic query format:
String SQLInsert = "INSERT INTO Tbl VALUES(NULL,?,?,?,?)";
The NULL in the statement is the autonumber in the table. and .setWhatever() clause replaces the question marks with the data types. Thus manipulating the database.
Thank you everyone for all your contributions. It helped a lot, and made this section a lot more understandable.

Categories

Resources