How to check if a table column exists using JdbcTemplate? - spring

I'm trying to write a query to check if a column exists within this table. As far as I can see, I can only query rows of columns. I'm wondering if there is a way to query if a column within a table exists?
String currentTable = "";
final Query query = dslContext
.select(field(COLUMN_COUNTRY_CODE))
.from(MANAGEMENT_TABLE_NAME)
.orderBy(field(COLUMN_CREATE_DATE).desc())
.limit(inline(1));
currentTable = jdbcTemplate.queryForObject(query.getSQL(), String.class);
This is what my query looks like at the moment. I want to check if COLUMN_COUNTRY_CODE column table exists in MANAGEMENT_TABLE_NAME. How would I go about doing this?

With JDBC, you can achieve this by creating an instance of DatabaseMetaData like so:
DatabaseMetaData databaseMetaData = connection.getMetaData();
Where your Connection object is an instance of JdbcConnection.
Next, by using the getColumns() method you can iterate over the columns of a particular table and check their names.
Code sample:
ResultSet columns = databaseMetaData.getColumns(null,null, "TABLE_NAME", null);
while(columns.next()) {
String columnName = columns.getString("COLUMN_NAME");
}
Source

Related

JPA Native query on Oracle database

I'm trying to do a native query on an Oracle database involve dates but I'm just not getting something.
I have a table with a number of rows in it and I know that the record with the oid=1234 has the latest updatetimeutc field, where updatetimeutc is a Date field. So I do the following:
Query query1 = entityManager.createNativeQuery("select updatetimeutc from TABLE where oid=1234");
List<Object[]> resultList = query1.getResultList();
Object[] os = resultList.get(0);
Date date = os[0];
Query query2 = entityManager.createNativeQuery("select oid, updatetimeutc from TABLE where updatetimeutc > :d");
query.setParameter("d", date);
resultList = query.getResultList();
At this point, I'm expecting the resultList.size() == 0, but that's not what's happening. I'm getting the same row returned to me as in query1.
Can someone explain why this is happening or what I'm doing wrong?
Thanks
The solution is to make sure you are using a version of Oracle's JDBC drivers that produce a java.sql.Timestamp for native queries that return DATE field.

How to query dataset table on primary key of smallInt using linq C#

Let say I have the following sql table. Customer_id column is a primary key with smallint.
And I am trying to create a linq query, where I want to get all data for customer with id, let say 1.
How to do this.
Already done and not working:
1.
var query = from row in dt.AsEnumerable()
where row.Field<Int32>("customer_id") == Convert.ToInt32(2)
select row;
2.
var query = from row in dt.AsEnumerable()
where row.Field<Int16>("customer_id") == Convert.ToInt16(2)
select row
debug for example 1,2
Syntax error
Exceptions
Why don't you use this:
DataRow needle = hayStack.Tables["Customer"].Rows.Find(2);
Your method should be rewritten as something like this:
private DataRow GetCustomerDetails(Int16 customer_id)
{
return _dt.Tables["Customer"].Rows.Find(customer_id);
}
The calling method would have to check for null beeing returned from the method, since an invalid customer_id would cause Find() tu return null.
Try using short type instead of Int32.

ASP.net LINQ on DataView to use Like query

I want to filter a DataView but DV.Rowfilter is taking too much time .
dv.RowFilter = "ProductName like '%" + searchtxt + "%'";
So I decided to use the LINQ but how to implement above like query in LINQ ?
LINQ is not more efficient in general, but it can increase readability and maintainability.
So you can try Linq-To-DataSet:
var query = from row in dv.Table.AsEnumerable()
let productName = row.Field<string>("ProductName")
where productName.Contains(searchtxt)
select row;
DataTable tbl = query.CopyToDataTable(); // use this as DataSource or use tbl.DefaultView
Here the same with method syntax:
var query = dv.Table.AsEnumerable()
.Where(row => row.Field<string>("ProductName").Contains(searchtxt));
MSDN: Creating a DataView Object with LINQ to DataSet
i have tried your second solution, but now its throwing the exception
"The source contains no DataRows." and actually the DataTable which i
make as DataTable.AsEnumerable() , it has the rows in it
The table contains rows but the filter skips all of them.
You could use if(query.Any()){...} to check if there are rows:
DataTable tbl = dv.Table.Clone(); // creates an empty table with the same columns
if(query.Any())
tbl = query.CopyToDataTable();

Parse.com linq relational query

Im using Xamarin.IOS and i want to run simple relations query using LINQ. I have to table. One table is NewSource other one is NewCategory. Two table relational with Name. For example :
NewSource table row:
Name: Radikal
Active: true
NewCategory table row:
NewSourceName: Radikal
Active:true
SportUrl: http://www.something.com
EconomyUrl= http://www.something.com
..
..
I wrote this query take from Parse document:
var query= from post in ParseObject.GetQuery("NewSource")
where (bool)post["Active"]==true //which mean i want to take only active New Source
select post;
var query2 = from comment in ParseObject.GetQuery("NewCategory")
join post in query on comment["NewSourcename"] equals post
select comment;
var comments = await query.FindAsync();
The code is not working. it returns always null. Where can i do wrong? I want to relational two table which connect is NewSource.Name and NewCategory.NewSourceName
How can i do this?
Thank you.
Assuming that NewSource table's Name column linked to NewSourceName column in NewCategory table, you can try to join them this way :
var query2 = from comment in ParseObject.GetQuery("NewCategory")
join post in query on (string)comment["NewSourcename"] equals (string)post["Name"]
select comment;

Adding a random Guid column to a Linq to Entities query to grab random records

I've found some articles about using the RandomView view and the GetNewID function to pull back randomized records, but they are using this method with Linq to SQL which allows Functions and Stored Procs to be used with no return value or a scalar return value. From what I understand, a Stored Proc has to been returned as one of the Entity Framework objects from my generated model. I have been able to get that to work as an object, but not returning a scalar or no return set.
What I want to do is to simply add a column to my Linq query that contains a newly generated Guid so that I can order by the new Guids and take a specific number of records randomly. Can anyone help with some sort of lambda expression or join that would enable me to do this? It seems like it should be something built into EF, but I understand that we are on EF v1.
(Please provide code in VB.net)
In the Select clause of your Linq query, you should be able to insert a GUID like this:
var result = from myRecord in myTable
select new {
field1 = myRecord.field1,
field2 = myRecord.field2,
guidField = Guid.NewGuid()
};
Well, my VB is a little rusty, but I think this will work...
Dim result =
From myRecord in myTable _
Select field1, _
field2, _
guidField = System.Guid.NewGuid()

Resources