mysql resultset用法_怎么使用ResultSet.insertRow()向mysql插入数据???

java的api文档下java.sql.ResultSet,有一段

rs.moveToInsertRow(); // moves cursor to the insert row

rs.updateString(1, "AINSWORTH"); // updates the

// first column of the insert row to be AINSWORTH

rs.updateInt(2,35); // updates the second column to be 35

rs.updateBoolean(3, true); // updates the third row to true

rs.insertRow();

rs.moveToCurrentRow();

从上边看可以向数据库插入一行新的数据

使用

private  String dbdriver = "org.gjt.mm.mysql.Driver";

private  String dbcc = "jdbc:mysql://localhost/test";

private String dbdriver = "sun.jdbc.odbc.JdbcOdbcDriver";

private String dbcc = "jdbc:odbc:mysql";

都尝试过,可是,不能编译成功

可能

Connection  conn = DriverManager.getConnection(this.dbcc,this.dbuser,this.dbpasswd);

Statement s = conn.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_UPDATABLE);

ResultSet rs = s.getResultSet();

的问题???

使用s的什么方法????

谁能给个具体的实现代码???

谢谢了

|

给你几个方法:

/**

* insert a Row into Orders table using Updateable ResultSet.

*/

public void insertNewRowIntoOrdersTableUsingColumnNames(ResultSet rs)

throws SQLException {

// this method works only if the table is Orders Table in the Demo

// database.

// if the table is different you should change most of this code

// for that purpose.

rs.moveToInsertRow();

rs.updateInt("CUSTOMER_ID_FK", 101);

rs.updateInt("ORDER_ID", 1001);

rs.updateDate("ORDER_DATE",  new Date(System.currentTimeMillis()) );

rs.updateString("SHIP_INSTRUCT",

" deliver at Apt. office" + " if no one at home");

rs.updateString("BACKLOG", "N");

rs.updateString("PO_NUM", "BSD100");

rs.updateDate("SHIP_DATE", new Date(System.currentTimeMillis()));

rs.updateFloat("SHIP_WEIGHT", 22.0f);

rs.updateFloat("SHIP_CHARGE", 44.56f);

rs.updateDate("PAID_DATE", new Date(System.currentTimeMillis()));

rs.insertRow();

}

/**

* insert a Row into Orders table using Updateable ResultSet

*/

public void insertNewRowIntoOrdersTableUsingColumnIndexes(ResultSet rs)

throws SQLException {

rs.moveToInsertRow();

rs.updateInt(1, 101);

rs.updateInt(2, 1001);

rs.updateDate(3, new Date(System.currentTimeMillis()));

rs.updateString(4, " deliver at Apt. office if no one at home");

rs.updateString(5, "N");

rs.updateString(5, "BSD100");

rs.updateDate(6, new Date(System.currentTimeMillis()));

rs.updateFloat(7, 22.0f);

rs.updateFloat(8, 44.56f);

rs.updateDate(9, new Date(System.currentTimeMillis()) );

rs.insertRow();

}

/**

* This method deletes nth Row from the result Set

*/

public void deleteNthRow(ResultSet rs,

int rowNum) throws SQLException {

rs.absolute(rowNum);

rs.deleteRow();

}

/**

* this methods updates a value for the nthRow , ith Column .

*/

public void updateNthRowIthColumnValue(ResultSet rs, int nThRow,

int iThColumn,

Object value) throws SQLException {

ResultSetMetaData rMetaData = rs.getMetaData();

int type = rMetaData.getColumnType(iThColumn);

String colName = rMetaData.getColumnName(iThColumn);

rs.absolute(nThRow);

switch (type) {

case Types.CHAR:

case Types.VARCHAR:

case Types.LONGVARCHAR:

rs.updateString(colName, value.toString());

break;

case Types.BIT:

rs.updateByte(colName,

Byte.valueOf(value.toString()).byteValue());

break;

case Types.TINYINT:

case Types.SMALLINT:

case Types.INTEGER:

rs.updateInt(colName,

Integer.valueOf(value.toString()).intValue());

break;

case Types.BIGINT:

rs.updateLong(colName,

Long.valueOf(value.toString()).longValue());

break;

case Types.FLOAT:

rs.updateFloat(colName,

Float.valueOf(value.toString()).floatValue());

break;

case Types.DOUBLE:

rs.updateDouble(colName,

Double.valueOf(value.toString()).doubleValue());

break;

case Types.DATE:

rs.updateDate(colName, Date.valueOf(value.toString()));

break;

case Types.TIME:

rs.updateTime(colName, Time.valueOf(value.toString()));

break;

case Types.TIMESTAMP:

rs.updateTimestamp(colName, Timestamp.valueOf(value.toString()));

break;

default:

rs.updateObject(colName, value);

break;

}

rs.updateRow();

}

/**

* cancel updates in a ResultSet

*/

public void cancelUpdatesInResultSet(ResultSet rs)

throws SQLException {

rs.cancelRowUpdates();

}

/**

* print ResultSet in the Forward Direction from current position;

*/

public void printResultSetRowsInForwardDirection(ResultSet rs)

throws SQLException {

ResultSetMetaData rMetaData = rs.getMetaData();

int nColumns = rMetaData.getColumnCount();

// print metadata

for (int i = 1; i

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值