I’m trying to make an auto increment function that each time user create table Departmenent_id(varchar) is incremented and the id i have given A1,A2….. so what i do in my autoincrement() that i use the list of ,result getting from select Dept_id from Department and try to separate the integer part from this and adding to TreeSet collection and taking out largest element and adding to first part of String but problem i face when i take out the substring of string like 9 and parsing it into integer i m getting number format exception. Why am I getting this problem my function code is right here
private String autoIncreament() {
String id = null;
String num = null;
String result = "SELECT DEPARTMENT_ID FROM Department";
TreeSet<Integer> treeSet = new TreeSet<Integer>();
List<Map<String, Object>> result1 = getJdbcTemplate().queryForList(
result);
for (Map<String, Object> map : result1) {
id = (String) map.get("DEPARTMENT_ID");
System.out.println("id=" + id);
num = id.substring(1);
int number = Integer.parseInt(num);
treeSet.add(number);
}
Integer in = treeSet.pollLast();
int c=in.intValue();
c++;
id = id +c ;
/* System.out.println("jcak="+rowSet.getRow()); */
return id;
}
my exception details
19 Dec, 2012 11:25:14 AM org.springframework.context.support.AbstractApplicationContext prepareRefresh
INFO: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@5d764be1: startup date [Wed Dec 19 11:25:14 IST 2012]; root of context hierarchy
19 Dec, 2012 11:25:14 AM org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
INFO: Loading XML bean definitions from class path resource [mybeans.xml]
19 Dec, 2012 11:25:14 AM org.springframework.beans.factory.support.DefaultListableBeanFactory preInstantiateSingletons
INFO: Pre-instantiating singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory@7b5a6029: defining beans [dataSource,employeeDaoImpl,departmentdao]; root of factory hierarchy
19 Dec, 2012 11:25:14 AM org.springframework.jdbc.datasource.DriverManagerDataSource setDriverClassName
INFO: Loaded JDBC driver: com.mysql.jdbc.Driver
ankur=com.nousinfo.tutorial.employee.model.Department@7eb1cc87
id=9
Exception in thread "main" java.lang.NumberFormatException: For input string: ""
at java.lang.NumberFormatException.forInputString(Unknown Source)
at java.lang.Integer.parseInt(Unknown Source)
at java.lang.Integer.parseInt(Unknown Source)
at com.nousinfo.tutorial.employee.dao.impl.DepartmentDAOImpl.autoIncreament(DepartmentDAOImpl.java:113)
at com.nousinfo.tutorial.employee.dao.impl.DepartmentDAOImpl.getDepartment(DepartmentDAOImpl.java:47)
at AccessClass.main(AccessClass.java:34)
If you are using normal jdbc, java has provided the support to read the key.
Hope this will help.