此代码适用于简单的应用程序,因此驱动程序很好。那么为什么连接对象无法使用驱动程序进行初始化。
导入java.sql.Connection;
导入 java.sql.DriverManager;
导入java.sql.SQLException;
导入 java.sql.Statement;
导入 com.opensymphony.xwork2.ActionSupport;
公共类插入扩展 ActionSupport {
public String execute() throws Exception, SQLException {
String sql = "";
String url = "jdbc:mysql://localhost:3306/test";
//String dbName = "test";
String driverName = "org.gjt.mm.mysql.Driver";
String userName = "root";
String password = "root";
Connection con=null;
Statement stmt=null;
try {
Class.forName(driverName).newInstance();
con = DriverManager.getConnection(url, userName, password);
stmt = con.createStatement();
} catch (Exception e) {
System.out.println(e.getMessage());
}
}