This seems like a really simple problem, but I cannot figure out what my problem is. I have a method addTask which adds some info to our database as seen in this code:
public static boolean addTask(String name, String question, int accuracy, int type){
StringBuilder sql = new StringBuilder();
sql.append("INSERT INTO tasks (name, question, type, accuracy) ");
sql.append("VALUES(?, ?, ?, ?)");
try {
Connection c = DbAdaptor.connect();
PreparedStatement preparedStatement = c.prepareStatement(sql.toString());
preparedStatement.setString(1, name);
preparedStatement.setString(2, question);
preparedStatement.setInt(3, type);
preparedStatement.setInt(4, accuracy);
preparedStatement.execute();
preparedStatement.close();
c.close();
return true;
}
catch (SQLException e) {
e.printStackTrace();
return false;
}
}
my problem is that preparedStatement.execute() always returns false, indicating the information hasnt been added to the database. I can run psql and this confirms that nothing has been written to the db. The connection definitely connects to the correct database (i put in some other printlns etc. to check this). I am trying to insert into a newly initialised table that looks like this:
CREATE TABLE tasks
(
id SERIAL PRIMARY KEY,
submitter INTEGER REFERENCES accounts (id),
name VARCHAR(100) NOT NULL,
question VARCHAR(100) NOT NULL,
accuracy INTEGER NOT NULL,
type INTEGER REFERENCES types (id),
ex_time TIMESTAMP,
date_created TIMESTAMP
);
code for DbAdaptor.connect():
public static Connection connect(){
try {
Class.forName("org.postgresql.Driver");
} catch (ClassNotFoundException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
Properties properties = new Properties();
properties.setProperty("user", USER);
properties.setProperty("password", PASSWORD);
try {
return DriverManager.getConnection(URL, properties);
} catch (SQLException e) {
e.printStackTrace();
}
return null;
}
where USER and PASSWORD are static fields in the class
You misunderstood the return value of PreparedStatement#execute().
Please carefully read the javadoc:
Returns:
true if the first result is a ResultSet object; false if the first result is an update count or there is no result.
It thus returns — as fully expected — false on an INSERT query. It returns only true on a SELECT query (for which you'd however usually like to use executeQuery() instead which returns directly a ResultSet).
If you're interested in the affected rows, rather use PreparedStatement#executeUpdate() instead. It returns an int as per the javadoc:
Returns:
either (1) the row count for SQL Data Manipulation Language (DML) statements or (2) 0 for SQL statements that return nothing
A return value of 1 or greater would then indicate a successful insert.
Unrelated to the concrete problem: your code is leaking DB resources. Please carefully read How often should Connection, Statement and ResultSet be closed in JDBC?
Related
I'm new to working with JDBC commands. I have a database in MYSQL and each entry gets an ID. As initially created the ID was just a static variable that I iterated when the constructor runs. This was okay until I started deleting entries or running the program a second time. Then I start getting collisions. I need a way to return the highest row in the table and assign it to an integer that I can iterate.
The QuerySELECT MAX(ID) FROM table seems to get the value that I'm looking for. But I'm not sure of the syntax to get that value into an integer so I can return it.
public int getHighestRow() {
PreparedStatement ps;
int highestID = 0;
try {
ps = getSQLDB().prepareStatement("SELECT MAX(studentID) FROM student");
ps.execute();
} catch (SQLException e){
Logger.getLogger(Undergraduate.class.getName()).log(Level.SEVERE, null, e);
}
if (highestID > 0) return highestID;
else return 0;
I have a feeling this is very simple, but I wasn't able to find an existing answer. Or is there a more elegant way to do this in general?
SQL of different providers solve the retrieval of automatic generated keys differently. JDBC provides a standard solution.
Better use this JDBC solution, as it prevents mixing up those keys when insertions are done at the same time.
try (PreparedStatement ps = getSQLDB().prepareStatement(
"INSERT INTO student(....) VALUES(?, ..., ?)",
Statement.RETURN_GENERATED_KEYS)) { // Without StudentId
ps.setString(1, name);
...
ps.executeUpdate();
try (ResultSet rsKeys = ps.getGeneratedKeys()) {
if (rsKeys.next()) { // Only one record inserted
int studentId = rsKeys.getInt(1); // One key generated
}
}
} catch (SQLException e){
Logger.getLogger(Undergraduate.class.getName()).log(Level.SEVERE, null, e);
}
The mechanism with try(...). try-with-resources, ensures that close is called automatically.
So I am creating a small application for a football organisation that needs to be able to add teams to the database.
My database has the following ERD:
I have the following code to add teams to my database:
public void toevoegenPloeg(Ploeg ploeg) throws DBException, ApplicationException {
//connectie tot stand brengen
System.out.println(ploeg.getTrainerID());
try (Connection connection = ConnectionManager.getConnection();) {
//statement opstellen
try (PreparedStatement statement = connection.prepareStatement("insert into ploeg (naam, niveau, trainer_id) values(?,?,?)");) {
statement.setString(1, ploeg.getNaam());
statement.setString(2, ploeg.getNiveau());
statement.setInt(3, ploeg.getTrainerID());
statement.execute();
} catch (SQLException ex) {
throw new DBException("SQL-exception in de toevoegenPloeg-methode - statement" + ex);
}
} catch (SQLException ex) {
throw new DBException("SQL-exception in de toevoegenPloeg-methode - connectie " + ex);
}
}
It has to be possible to add teams without a trainer.
Like this:
PloegTrans PT = new PloegTrans();
PersoonTrans PeT = new PersoonTrans();
Ploeg ploeg1 = new Ploeg();
ploeg1.setNiveau("U9");
PT.ploegToevoegen(ploeg1);
Trainer_id is an int and because I haven't defined the trainer_id.
The trainer_id becomes the default of an int, 0.
But then I get a Foreign Key exception, because the database looks for a trainer with id 0.
How can I overcome this?
How can I initialize my int as a "null"?
Use statement.setNull(3, java.sql.Types.INTEGER); or construct the statement as insert into ploeg (naam, niveau) values(?,?), so trainer_id will default to NULL.
On the POJO's side, the trainer ID should be a java.lang.Integer, not a primitive int, in order to allow nulls. On the JDBC side, you could use setObject instead of setInt, which does accept nulls:
// getTrainerID() returns either an Integer instance or null
statement.setObject(3, ploeg.getTrainerID());
I try to put some Data in my H2 database but I'm a total noob in databases so it throws error over error since more than a hour.
Normaly I can fix it somehow but now I got a new problem I try to use
getGeneratedKeys() first I tried to use AUTO_INCREMENT(1,1) but that didn't works too function but it won't work rigth.
The exception my programm throws is
org.h2.jdbc.JdbcSQLException: Funktion "GETGENERATEDKEYS" nicht gefunden
Function "GETGENERATEDKEYS" not found; SQL statement:
insert into logTbl values( getGeneratedKeys(),Webservice->startThread0: Thread0) [90022-173]
an my database function looks like this
public void createTable(String Log) {
try {
Class.forName("org.h2.Driver");
} catch (ClassNotFoundException e) {
System.err.println("TREIBER FEHLER");
e.printStackTrace();
}
Connection conn = null;
try {
conn = DriverManager.getConnection("jdbc:h2:~/DBtest/Logs");
Statement stat = conn.createStatement();
stat.execute("CREATE TABLE IF NOT EXISTS logTbl(ID INT PRIMARY KEY, LOG VARCHAR(255))");
//stat.execute("insert into test values(1, 'Hello')");
for (int i = 0; i < 20; i++) {
stat.execute("insert into logTbl values( getGeneratedKeys()," + Log + ")");
}
stat.close();
conn.close();
} catch (SQLException e) {
System.err.println("SQL FEHLER");
e.printStackTrace();
}
}
hope you can help me to fix my error as I said I'm totaly new and just had some code example as "tutorial" because I don't found a good tutorial
If you want to automatically generate primary key values, you need to first change the definition of your table:
CREATE TABLE IF NOT EXISTS logTbl
(
ID integer AUTO_INCREMENT PRIMARY KEY,
LOG VARCHAR(255)
);
You should also use a PreparedStatement rather than concatenating values.
So your Java code would look something like this:
String insert = "insert into logTbl (log) values(?)";
PreparedStatement pstmt = connection.prepareStatement(insert, Statement.RETURN_GENERATED_KEYS);
pstmt.executeUpdate();
ResultSet rs = pstmt.getGeneratedKeys();
long id = -1;
while (rs.next())
{
rs.getLong(1);
}
It might be that you need to use the overloaded version of prepareStatement() where you supply the column to be returned. Not sure which one works with H2:
prepareStatement(insert, new String[] {"ID"});
Btw: there is nothing "magic" about 255 as the length of a varchar column. There is no performance difference between varchar(500), varchar(20)or varchar(255). You should use the length that you expect you need, not some "magic" limit you think performs better.
Following is my Table Definition:
create Table alarms(
alarmId int primary key identity(1,1),
alarmDate varchar(50) not null,
alarmText varchar(50) not null,
alarmStatus varchar(10) Check (alarmStatus in(-1, 0, 1)) Default 0
);
Secondly here are some of my methods i'm using:
public void restartDatabase(){
try{
Class.forName(Settings.getDatabaseDriver());
connection = DriverManager.getConnection( Settings.getJdbcUrl() );
statement = connection.createStatement();
}
catch(Exception e){
e.printStackTrace();
}
}
public ResultSet executeQuery(String query){
ResultSet result = null;
try {
result = statement.executeQuery(query);
} catch (SQLException e) {
e.printStackTrace();
}
return result;
}
public void closeDatabase() {
try {
if ((statement != null) && (connection != null)) {
statement.close();
connection.close();
}
} catch (Exception e) {
e.printStackTrace();
}
}
What i want to do is to get all the alarmId's from the table where date is equal to the given date and then against each alarmId i want to update its status to given status:
public static void updateAlarmStatus(int status) {
ResultSet rs = null;
database.restartDatabase();
try {
rs = database
.executeQuery("Select alarmId from alarms where alarmDate = '"
+ Alarm.getFormattedDateTime(DateFormat.FULL,
DateFormat.SHORT) + "'");
while (rs.next()) {
database.executeUpdate("update alarms set alarmStatus = '"+status+"' where alarmId = '"+rs.getString("alarmId")+"'");
}
} catch (Exception e) {
e.printStackTrace();
} finally {
database.closeDatabase();
}
}
But it generates the Error that Result Set is Closed.
I Goggled it and came to know that a result set automatically closes when we try to execute another query inside it
and it needs to restart the connection.
i tried calling restartDatabase() method that is creating new connection but still getting the same error.
I'm guessing executeUpdate uses the same instance variable for its Statement as the query uses. When you create a new Statement and assign it to the variable, nothing is referring to the old one, so it gets cut loose and becomes subject to garbage-collection. During garbage collection the statement's finalizer is invoked, closing it. Closing the statement makes the ResultSet it created close as well.
You shouldn't be sharing these Statement variables between different queries and updates. The statement should be a local variable and not a member of an object instance.
Also, result Sets should always be local variables, they shouldn't be passed outside the method where they're created. The resultSet is a reference to a cursor, it doesn't actually hold any data. Always have your code read from the resultSet and populate some data structure with the results, then return the data structure.
You can also select and change all alarmIds at once:
rs = database.
executeQuery("Select group_concat(distinct alarmId) as alarmIds from alarms group by alarmDate having alarmDate = '"
+ Alarm.getFormattedDateTime(DateFormat.FULL,
DateFormat.SHORT) + "'");
while (rs.next()) { // there will be only one result
database.executeUpdate("update alarms set alarmStatus = '"+status+"' where alarmId in ("+rs.getString("alarmIds")+")");
}
I am using Java to connect to a MySQL database. I am trying to insert or update data into the database.
Even though I am quite sure the insert was successful, it returns false.
According to the "execute" API, the return value is "true if the first result is a ResultSet object; false if it is an update count or there are no results".
How can I determine whether or not my insert or update was successful?
public boolean insertSelections(String selection, String name){
String sql ="INSERT INTO WORKREPORT VALUES (?,?,?,?,?)";
boolean action = false;
try {
PreparedStatement stmt = conn.prepareStatement(sql);
SimpleDateFormat dateFormat = new java.text.SimpleDateFormat("yyyy:MM:dd hh:mm:ss");
String formatDate = dateFormat.format(new java.util.Date(System.currentTimeMillis()));
java.util.Date mDate = dateFormat.parse(formatDate);
java.sql.Timestamp timeStamp = new java.sql.Timestamp(System.currentTimeMillis());
// Date time= new Date(mDate.getTime());
stmt.setInt(1, Integer.parseInt(getNumberByName(name).trim()));
stmt.setString(2, name);
// stmt.setDate(3, time);
stmt.setTimestamp(3, timeStamp);
stmt.setString(4, selection);
stmt.setString(5, "N/A");
action = stmt.execute();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return action;
}
Since you are using PreparedStatement you can call executeUpdate() -
int count = stmt.executeUpdate();
action = (count > 0); // <-- something like this.
From the Javadoc (Returns) link above, emphasis added,
either (1) the row count for SQL Data Manipulation Language (DML) statements or (2) 0 for SQL statements that return nothing.
If you want to insert a large number of entries, I would prefer addBatch() and executeBatch().
First of all this you should know :
boolean execute()
Executes the SQL statement in this PreparedStatement object, which may be any kind of SQL statement.
ResultSet executeQuery()
Executes the SQL query in this PreparedStatement object and returns the ResultSet object generated by the query.
int executeUpdate()
Executes the SQL statement in this PreparedStatement object, which must be an SQL INSERT, UPDATE or DELETE statement; or an SQL statement that returns nothing, such as a DDL statement.
int i = stmt.executeUpdate();
if (i > 0) {
System.out.println("success");
} else {
System.out.println("stuck somewhere");
}
Try this and check it out whether insert is happening or not
If you don't get a exception I think query is went ok.
Or, you might be able to use executeUpdate() (http://docs.oracle.com/javase/7/docs/api/java/sql/PreparedStatement.html#executeUpdate() )
You can do a select count(*) do validate number of records if you want.
Try this, whether you want to know whether the data is inserted or not , if the record is inserted it return true or else false.
if(action > 0){
return true;
}else{
return false;
}