public class DBHelper {
/* 定义driver,url,user,pwd等 */
private static String driver = "com.mysql.jdbc.Driver";
private static String url = "jdbc:mysql://127.0.0.1:3306/shiepsns";
private static String user = "root", pwd = "20091691";
private static Connection con;
/* 加载驱动程序 */
static {
try {
Class.forName(driver);
} catch (Exception ex) {
}
}
/* 执行增删改操作 */
public static int executeUpdate(String sql) {
int r=0;
try {
con = DriverManager.getConnection(url, user, pwd);
Statement cmd = con.createStatement();
r=cmd.executeUpdate(sql);
con.close();
} catch (Exception ex) {
ex.printStackTrace();
}
return r;
}
/* 执行查询操作 */
public static ResultSet executeQuery(String sql) {
ResultSet rs = null;
try {
con = DriverManager.getConnection(url, user, pwd);
Statement cmd = con.createStatement();
rs = cmd.executeQuery(sql);
} catch (Exception ex) {
}
return rs;
}
/* 执行关闭数据库连接操作 */
public static void closeConnection() {
try {
if (!con.isClosed())
con.close();
} catch (Exception ex) {
}
}
}
/* 定义driver,url,user,pwd等 */
private static String driver = "com.mysql.jdbc.Driver";
private static String url = "jdbc:mysql://127.0.0.1:3306/shiepsns";
private static String user = "root", pwd = "20091691";
private static Connection con;
/* 加载驱动程序 */
static {
try {
Class.forName(driver);
} catch (Exception ex) {
}
}
/* 执行增删改操作 */
public static int executeUpdate(String sql) {
int r=0;
try {
con = DriverManager.getConnection(url, user, pwd);
Statement cmd = con.createStatement();
r=cmd.executeUpdate(sql);
con.close();
} catch (Exception ex) {
ex.printStackTrace();
}
return r;
}
/* 执行查询操作 */
public static ResultSet executeQuery(String sql) {
ResultSet rs = null;
try {
con = DriverManager.getConnection(url, user, pwd);
Statement cmd = con.createStatement();
rs = cmd.executeQuery(sql);
} catch (Exception ex) {
}
return rs;
}
/* 执行关闭数据库连接操作 */
public static void closeConnection() {
try {
if (!con.isClosed())
con.close();
} catch (Exception ex) {
}
}
}