import java.sql.*;
public class Login
{
public static void main(String args[]) throws SQLException
{
Connection con = null;
PreparedStatement stmt = null;
Statement st = null;
try {
Driver driver = new oracle.jdbc.driver.OracleDriver();
DriverManager.registerDriver(driver);
System.out.println("coneecting to the database:");
con = DriverManager.getConnection("driverURL","usrr","pass");
System.out.println("creating statement");
String sql = "Update abhi SET age = ? WHERE id = ?";
stmt = con.prepareStatement(sql);
stmt.setInt(1, 35);
stmt.setInt(2,102);
int rows = stmt.executeUpdate();
S.O.P("rows updated"+rows);
stmt.close();
String sql2;
sql2 = "select * from abhi";
st = con.createStatement();
ResultSet rs = st.executeQuery(sql2);
while(rs.next())
{
System.out.println("hi");
int age = rs.getInt("age");
System.out.println("age is"+age);
}
rs.close();
stmt.close();
con.close();
}
catch(SQLException e) {
e.printStackTrace();
}
catch (Exception e) {
e.printStackTrace();
}
finally {
try {
if (st != null)
st.close();
}
catch(Exception e) {
// nthing to do
}
try {
if(con != null)
con.close();
}
catch (Exception e) {
e.printStackTrace();
}
}
System.out.println("good bye johny");
}
}
這是我的代碼,它的prefectly乳寧沒有任何錯誤......但在我的數據庫的NT 更新我無法明白其中的道理就任何機構幫助我 與價值觀爲什麼它的NT更新的價值年齡其中id是102 ...如何有效地使用prepared語句?
我沒有看到提交。 (請儘量和格式化你的代碼,這真的很難跟隨你的貼吧。) – Mat 2013-03-10 10:40:28