用select语句查询一次并按ID排序,取最后那条记录的值,你上面语句里面根本没有ID字段,它当然找不到ID字段了。
解决方案 »
- config.properties文件的作用是什么
- 单点登录
- 用ibatis连的数据库报SQL command not properly ended的错是什么原因?求牛人解决!!!!万分感谢!!
- ssh启动提示严重错误,信息如下,急!急!
- 高手,来帮忙看下,spring事务配置的问题
- java中对xml的操作
- java 变量
- 请问大家,一个简单的XML问题
- 如何配置jboss啊~!
- 线程是Runnable,偶尔 java.lang.ClassLoader.defineClass1(Native Method)会卡住
- 敢在china-pub瞎买书了,请大虾们推荐以后再买,介绍XML方面的书,我准备开始学习XML。
- 请问如何一个节点里增加新属性
另外我发现即使取得是rs.getString("CompanyID")字段,也返回类似错误~~~~~
是不是getString这个方法只能用于String Sql 是“select * from vaaa”类型得数据库操作得啊?
实际上这类操作是应该用 executeUpdate(),它返回的是整型。你可以用它来判断操作是否成功。
还是先插入,让数据库自动分配id,插入成功后再取id好呢??
如下:
String Sql = "insert into company_modulelink (CompanyID,ModuleID,IsEnabled ) values ('1','1','1')";
int i = stat.executeUpdate(sql);
if(i>0){}ResultSet rs=stat.executeQuery(Sql);
this.company_id = rs.getString("ID")
插入记录是不用插入id的值因为它是自动分配的。
如下:
String Sql = "insert into company_modulelink (CompanyID,ModuleID,IsEnabled ) values ('1','1','1')";
int i = stat.executeUpdate(sql);
if(i>0){ sql = "select id from company_modulelink order by id desc";
ResultSet rs=stat.executeQuery(Sql);
rs.first();
this.company_id = rs.getString("ID")
}
java.lang.AbstractMethodError: org.gjt.mm.mysql.ResultSet.first()Z
at company.AddCompanyInfo.AddCompanyInfoFunction(AddCompanyInfo.java:64)
at _manage._company._update__jsp._jspService(/agehouse/manage/company/update.jsp:35)
at com.caucho.jsp.JavaPage.service(JavaPage.java:75)
at com.caucho.jsp.Page.subservice(Page.java:497)
at com.caucho.server.http.FilterChainPage.doFilter(FilterChainPage.java:182)
at com.caucho.server.http.Invocation.service(Invocation.java:312)
at com.caucho.server.http.CacheInvocation.service(CacheInvocation.java:135)
at com.caucho.server.http.HttpRequest.handleRequest(HttpRequest.java:244)
at com.caucho.server.http.HttpRequest.handleConnection(HttpRequest.java:163)
at com.caucho.server.TcpConnection.run(TcpConnection.java:137)
at java.lang.Thread.run(Thread.java:536)什么问题?为什么加入rs.next();就不报这个错误??而加rs.first()或rs.last();就报错?
rs.first()或rs.last();主要应用于回滚式的结果集合更新操作过程中,如果没有设置相关结果集合属性,当然要报错了。
具体可以查看API帮助上的结果集合属性说明
1.只有JDK1.4可以表示是否从插入中返回自动产生的值,但和具体数据库有关系,不易跨平台。
2.可以用时间(System.currenttime)等做主键,由程序生成(重复的可能性很小)。
第二个方法是Jive中的方法,看下面的代码
/**
* $RCSfile: SequenceManager.java,v $
* $Revision: 1.1.1.1 $
* $Date: 2002/09/09 13:51:02 $
*
* New Jive from Jdon.com.
*
* This software is the proprietary information of CoolServlets, Inc.
* Use is subject to license terms.
*/package com.jivesoftware.forum.database;import java.sql.*;
import com.jivesoftware.util.StringUtils;/**
* Manages sequences of unique ID's that get stored in the database. Database
* support for sequences varies widely; some don't support them at all. So,
* we handle unique ID generation with a combination VM/database solution.<p>
*
* A special table in the database doles out blocks of unique ID's to each
* virtual machine that interacts with Jive. This has the following consequences:
* <ul>
* <li>There is no need to go to the database every time we want a new unique
* id.
* <li>Multiple app servers can interact with the same db without consequences
* in terms of id's.
* <li>The order of unique id's may not correspond to creation date
* (as they once did).
* </ul>
*/
public class SequenceManager { private static final String LOAD_ID =
"SELECT id FROM jiveID WHERE idType=?";
private static final String UPDATE_ID =
"UPDATE jiveID SET id=? WHERE idType=? AND id=?"; /**
* The number of ID's to checkout at a time. 15 should provide a good
* balance between speed and not wasing too many ID's on appserver restarts.
* Feel free to change this number if you believe your Jive setup warrants
* it.
*/
private static final int INCREMENT = 15; // Statically startup a sequence manager for each of the five sequence
// counters.
private static SequenceManager[] managers;
static {
managers = new SequenceManager[5];
for (int i=0; i<managers.length; i++) {
managers[i] = new SequenceManager(i);
}
} /**
* Returns the next ID of the specified type.
*
* @param type the type of unique ID.
* @return the next unique ID of the specified type.
*/
public static long nextID(int type) {
return managers[type].nextUniqueID();
} public static long nextID() {
return managers[0].nextUniqueID();
} private int type;
private long currentID;
private long maxID; /**
* Creates a new DbSequenceManager.
*/
public SequenceManager(int type) {
this.type = type;
currentID = 0l;
maxID = 0l;
} /**
* Returns the next available unique ID. Essentially this provides for the
* functionality of an auto-increment database field.
*/
public synchronized long nextUniqueID() {
if (! (currentID < maxID)) {
// Get next block -- make 5 attempts at maximum.
getNextBlock(5);
}
long id = currentID;
currentID++;
return id;
} /**
* Performs a lookup to get the next availabe ID block. The algorithm is as
* follows:<ol>
* <li> Select currentID from appropriate db row.
* <li> Increment id returned from db.
* <li> Update db row with new id where id=old_id.
* <li> If update fails another process checked out the block first; go
* back to step 1. Otherwise, done.
* </ol>
*/
private void getNextBlock(int count) {
if (count == 0) {
System.err.println("Failed at last attempt to obtain an ID, aborting...");
return;
}
boolean success = false;
Connection con = null;
PreparedStatement pstmt = null;
try {
con = ConnectionManager.getConnection();
// Get the current ID from the database.
pstmt = con.prepareStatement(LOAD_ID);
pstmt.setInt(1, type);
ResultSet rs = pstmt.executeQuery();
if (!rs.next()) {
throw new SQLException("Loading the current ID failed. The " +
"jiveID table may not be correctly populated.");
}
long currentID = rs.getLong(1);
pstmt.close();
// Increment the id to define our block.
long newID = currentID + INCREMENT;
// The WHERE clause includes the last value of the id. This ensures
// that an update will occur only if nobody else has performed an
// update first.
pstmt = con.prepareStatement(UPDATE_ID);
pstmt.setLong(1, newID);
pstmt.setInt(2, type);
pstmt.setLong(3, currentID);
// Check to see if the row was affected. If not, some other process
// already changed the original id that we read. Therefore, this
// round failed and we'll have to try again.
success = pstmt.executeUpdate() == 1;
if (success) {
this.currentID = currentID;
this.maxID = newID;
}
}
catch( Exception sqle ) {
sqle.printStackTrace();
}
finally {
try { pstmt.close(); }
catch (Exception e) { e.printStackTrace(); }
try { con.close(); }
catch (Exception e) { e.printStackTrace(); }
}
if (!success) {
System.err.println("WARNING: failed to obtain next ID block due to " +
"thread contention. Trying again...");
// Call this method again, but sleep briefly to try to avoid thread
// contention.
try {
Thread.currentThread().sleep(75);
} catch (InterruptedException ie) { }
getNextBlock(count-1);
}
}
}这是用了一个多态的方法,来解决取ID的问题,很不错!