How to Select From Mysql table With Escapable Characters - java

i am trying to select tables from my mysql database , and the tables names may be any thing unsafe, since users choose them, i have tried to do it like this :
String table = "*jjs> o";
PreparedStatement stmt = conn.PrepareStatement("SELECT * FROM ? ");
stmt.setString(1,table);
stmt.executeQuery();
but seems it to throw exception due to unacceptable command, can some one help me how can i achieve that please? thanks

add a back tick(`) to table name
select * from `*jjs> o`

Ok Using BackTicks Was the Answer, Like select * from `*jjs> o` without statement.Thanks Alot

Related

Executing multi - statement query in one session

I have asked this question and wanted to edit it , however StackOverflow for some reason did not allow me to edit . So here is the edited version
For example a query :
create volatile table testTable as (select * from ... blah blah) ;
select top 10 * from testTable ;
drop table testTable ;
It executes perfect in sql assistance as one session. I am sure it is possible to execute it in Java in one session.
Goal : need to execute it in one session similar to sql assistant so that it is possible to refer to the volatile table in the subsequent select statement. Also the data from the select statement should be saved in the ResultSet
PS
I saw one answer to a similar question about mysql. The trick is to turn on allow multiple queries
String dbUrl = "jdbc:mysql:///test?allowMultiQueries=true";
For teradata specifically,
what is the solution ?
I tried
String dbUrl = "jdbc:odbc:dsn?allowMultiQueries=true";
What is exactly failing?
Is there an error message "testtable doesn't exist"? Then your program closes the connection after each request.
Is the table empty when you do the SELECT? Then you forgot to add ON COMMIT PRESERVE ROWS to the CREATE.

Selecting from FileMaker table via JDBC when the table name contains spaces

I am using JDBC to get data out of a file maker server v12.
For some unknown reason filemaker allows you to have spaces in your table names. I am unable to select these tables because I just get a syntax error.
I have written an application in java to get the data out. Does anyone have any idea how i can select the data from a table with a space in it?
EDIT (from OP's comments):
This is the Java part:
String selectSQL = "SELECT "+this.getImportableColumnsString()+" FROM "+this.getTableName();
PreparedStatement preparedStatement = this.connection.prepareStatement(selectSQL);
ResultSet rs = preparedStatement.executeQuery();
As mentioned in a comment to the question, if the FileMaker table name contains spaces then it must be enclosed in double-quotes in the SQL statement, e.g.,
String selectSQL = "SELECT * FROM \"table name\"";
My first thought is that you could put the table name within ' characters like: SELECT * FROM 'my table'. Does this not work?
Otherwise I suggest you contact the Filemaker Server support on this page:
http://help.filemaker.com/app/ask
It is likely that they have had this question before and knows how to build the query.
//Flipbed
I'm pretty sure in the docs for OCDB and JDBC support FileMaker says that tables may have to comply to naming conventions that are stricter than what FileMaker allows. It is easy to change table names in FileMaker if you have admin access to the database you are sourcing why not just replace the spaces in the table names with underscores.

Java JDBC call to Oracle 10 function "invalid identifier"

I'm having a problem I haven't encountered before: there is a stored function in a database: CC_PROC, which takes two date entries and returns a table. In other words, to call it, you type:
SELECT * FROM (TABLE( CC_PROC( DATE '2012-01-01', DATE '2012-01-15')));
This seems to work perfectly in SQLPlus and NetBeans, and the above line has been apparently been in use for some time.
Anyway, when calling it from java using a prepared statement, I get: "CC_PROC": invalid identifier on the executeQuery call.
This is with:
PreparedStatement preparedStatement =
connection.prepareStatement("SELECT * FROM (TABLE ( CC_PROC( ? , ? )))");
preparedStatement.setDate(1,firstDate);
preparedStatement.setDate(2,secondDate);
resultSet = preparedStatement.executeQuery();
I feel like maybe this is obvious and my limited experience using JDBC directly instead of Hibernate is throwing me. I'd like to not have to re-code the contents of CC_PROC in java business logic. Any ideas?
Thanks!
Aha, found the answer:
The oracle user was SALESOWN, so the fix was:
PreparedStatement preparedStatement = connection.prepareStatement(
"SELECT * FROM (TABLE ( SALESOWN.CC_PROC( ? , ? )))");
Yikes. I don't want to admit the amount of time it took to figure that out.
Apparently SQLPlus and NetBeans do attempt to help out a little...
Thanks for the help guys!

Prepared statement with wildcard not returning certain rows but query returns all rows?

I'm using JTDS as a driver to connect to SQL server.
Here's the query that's giving me problems:
SELECT EmpID,FirstName,LastName,CompanyName,DepartmentName,JobTitle,HireDate FROM Employees where UPPER(FirstName) LIKE 'KEVIN%'
It returns 2 rows on SQL Server. One that has 'KEVIN' in upper case and another that has 'Kevin' like so. I used the wildcard to make sure I get both results. In my EmployeeDAO class I'm using the following:
ps = con.prepareStatement("SELECT EmpID,FirstName,LastName,CompanyName,"
+ "DepartmentName,JobTitle,HireDate FROM Employees WHERE UPPER(FirstName) LIKE ?");
ps.setString(1, FirstName + "%");
rs = ps.executeQuery();
And then of course I put KEVIN on my main. It only returns ONE row, which is the 'Kevin' row.
How do I fix this so it returns all rows?
Your query looks fine (although I would uppercase the parameter value before setting it, to make it more robust). The problem is just in the way how you're collecting the rows from the ResultSet. Likely you're plain overriding the previous row with the next row so that you end up with only one row (the last one) in your collection.
Default collation of the SQL Server installation is SQL_Latin1_General_CP1_CI_AS and it is not case sensitive.
Change collation of the query:
SELECT Col1
FROM Table1
WHERE Col1 COLLATE Latin1_General_CS_AS LIKE 'KEVIN%'

desc command through code

Can we get the table description through java code that we get by typing desc in Toad?
Use DatabaseMetaData to get the table information.
You can use the getTablexxx() and getColumnxx() methods to get the table information.
Connection conn = DriverManager.getConnection(.....);
DatabaseMetaData dbmd = conn.getMetaData();
dbmd.getxxxx();
If you want to just get column names,types, precision etc you can use ResultSetMetaData. Here is an example.
If you want to go beyond this and find out all the constraints, indexes etc defined on the table you can query the corresponding data dictionary views.
select dbms_metadata.get_ddl('TABLE','YOUR_TABLE_NAME')
from dual;
It will show you column names,types as well as additional components for creating this table,such as TABLESPACE...;
DBMS_METADATA package or
Select * from all_tab_columns where owner=user and table_name='table_name' order by column_id

Categories