也可以在这里查看地址:http://micely.net/sourceCodeDetail/204
下面是代码:
package db;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import org.apache.commons.dbutils.QueryRunner;
public class T {
public static Connection getConnection() throws SQLException,
InstantiationException, IllegalAccessException,
ClassNotFoundException {
String strDriver = "org.gjt.mm.mysql.Driver";
String strUrl = "jdbc:mysql://localhost:3306/test";
String strUser = "root";
String strPass = "abc";
Class.forName(strDriver).newInstance();
return DriverManager.getConnection(strUrl, strUser, strPass);
}
public static void main(String[] args) {
// String sql = "insert into tab(id,name)values(?,?)";
// Object params[] = { 5, "aaa" };
String sql = "update tab set name=? where id=?";
Object params[] = { "xxx", 5 };
QueryRunner runner = new QueryRunner();
Connection conn = null;
try {
conn = getConnection();
runner.update(conn, sql, params);
} catch (SQLException e) {
e.printStackTrace();
} catch (InstantiationException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
} finally {
if (conn != null) {
try {
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
}
}
mysql的表
create table tab(
id int(4),
name varchar(8)
)engine=innodb character set=utf8;