连接MySQL并执行Update
2007-12-23 12:35:26 来源:WEB开发网核心提示:/*Database PRogramming with JDBC and java, Second EditionBy George ReeseISBN: 1-56592-616-1Publisher: O'Reilly*/import java.sql.Connection;import java.sql.D
/*
Database PRogramming with JDBC and java, Second Edition
By George Reese
ISBN: 1-56592-616-1
Publisher: O'Reilly
*/
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;
/**
* Example 3.3
*/
public class Update {
public static void main(String args[]) {
Connection con = null;
if (args.length != 2) {
System.out.println("Syntax: <java UpdateApp [number] [string]>");
return;
}
try {
String driver = "com.imaginary.sql.msql.MsqlDriver"; Class.forName(driver).newInstance();
String url = "jdbc:msql://carthage.imaginary.com/ora";
con = DriverManager.getConnection(url, "borg", "");
Statement s = con.createStatement();
String test_id = args[0];
String test_val = args[1];
int update_count = s
.executeUpdate("INSERT INTO test (test_id, test_val) "
+ "VALUES(" + test_id + ", '" + test_val + "')");
System.out.println(update_count + " rows inserted.");
s.close();
} catch (Exception e) {
e.printStackTrace();
} finally {
if (con != null) {
try {
con.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
}
}
Database PRogramming with JDBC and java, Second Edition
By George Reese
ISBN: 1-56592-616-1
Publisher: O'Reilly
*/
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;
/**
* Example 3.3
*/
public class Update {
public static void main(String args[]) {
Connection con = null;
if (args.length != 2) {
System.out.println("Syntax: <java UpdateApp [number] [string]>");
return;
}
try {
String driver = "com.imaginary.sql.msql.MsqlDriver"; Class.forName(driver).newInstance();
String url = "jdbc:msql://carthage.imaginary.com/ora";
con = DriverManager.getConnection(url, "borg", "");
Statement s = con.createStatement();
String test_id = args[0];
String test_val = args[1];
int update_count = s
.executeUpdate("INSERT INTO test (test_id, test_val) "
+ "VALUES(" + test_id + ", '" + test_val + "')");
System.out.println(update_count + " rows inserted.");
s.close();
} catch (Exception e) {
e.printStackTrace();
} finally {
if (con != null) {
try {
con.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
}
}
(出处:http://www.cncms.com)
更多精彩
赞助商链接