ID for automatically growing after inserting records in the JdbcTemplate interface class in SPRINGMVC

Source: Internet
Author: User

Package user.jdbctemplate;

Import java.sql.Connection;
Import java.sql.PreparedStatement;
Import java.sql.SQLException;
Import Java.util.Iterator;
Import java.util.List;

Import Javax.sql.DataSource;

Import org.springframework.dao.DataAccessException;
Import Org.springframework.jdbc.core.JdbcTemplate;
Import Org.springframework.jdbc.core.PreparedStatementCreator;
Import Org.springframework.jdbc.support.GeneratedKeyHolder;
Import Org.springframework.jdbc.support.KeyHolder;

Interface classes for manipulating users
public class Usertemplate {
Private JdbcTemplate JdbcTemplate;

public void Setdatasource (DataSource DataSource) {
JdbcTemplate = new JdbcTemplate (DataSource);
}

public int getallcount (String sql) {
SYSTEM.OUT.PRINTLN (SQL);
int count = 1;
try {
Count = Jdbctemplate.queryforint (SQL);
catch (DataAccessException e) {
System.out.println ("Getallcount inside the error is:" + e);
return count;
}
return count;
}

public string saveorupdate (String sql) {
SYSTEM.OUT.PRINTLN (SQL);
try {
Jdbctemplate.update (SQL);

catch (DataAccessException e) {
System.out.println (e);
return "Returnfail";
}
return "RETURNSUC";
}

 /*
  * 1.KeyHolder keyholder = new generatedKeyHolder ();
  * 2.jdbctemplate.update (new PreparedStatementCreator () {3. Public
  * PreparedStatement Createpreparedstatement (Connection Connection) throws
  * SQLException {4. PreparedStatement PS = connection.preparestatement (5.
  * "INSERT into Test (ID, NAME) VALUES (Phoenix_sequence.nextval,?)", New
  * string[] {"id"}); 6. Ps.setstring (1, "Heipark"); 7. return PS; 8.
  * 9.}, Keyholder); 10.Long Generatedid = Keyholder.getkey (). Longvalue ();
  * 11.SYSTEM.OUT.PRINTLN (Generatedid);
 /*

/*
* Final String sql =
* "INSERT into Tag_info (site_id,name,content) VALUES (?,?, '" +content+ ")";
* Keyholder keyholder = new generatedKeyHolder (); Getjdbctemplate (). Update (
* New PreparedStatementCreator () {public PreparedStatement
* Createpreparedstatement (Connection con) throws SQLException {
* PreparedStatement PS = getjdbctemplate (). Getdatasource ()
*. getconnection (). Preparestatement (sql,new string[]{"site_id", "NAME"});
* Ps.setstring (1, "site number"); Ps.setstring (2, "my Name"); return PS; } },
* Keyholder); System.out.println ("Insert id============================ automatically" +
* Keyholder.getkey (). Intvalue ()); Return Keyholder.getkey (). Intvalue ();
*/

public int saveproject (String sql) {
Final String innersql = SQL;
Keyholder keyholder = new generatedKeyHolder ();

System.out.println (Innersql);

/*
* SYSTEM.OUT.PRINTLN ("Insert scheme record, get current ProjectID"); Int
* Projectid=jdbctemplate.update (SQL);
*/
try {
Jdbctemplate.update (New PreparedStatementCreator () {
Public PreparedStatement createpreparedstatement (Connection con)
Throws SQLException {
PreparedStatement PS = Jdbctemplate.getdatasource ()
. getconnection (). Preparestatement (Innersql);
return PS;
}
}, Keyholder);
catch (DataAccessException e) {

E.printstacktrace ();
}
SYSTEM.OUT.PRINTLN (insert id============================ automatically)
+ Keyholder.getkey (). Intvalue ());
Return Keyholder.getkey (). Intvalue ();

}

Public List findall (String sql) {
List rows;
try {
rows = jdbctemplate.queryforlist (SQL);

catch (DataAccessException e) {
return null;
}

Iterator it = Rows.iterator ();
if (It.hasnext ()) {

return rows;
}

return null;
}

Public Object Findusermoney (String sql) {
List rows;
Object Money = "0.00";
try {
rows = jdbctemplate.queryforlist (SQL);
catch (DataAccessException e) {
return null;
}

Iterator it = Rows.iterator ();
if (It.hasnext ()) {
Object o = rows.get (0);
System.out.println ("User's balance is:" + money);

return money;
}

return null;
}

public string Findexistuser (String sql) {
List rows;
String returnstr = "RETURNSUC";
try {
rows = jdbctemplate.queryforlist (SQL);
catch (DataAccessException e) {
return "fail";
}

Iterator it = Rows.iterator ();
if (It.hasnext ()) {

return returnstr;
}

return "fail";

}

public string Findexistusername (String sql) {

List rows;
String returnstr = "RETURNSUC";
try {
rows = jdbctemplate.queryforlist (SQL);
catch (DataAccessException e) {
return "fail";
}

Iterator it = Rows.iterator ();
if (false = = It.hasnext ()) {

return returnstr;
}

return "fail";
}

Public list<double> findusermoneybyname (String sql) {
List rows;
try {
rows = jdbctemplate.queryforlist (SQL);
catch (DataAccessException e) {
return null;
}

Iterator it = Rows.iterator ();
if (It.hasnext ()) {

return rows;
}

return null;
}

}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.