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
Related
I have been using UCanAccess to use Access databases my problem is when i want to delete a recor this returns automatically.
For example if i have:
Table Names
Id Name
1 Jessy
2 Abraham
String deleteQuery = "DELETE From Names where Id =?";
PreparedStatement pstm = con.getConnection().prepareStatement(deleteQuery);
pstm.setInt(1, 1); // "id" is type numeric
pstm.executeUpdate();
pstm.close();
it will works And then if i open the database the recor will be there!
that's my problem. (i hide the connection code but i have it)
Try to use compact feature provided by Access. On the Tools menu, point to Database Utilities, and then click Compact and Repair Database. This might help.
Do you do the commit after? If not and autocommit=false, just do it.
I found the problem, I was using data type OLE to save images simple sentences doesn't works so the way to delete a row with OLE field is creating Database and Table objects from java. It works.
JDBC supports the following:
ResultSet columns = connection.getMetaData().getColumns(null, schema, tableName, null);
This way I can retrieve information for all columns in the given table including the DATA_TYPE information. That information is of type int in java.
I used the following query to retrieve column name and data type but this returns the string representation of data type and I need int :
select column_name, data_type from all_tab_cols where table_name = 'tablename' and owner = 'owner';
How does the line of java code translate to a query in Oracle DB ?
How can I retrieve the int representation of a column ?
I would use a join but I don't know where to get the information from. Where does Oracle store these informations ?
For running SQL against a DB without using any ORM tools like hibernate, you can take a look at PreparedStatement class.
If you want to look at the "metadata" about the columns your PreparedStatement returned, you can take a look at the getMetaData() method, documentation can be found here and then ask the ResultSetMetaData about the column type: getColumnType(<0 based index of the column you want to find the type>) documentation can be found here
Regarding your question about how Java translates to Query in Oracle, it is too broad to be described in an answer. I would recommend looking for a tutorial on JDBC or the official tutorial.
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
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.
I am trying to build a dynamic sql query in java (shown below)
sqlStr = "Select * " +
"from " + tableName
if(tableName!=null){
if(tableName.equals("Table1"){
sqlStr = sqlStr.concat("order by city desc");
}else if(tableName.equals("Table2"){
sqlStr = sqlStr.concat("order by country desc");
}else if(tableName.equals("Table3"){
sqlStr = sqlStr.concat("order by price desc");
}
}
Now what i would like to do is to add a final 'else' statement which would order the query based on whether the table contains a column named 'custID'. There will be several tables with that column so i want to sort the ones that have that column by custID. (Rather than having hundreds of additional if statements for each table that does have that column name.) Is this possible? i have seen people using the 'decode' function but i cant work out how to use it here.
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();
Note: you forgot space in your code before ORDER BY clause.
If you are happy with hardcoding things, a way to avoid multiple conditionals would be to store a list of all the tables that include custID.
private final static String tablesWithCustID = "/TableX/TableY/TableZ/";
...
if (tablesWithCustID.contains( tableName )) {
sqlStr = sqlStr.concat("order by custID")
}
You could use a List instead of a simple delimited string if you like.
Perhaps better, you could store a map with table names as the key, and the sort string as the value. Load it up once, then you don't need any conditional at all.
The most straight-forward way to do it is to read the column definitions from USER_TAB_COLUMNS or ALL_TAB_COLUMNS and check for the existence of a custID column. Without crazy PL/SQL tricks, you won't be able to solve this in SQL alone.
BTW, there is a " " missing between tableName and the order by clauses.
I understand that you're looking for a solution that can do this in one query, i.e. without running a separate metadata query beforehand.
Unfortunately, this won't be possible. The decode function can do some dynamic things with column values, but not with column name. And you're looking for a solution dynamically derive the column name.
An alternative might be to just add ORDER BY 1, 2. This is an old syntax that means order by the first and than by the second column. It might be a good solution if the custID column is the first column. Otherwise it at least gives you some sorting.
How about ArrayList.contains()?
You can create a list of tables which have that column, and just check for tables.contains(tablename) in the final if condition.