copy column values if column name is a date - java

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

Related

How can I batch select from several tables with jdbctemplate?

I have the following query:
select name_table.name, age_table.age, address_table.address
from
name_table name_table,
age_table age_table,
address_table address_table
where
name_table.id = age_table.id
and age_table.id = address_table.id
and name_table.new_date between ?(start_date) and ?(end_date)
How can i use JDBCTemplate to get the needed data in batches? The start_date and end_date are variables.
First your query is not correct:
from the syntax point of view "date" is a reserved word and
from the logic point of view, you are doing a cross table join over the 3 tables because you don't have any condition to join them in the WHERE clause (and you should better do it using standard ANSI join).
Second if your intent is to have the table names be parameters of the template, the answer is NO: table names and column names can't be parameters of a query.
This has been answered several times on stackoverflow, including by AskTOM site's guys, explaining in details why: mainly the data structure of the result must be known at parse time.

how can solve these trouble when i convert form apathy server DB to derby DB

because i'm convert my concoction DB to derby in netbeans
all statement happen error in it.
the error is it
java.sql.SQLSyntaxErrorException: table or view does not exist.
to solve problem must be change in all attribute and tables name
by Place it between brackets
for example
st.executeQuery("SELECT * FROM loges ");
not run its syntax error
must change to
st.executeQuery("SELECT * FROM \"loges\" ");
Then it works properly
doing this very hard,i have 137 query statement contain a lot
of tables and attribute names.
i'm doing this change because extract my project to executable desktop program
if can make this in other way will be good
If your code uses all unquoted table and column names, then your database schema should be created with unquoted names.
If you're having trouble now, then your new database was created/migrated with quoted names. Fix that, and you code will work unchanged.
Be aware that databases treats unquoted names differently, e.g. Oracle will change the names to uppercase, PostgreSQL will change the names to lowercase, and MS SQL Server will store the names as given, but will by default match them case-insensitively.
You should create the tables without double quotes, as explained in the duplicate question

find column names and metadata from select query without executing the query

I have a scenario, where the user will provide a Select statement. I need to find out the columns (their names, type, and other metadata), but I do not want to execute the query.
I know that I can execute the query and figure it from the ResultSet, but if the query returns many rows, then it may not be a good approach.
For example, consider the query
select name, age from people where people.dob = '1976';
Is there a way of getting the projected column metadata (i.e. metadata of name and age columns) without executing the query?
Solved this by using a PreparedStatement. A PreparedStatement does not execute the statement.

PreparedStatement Update showing error ORA-00927 missing equal sign

I am trying to update the records in database according to data read from an Excel sheet. I have more than 50 columns in db whose column names are stored in an array columnNames[].
I use following code to create the Sql query.
String sqlUpdate= "Update "+tableName+
" set "+columnNames[0]+"=?";
for (int i=1;i<columnCount;i++)
{
sqlUpdate= sqlUpdate+","+columnNames[i]+"=?";
}
sqlUpdate= sqlUpdate+
" where demand_id=?";
the equivalent query obtained to printing it on console is :
Update fulfillment_plan set DEMAND_ID=?,SBU=?,PROJ_DOMAIN=?,JOBCODE=?,INDENT_STATUS=?,JC_CREATED_ON=?,PROJECT_NAME=?,CUSTOMER_NAME=?,GROUP_CUSTOMER=?,US_DEMANDS=?,SUITE_NAME=?,ROLE_NAME=?,LOCATION=?,COUNTRY=?,GEO=?,AREA=?,OPEN_POS=?,PRODUCT=?,DEMAND_TYPE=?,POSITIONS_TO_FULFILL_Q4=?,FULFILLMENT_PLAN_Q4=?,TA_STATUS_Q4=?,POSITIONS_TO_FULFILL_Q3=?,FULFILLMENT_PLAN_Q3=?,TA_STATUS_Q3=?,POSITIONS_TO_FULFILL_Q2=?,FULFILLMENT_PLAN_Q2=?,TA_STATUS_Q2=?,POSITIONS_TO_FULFILL_Q1=?,FULFILLMENT_PLAN_Q1=?,TA_STATUS_Q1=?,NET_ADD_TYPE=?,ESSENTIAL_SKILL=?,SUITE_SKILLS=?,ADDITIONAL_SKILLS=?,POSITIONS_WITH_PROPOSALS=?,POSITIONS_WITHOUT_PROPOSALS=?,DEM_ST_DATE=?,OVER_DUE_STATUS=?,OVERDUE_DAYS=?,LEAD_TIME_DAYS=?,LEAD TIME BUCKET=?,DEM_END_DATE=?,CREATED_ON=?,INDENT_CREATED_ON=?,EBD=?,OPPORTUNITYID=?,LOAD_DATE=?,PROJECT_NUMBER=?,CUSTOMER_NO=?,CUSTOMER_SUB_GEO=?,DEMAND_STATUS=?,ENGAGEMENT_TYPE=?,INVOICE_TYPE=?,INDENT_CLASSIFICATIONS=?,PROJ_STAT=?,EFD_SLA=?,RM_EMP_NAME=?,MONTH=?,QUARTER=?,YEAR=?,ACCOUNT_ID=?,ACCOUNT_TEXT=?,STATUS=? where demand_id=?
Then i have set the values to the '?' and on executing the above prepared statement in am getting the "missing equal sign" error. I have been looking into it for around 3 hours now and am not able to solve it. Kindly help.
I suspect this is due to the LEAD TIME BUCKET column name, which should either have underscores (like the other column names) or be escaped somehow - the spaces within the column name are causing the error. It would be better to have underscores in order to be consistent with your other columns, and to make the SQL simpler.
(I'd also suggest adding spaces within your SQL - e.g. one after every comma - so that the SQL can be reformatted in a text editor by line-breaking on spaces, making it easier to read. I'd have more whitespace in the Java code too, but that's clearly a matter of personal/team preference.)

Simple getColumnName(0) call throws Invalid column index: getValidColumnIndex

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.

Categories