hsqldb 2.3.2 jdbc driver does not support ResultSet.first()? - java

I am playing with HSQLDB+JDBC driver using JDK 8.
Using rs.next() looping results works fine, however, using rs.first() does not work: feature is not supported ?! Is is by design or a bug?
I plan to access hsqldb using Spring jdbc template, and I am concerned that I may stuck if I encounter such issue later on.
String jdbcUrl = "jdbc:hsqldb:hsql://localhost:9999/configdb";
try(Connection con = DriverManager.getConnection(jdbcUrl, "SA", "");
PreparedStatement stmt = con.prepareStatement(
"SELECT * FROM contacts");
) {
ResultSet rs = stmt.executeQuery();
// rs.first() does not work !
while(rs.next()){
//do sth here
}
} catch (SQLException e) {
throw new RuntimeException("test jdbc connection failed", e);
}

Try to make your ResultSet scrollable:
PreparedStatement stmt= conn.prepareStatement("SELECT * FROM contacts",
ResultSet.TYPE_SCROLL_SENSITIVE,
ResultSet.CONCUR_UPDATABLE);
I think that should probably work

Related

MySQL not pushing insert into database with netbeans

Recently I'm just learning some HTML, JSP and servlets for a university project, the thing is that I made a database into MySQL Workbench with id primary key, auto increment , then some fields like username, password, firstname, lastname, and so on.
The goal is to make a login page and register page, for some reason if I push data with MySQL Workbench into the database it will let me retrieve it with my login form and my select statment, but for some reason I'm doing the same thing with register but in this case with the query INSERT.
So, after research, I did preparestatment and changed the executeQuery to executeUpdate and everything, but my log says a nullPointerException somewhere, I know it may be a simple and silly error that I'm not seeing, but I'm new at this. This is what U have made so far to insert data into my database:
public static UserBean registarUsuario(UserBean bean){
//preparing some objects for connection
Statement stmt = null;
ResultSet rs = null;
try {
Class.forName("com.mysql.jdbc.Driver");
} catch (ClassNotFoundException ex) {
System.out.println("Error al cargar el driver");
System.out.println(ex.getMessage());
}
String firstname = bean.getFirstName();
String lastname = bean.getLastName();
String username = bean.getUsername();
String password = bean.getPassword();
boolean admin = bean.isAdmin();
int tipo = bean.getType();
String insertQuery =
"insert into idusuario (firstname,lastname,username,password,admin,tipo) values ('"+firstname+"','"+lastname+"','"+username+"','"+password+"','"+admin+"','"+tipo+"')";
System.out.println("Firstname is " + firstname);
System.out.println("Surname is " + lastname);
System.out.println("Query: "+insertQuery);
try
{
//connect to DB
currentCon = DriverManager.getConnection("jdbc:mysql://localhost:3306/usuarios", "root", "admin");
rs = stmt.executeQuery(insertQuery);
...
My output:
Info: Query: insert into idusuario
(firstname,lastname,username,password,admin,tipo) values
('jhon','marston','jmar','123','true','0') Info: Error :
java.lang.NullPointerException
The thing is that Netbeans doesn't even tell me where the NPE is happening so I'm kind of confused, I don't know if the query is wrong or if something else is, because as I can see in my output, the query seems ok.
I leave you here my database structure
You are assigining the stmt as null and never initializing it.
Statement stmt = null;
ResultSet rs = null;
Then you are trying to use it:
rs = stmt.executeQuery(insertQuery);
You will need to do something like this before you use it:
PreparedStatement stmt=currentCon.prepareStatement(yourQuery);
So, after research, i did preparestatment and changed the executeQuery
to executeUpdate and everything, but my log says a
nullPointerException somewhere, i know it may be a simple and silly
error that im not seeing, but understand that im new at this. this is
what i have made so far to insert data into my database
When we use insert,update or delete we need to use executeUpdate.
When we use select we need to use executeQuery.
In your example you are doing executeQuery for an insert. This is wrong. You need to use this:
rs = stmt.executeUpdate(insertQuery);
You're getting a NPE because you are trying to retrieve the results where there are none.
Here is a nice thing to do to help you reduce boilerplate code... (so you don't have to keep repeating yourself with db initialization values)
Create a class for your database connection:
public class DBConnection {
private static String url = null;
private static Connection conn = null;
public static Connection getConnection(){
try{
Class.forName("com.mysql.jdbc.Driver");
url = "jdbc:mysql://localhost:3306/usuarios";
conn = DriverManager.getConnection(url,"root","admin");
} catch (Exception e) {
System.out.println(e);
}
return conn;
}
}
Now you can use this in all your other classes like this:
public static UserBean registarUsuario(UserBean bean){
try(Connection conn= DBConnection.getConnection()){
PreparedStatement pst = conn.prepareStatement("insert into idusuario (firstname,lastname,username,password,admin,tipo) values (?,?,?,?,?,?);");
pst.setString(1, bean.getFirstName());
pst.setString(2, bean.getLastName());
pst.setString(3, bean.getUserName());
pst.setString(4, bean.getPassword());
pst.setBoolean(5, bean.isAdmin());
pst.setInt(6, bean.getType());
pst.executeUpdate();
}catch (SQLException e) {
e.printStackTrace();
}
}

ResultSet.next() is taking too much time for few records

I'm using OJDBC(v7) to connect to Oracle(11g), in Java.
In some cases on some big tables, the Resultset can not fetch the data in appropriate time.
For example the output records are just 2, but on the Resultset.next() java freezes and waits too long!
Note1: The problem is not about setting FetchSize(), RsultSet.TypeX , not using connection pools like c3p0 , ... . I've tested all of those.
Note2: Also when I run the query directly in navicat, the result is shown perfectly!
Getting connection method:
public Connection getDBConnection() throws DBConnectionException {
Connection conn = null;
String connectionUrl;
try {
Class.forName("oracle.jdbc.driver.OracleDriver");
conn = DriverManager.getConnection("jdbc:oracle:thin:user/pass#xxx.xxx.xxx.xxx:1521:DBNAME");
} catch (Exception e) {
e.printStackTrace();
throw new DBConnectionException();
}
return conn;
}
connectiong to DB part:
...
conn = connectionManager.getDBConnection();
conn.setAutoCommit(false);
String query = "{call ...(...)}";
CallableStatement stmt = conn.prepareCall(query,ResultSet.TYPE_FORWARD_ONLY,
ResultSet.CONCUR_READ_ONLY);
stmt.setFetchSize(10000);
.
.
.
stmt.registerOutParameter(x, OracleTypes.CURSOR);
stmt.execute();
Resultset rs = (ResultSet) stmt.getObject(x);
while (rs.next()) { /** Problem occurs here **/
...
}
WHY?!
use in try catch block:
if ((cnn==null)||cnn.isClosed()){
cnn=DB.getDBConnection(); //e.g. DB is instance of class where getDBConnection() resides
}
Then call queries. I believe somewhere in your code either you are closing connection or connection is becoming null that's why you are facing this problem.
Cheers

Delete data from database using jsp

I want to delete a particular record from the database.
My code is given below:
<%
String value = request.getParameter("Meter_No");
int v=Integer.parseInt(value);
try{
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDirver");
Connection conn = DriverManager.getConnection ("jdbc:sqlserver://localhost:1433;databaseName=myDatabase;
user=shakir;password=shakir123");
Statement st = conn.createStatement();
ResultSet rs =st.executeQuery("DELETE * FROM qesco_table
WHERE Meter_No ="+v+"");
rs.close();
conn.close();
}catch(Exception e){
System.out.print(e.getMessage());
}
%>
But it is not deleting the data from database.
Can anyone guide me that where is the problem with the code?
Every thing in your code is fine.
But You need to run your query using st.executeUpdate().
So change the following line
ResultSet rs =st.executeQuery("DELETE * FROM qesco_table WHERE Meter_No ="+v+"");
to
st.executeUpdate("DELETE * FROM qesco_table WHERE Meter_No ="+v);
PLUS
You don't need to have ResultSet in this program as your query is not going to return you any data to store.
You don't need to have empty "" (double quotes) at the end of your query.
You should close connection and others in finally block rather than try block itself.
And better if you try to use PreparedStatements to write dynamic queries. So it will become something like,
<%
String value = request.getParameter("Meter_No");
int v=Integer.parseInt(value);
Connection conn = null;
PreparedStatement pst = null;
try{
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDirver");
conn = DriverManager.getConnection ("jdbc:sqlserver://localhost:1433;databaseName=myDatabase;user=shakir;password=shakir123");
pst = conn.prepareStatement("delete from qesco_table where Meter_No = ?");
pst.setInt(1,v);
pst.executeUpdate();
}catch(Exception e){
System.out.print(e.getMessage());
}finally{
pst.close();
conn.close();
}
%>
To delete a record from data base using JDBC you need to use executeUpdate("your query") method.The executeQuery() query is used when you want to retrieve data from data base.
Then query should be
DELETE FROM qesco_table
WHERE Meter_No ="+v+"
It is not Delete * from table and correct is Delete from table
Change to
st.executeUpdate("DELETE FROM qesco_table WHERE Meter_No ="+v);
try{
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDirver");
Connection conn = DriverManager.getConnection ("jdbc:sqlserver://localhost:1433;databaseName=myDatabase;user=shakir;password=shakir123");
Statement st = conn.createStatement();
ResultSet rs =st.executeUpdate("DELETE * FROM qesco_table WHERE Meter_No ="+v);
}
catch(SQLException e){
System.out.print(e.getMessage());
}
finally
{
rs.close();
conn.close();
}
I suggest to avoid Scriplet instead use JSP Standard Tag Library and Expression language that is easy to user and less error prone.
One more thing you should move this database code in a servlet and call it from JSP. If you are still interested to do it in JSP then you should use SQL Tag Library that is designed for database access from JSP.
Sample code:
<sql:setDataSource var="dataSource"
driver="com.microsoft.sqlserver.jdbc.SQLServerDirver"
url="jdbc:sqlserver://localhost:1433;databaseName=myDatabase"
user="shakir" password="shakir123" />
<sql:update dataSource="${dataSource}"
sql="DELETE FROM qesco_table WHERE Meter_No =?">
<sql:param value="${param.Meter_No }" />
</sql:update>
try this bro!! it will work for sure 101%
<%
String value = request.getParameter("Meter_No");
int v=Integer.parseInt(value);
try{
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDirver");
Connection conn = DriverManager.getConnection ("jdbc:sqlserver://localhost:1433;databaseName=myDatabase;user=shakir;password=shakir123");
Statement st = conn.createStatement();
String sql = "DELETE FROM qesco_table WHERE Meter_No= '"+v+"'";
st.executeUpdate(sql);
}
catch(Exception e){
System.out.print(e.getMessage());
}
%>
Class.forName("com.mysql.cj.jdbc.Driver");
Connection c=DriverManager.getConnection("jdbc:mysql://localhost:3306/advjava", "root", "root");
Statement stat=c.createStatement();
int del=stat.executeUpdate("Delete from Cart where U_Id="+uid);

Using SQL statements in IBM Maximo, using Java

Is it possible to write a pure sql statement for Maximo using Java?
At the moment I use myObj.setWhere("employee = 'brian'");
But I'd like to do:
myObj.xxx("select * from employee where name = 'brian'");
is there such a function?
While I would strongly discourage it, you can run direct SQL statements without needing to deal with the driver manager yourself. You can just request a connection from Maximo.
Most of the time (like in the below examples), Maximo already provides the functionality for the statement you would run, or you will be selecting a subset of fields that Maximo, as a basic ORM, can't handle. In those cases, you wouldn't want to do this.
Connection con = null;
try {
con = getMboServer().getDBConnection(getUserInfo().getConnectionKey());
PreparedStatement stmnt = con.prepareStatement("update asset set description = 'Hello World'");
stmnt.execute();
stmnt = con.prepareStatement("select count(*) from asset");
if (stmnt.execute()) {
ResultSet results = stmnt.getResultSet();
if (results.next()) {
int count = results.getInt(1);
}
}
} catch (SQLException e) {
logger.error("There was an 'SQLException' exception while getting the count of assets; The error is:\n", e);
} finally {
if (con != null) {
getMboServer().freeDBConnection(getUserInfo().getConnectionKey());
}
}
I think Maximo will work with the java.sql package, which can be used like so:
import java.sql.*
String connectionString = "Your JDBC connection string here";
Connection conn = java.sql.DriverManager.getConnection(connectionString);
String sQuery = "SELECT SAMPLE_COLUMN FROM SAMPLE_TABLE";
Statement stmt= conn.createStatement();
ResultSet result = stmt.executeQuery(sQuery);
Read here how to parse through a ResultSet to get the information you want: http://docs.oracle.com/javase/7/docs/api/java/sql/ResultSet.html

ResultSet.TYPE_FORWARD_ONLY how does it actually work?

I have the following code:
try(Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/test","root","VENIVIDIVICI");
Statement st = con.createStatement(ResultSet.TYPE_FORWARD_ONLY,ResultSet.CONCUR_READ_ONLY);
ResultSet rs = st.executeQuery("SELECT * FROM example184"))
{
rs.absolute(2);
rs.absolute(1);
rs.beforeFirst();
while (rs.next())
{
System.out.println(rs.getString("iD"));
}
while(rs.previous())
{
System.out.println(rs.getString("Id"));
}
}
catch(SQLException sql ){System.out.println(sql);}
}
It should give a SQLException since it's only ResultSet.TYPE_FORWARD_ONLY, instead it works fine without throwing any exception.
UPDATE: As Aleroot suggested in the below response, I have tried the following modifications:
try(Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/test","root","VENIVIDIVICI");
Statement st = con.createStatement(ResultSet.TYPE_FORWARD_ONLY,ResultSet.CONCUR_READ_ONLY);
ResultSet rs = st.executeQuery("SELECT * FROM example184"))
{
st.setFetchSize(Integer.MIN_VALUE);
rs.afterLast();
while (rs.previous())
{
System.out.println(rs.getString("iD"));
}
}
catch(SQLException sql ){System.out.println(sql);}
}
}
without success, it still compiles and run properly.
Thanks,
Indeed ItIs
TYPE_FORWARD_ONLY means that the ResultSet can only be navigated forward. You cannot move backwards in the ResultSet.
In this case I think the call to previous() is allowed because is cached, try to change the fetch size :
st.setFetchSize(Integer.MIN_VALUE);
Or to read the resultset from the end to the start without previously reading from start to end(with next) ...
To move backward try :
rs.afterLast()
while (rs. previous())
{
System.out.println(rs.getString("iD"));
}
If it in both cases the driver keep executing without throwing the SQLException, it could be a bug of the driver you are using...

Categories

Resources