I'm trying to write a JTable that takes the data from a ResultSet and uses that to create a dynamic sized table with appropriate column names and row data values from the ResultSet but I can't get JDBC to get the column names for me dynamically.
I know my select statement is good! I can print the results out easily with my ResultPrinter class that I wrote but I can't seem to get the column names for some reason.
The code: http://pastebin.com/SSNdCkNu
The output:
Connected to DB!
SNUM, SNAME, STATUS, CITY, SUPPLIERS_ID_SEQ // printed by static Suppliers class
Columns: 5 // result set shows there are 5 valid columns as expected
Exception in thread "main" java.sql.SQLException: Invalid column index: getValidColumnIndex
at oracle.jdbc.driver.OracleResultSetMetaData.getValidColumnIndex(OracleResultSetMetaData.java:138)
at oracle.jdbc.driver.OracleResultSetMetaData.getColumnName(OracleResultSetMetaData.java:306)
at Main.main(Main.java:15)
JDBC column indexes start from 1 and not 0. As far as possible, it is better to retrieve data using column names to avoid hard dependency on the order of columns in the results.
Column index starts by 1. So increase your variable pointing column variable by 1.
Related
I found a SQL query in some Java code that looks like this:
SELECT * FROM tableA JOIN tableB ON ...
Both tableA and tableB contain the field named id, and the Java code that executes this query does this:
Integer id = (Integer)rs.getObject("id");
Does the JDBC spec have anything to say about which field value will be returned? I haven't been able to find anything that says one way or another. I see that some databases throw an error but MySQL/MariaDB (the database in use, here) do not complain and the value returned seems to be tableA.id, or the "leftmost" field whose name is "id".
I'm asking if there is spec-defined behavior in this case, or if it's up to the database and/or JDBC driver to decide how to behave.
Needless to say, I'll be fixing this to work predictably and unambiguously while I'm looking at the code, but I'm curious as to the answer.
The API documentation of ResultSet answers your question:
Column names used as input to getter methods are case insensitive.
When a getter method is called with a column name and several columns
have the same name, the value of the first matching column will be
returned.
The JDBC 4.3 specification document in section 15.2.3 Retrieving Values also says:
The columns are numbered from left to right, as they appear in the
select list of the query, starting at 1.
Column labels supplied to getter methods are case insensitive. If a
select list contains the same column more than once, the first
instance of the column will be returned.
The index of the first instance of a column label can be retrieved
using the method findColumn. If the specified column is not found,
the method findColumn throws an SQLException.
So, if a result set has multiple columns with the same (case insensitive(!)) name, then the value of the first matching column is returned.
I am trying to copy all values from one table to another table. I am using SQlite. I have multiple columns with the following column names - name,accounts,email, 12/30/2016,01/13/2017.... All columns are named as dates except for the first 3 columns. I am trying to copy using this statement:
PreparedStatement prepCopy = con
.prepareStatement("INSERT INTO table1(12/30/2016) SELECT 12/30/2016 FROM table2");
prepCopy.execute();
But I am getting this error:
near "12": syntax error:
I tried doing this for the other columns like name, accounts, and email and it works. This error only occurs on the date column names. I also tried putting '' before and after the date but then it populates all rows with the date itself. I think I am missing a small detail, but I can't seem to figure it out.
Any suggestion is appreciated. :)
Surround the dates - in those cases in which they are column names - with double quotes or backticks.
INSERT INTO table1("12/30/2016") SELECT "12/30/2016" FROM table2
See also: http://www.sqlite.org/lang_keywords.html
I am working on Java coding..
Trying to set author value:
XXX.setStatusValue(YYY.AUTHOR.toString());
When trying to insert this value in database,
Here is the error I get:
too long for column 'status_value' at row 1
I have not designed the tables, just using the existing ones.
Here is the error i get: too long for column 'status_value' at row 1
The error says it all.
The value you are trying to insert into the database is larger than the maximum length allowable in the field in the table.
Either insert a shorter string, or make the field in the database longer.
I think all database columns have it's own length and you cannot set value have bigger than that. If you don't know how long is your column length, just try insert value with increase length (example from 1 to 200). When error occurs, you will know exactly length of column.
I need to know what the number X needs to be in order to receive the data properly.
Example code :
Statement sta = (connection object).createStatement();
sta.executeQuery("SELECT 'points' FROM TABLEX WHERE 'player'='" + player_name + "'").getString(X); ///HERE
Either 1, or "points" will work.
1 is the index of the column as specified in the select statement. The indexing starts from 1 and increments from there.
Otherwise the name of the column may be used, in this case "points". That method may cause a bit more meta data to get loaded and so performance can vary.
As the javadoc says :
getString(int columnIndex)
Retrieves the value of the designated column in the current row of this ResultSet object as a String in the Java programming language. and
getString(String columnLabel)
Retrieves the value of the designated column in the current row of this ResultSet object as a String in the Java programming language.
So this is not going to work. It will give you :
java.sql.SQLException: Before start of result set
First of all you need to iterate through the Resultset obtained using next() then you can retrieve the specific values by either pass 1 which is the column index in this case or points which is the columnName of your table and based on the where clause it shall give you different values of the column points
X is the column number(index) in the table.
It is needed to fetch the data from table.
You also can use column name instead of column index like this -
getString(ColumnName);
In my java web application, I am inserting some records into MySql db. I have a column name 'value' whose Field type is float. When I insert the following value 76.8653846153846 into db, it gives exception of Data truncated for column 'value' at row 1. I changed field type to Double but same message. Any idea?
Field description:
As per the documentation you must define your number type with the level of precision you require.
for your number 76.8653846153846 use
FLOAT(10,13)
The 13 being large enough to handle your .8653846153846
[Edit]
to alter your existing table execute this command substituting your table and column name for mytable and mycolumn respectively
ALTER TABLE mytable MODIFY mycolumn FLOAT(10,13)
This comes from the database engine where the declared field's description in the table is not big enough to insert the complete data