当前位置: 技术问答>java相关
关于vj的一个问题,晚上就结算
来源: 互联网 发布时间:2015-01-11
本文导语: 在数据库连接好后,怎么给字段赋值。 最好能给我一个实例程序。包括数据库的查找,更新,删除,添加!谢谢! | 我们实习做过,你的email?我发给你 | Statement stmt; String sq...
在数据库连接好后,怎么给字段赋值。
最好能给我一个实例程序。包括数据库的查找,更新,删除,添加!谢谢!
最好能给我一个实例程序。包括数据库的查找,更新,删除,添加!谢谢!
|
我们实习做过,你的email?我发给你
|
Statement stmt;
String sql;
int rows;
sql = "INSERT INTO tCust "
+ "(custId, custName, custAddr) "
+ "VALUES "
+ "('" + custId + "',"
+ "('" + custName + "',"
+ "('" + custAddr + "')";
stmt = theConn.dbConn.createStatement();
rows = stmt.executeUpdate(sql);
theConn.dbConn.commit();
stmt.close();
A PreparedStatement is used to insert data containing QUOTES PreparedStatement stmt = null;
String sql;
int rows;
try {
sql = "INSERT INTO tCust"
+ "(custName) "
+ "VALUES "
+ "(?)";
stmt = theConn.dbConn.prepareStatement(sql);
stmt.setString(1, "Name with " are permitted!");
rows = stmt.executeUpdate();
theConn.dbConn.commit();
stmt.close();
System.out.println(sql);
}
catch (Exception e){
e,printStackTrace();
}
========================================
PreparedStatement prepstmt;
try {
prepstmt = theConn.dbConn.prepareStatement
("DELETE FROM tCust "+
" WHERE custId = ?");
prepstmt.setString(1,cust_id.getText());
prepstmt.executeUpdate();
theConn.dbConn.commit();
prepstmt.close();
}
catch (Exception e) {
e.printStackTrace();
}
String sql;
int rows;
sql = "INSERT INTO tCust "
+ "(custId, custName, custAddr) "
+ "VALUES "
+ "('" + custId + "',"
+ "('" + custName + "',"
+ "('" + custAddr + "')";
stmt = theConn.dbConn.createStatement();
rows = stmt.executeUpdate(sql);
theConn.dbConn.commit();
stmt.close();
A PreparedStatement is used to insert data containing QUOTES PreparedStatement stmt = null;
String sql;
int rows;
try {
sql = "INSERT INTO tCust"
+ "(custName) "
+ "VALUES "
+ "(?)";
stmt = theConn.dbConn.prepareStatement(sql);
stmt.setString(1, "Name with " are permitted!");
rows = stmt.executeUpdate();
theConn.dbConn.commit();
stmt.close();
System.out.println(sql);
}
catch (Exception e){
e,printStackTrace();
}
========================================
PreparedStatement prepstmt;
try {
prepstmt = theConn.dbConn.prepareStatement
("DELETE FROM tCust "+
" WHERE custId = ?");
prepstmt.setString(1,cust_id.getText());
prepstmt.executeUpdate();
theConn.dbConn.commit();
prepstmt.close();
}
catch (Exception e) {
e.printStackTrace();
}
|
建议看一看 SQL 语句。
如果用一般的查询,直接用
Connection conn = ....;
Statement stmt = conn.creatStatement();
String sql = "update into ... set ... ";
stmt.excuteUpdate(sql);
就可以了。
简单的查询,一般不要用到事物处理和 perpareStatement(楼上的),因为事物处理和 perpareStatement 都需要额外的开销。
如果用一般的查询,直接用
Connection conn = ....;
Statement stmt = conn.creatStatement();
String sql = "update into ... set ... ";
stmt.excuteUpdate(sql);
就可以了。
简单的查询,一般不要用到事物处理和 perpareStatement(楼上的),因为事物处理和 perpareStatement 都需要额外的开销。