how to update only few columns of a table in spring jdbc template - spring

i am using spring jdbc template and i want to update some of the columns in a table.
Ex:Consider Student table
id-->1007.
name-->Krish.
age-->25.
tel-->0112538956536.
this is a existing record.i want to update some fields only(updating fields change time to time).others should have their existing values.how can i acheive this in Spring JDBC template.Any suggestions will be very much helpful.
Thanks.

you can use jdbc template for update table from application
here is the simple example
String name = "asdads";
int age = 12;
String tel = "+905655465465";
int id = 1;
String SQL = "update Student set name = ?, age= ? ,tel=?, last_updated = sysdate() where id = ?";
jdbcTemplate.update(SQL,name, age, tel, id);

may be just like
jdbcTemplate.update(
"update Student set last_updated=now(),some_field=?",
"some value");

Related

How to check if a table column exists using JdbcTemplate?

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

Inserting the Date from JSpinner Component into Oracle DB in the correct format

So I've been trying to insert the date from a JSpinner component into an Oracle DB.
The date format of a sample entry in the Oracle table is like "2018-01-01 09:30:00.0" (image for reference):
I want to save the value obtained from the JSpinner in the same format as otherwise, I'm getting a Unparseable date exception.
For reference this is the date I enter in the spinner :
and this is the error that shows up :
This is the code I'm using to try and insert it into the Oracle DB:
TimeReserved = jSpinner1.getValue().toString();
String Query = "Insert into RoomTable Values( ?, ?, ?)";
PreparedStatement stmt = conn.prepareStatement(Query);
//setting Date
DateFormat formatter = new SimpleDateFormat("yyyy-MM-dd hh-mm-ss a" );
Date fd = formatter.parse(TimeReserved);
java.sql.Date sqlDate = new java.sql.Date(fd.getTime());
stmt.setDate(2, sqlDate);
Additionally, I've set the editor property up like this :
Thank you guys for your help in advance :)

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.

entity framework lazy loading null properties in child

Using entity framwork with lazy loading - Have the following question on loading related entities when the entities
are null.
Say I have two tables employee and employeedetails. Assume in the above case not all employee entries have an entry in the employeedetails table.
If I want to look up a list of Employees
(from e in objectcontext.employees
select new EmployeeEntity
{
EmpID= e.EmployeeID,
FirstName = e.FirstName,
Address = e.employeedetails.Address
}).ToList();
EmployeeEntity is the data class into which we stuff the results.
The above code breaks if even one employee in the returned list
does not have a entry in table employeedetails. This is obvious since e.employeedetails will be null for those customers who do not have a details entry
What is the best way to rewrite the above query?
Would something like this be acceptable ?
(from e in objectcontext.employees
select new EmployeeEntity
{
EmpID= e.EmployeeID,
FirstName = e.FirstName,
Address = e.employeedetails == null ? "" : e.employeedetails.Address,
}).ToList();
I am not clear on the efficiency of this above query - Would this statment do the null check at DB level?
Should I instead do an explicit include like
objectcontext.include("employeedetails")...
And then loop through the results to check for null?
Yes, this statement would indeed perform a null check in the SQL query that is generated. Most likely, it will simply be a NVL or COALESCE.
That's the way you should be doing it.

NHibernate, updating a db column to a function call when persisting a domain object

I have a domain class that when persisted to Oracle must update a column to sysdate. NHibernate must only generate 1 SQL. e.g.
update person set age = 12, stamp = sysdate where id = 1;
Can this be done?
EDITED:
Could be something like:
Person person = (Person)session.Get(typeof(Person), 1);
session.SetFunction(person, "stamp", Functions.CurrentTimestamp);
person.Age = 12;
session.Flush();
You could update the stamp via a database trigger:
create trigger person_trg
before update
for each row
begin
:new.stamp := sysdate;
end;
Then all hibernate would need to do is "persist" the change to age (for example).
If you want to do that on demand, you can just execute the query above:
session.CreateQuery("update person set age = 12, stamp = sysdate where id = 1")
.ExecuteUpdate();
Interestingly, that is both valid HQL and SQL.

Resources