当前位置: 技术问答>java相关
怎么使用ResultSet.insertRow()向mysql插入数据???
来源: 互联网 发布时间:2015-07-29
本文导语: 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...
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的什么方法????
谁能给个具体的实现代码???
谢谢了
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
/**
* 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