|
public class BaseDao {
public static String url="jdbc:mysql://localhost/school?characterEncoding=utf-8";
public static String username="root";
public static String userpwd="1234";
public static String driver="com.mysql.jdbc.Driver";
static {
try {
Class.forName(driver);
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
}
public static Connection createConnection(){
Connection connection=null;
try {
connection= DriverManager.getConnection(url,username,userpwd);
} catch (SQLException e) {
e.printStackTrace();
}
return connection;
}
public static void closeAll(Connection connection, Statement statement, ResultSet resultSet){
try {
if (resultSet!=null) resultSet.close();
if (statement!=null) statement.close();
if (connection!=null) connection.close();
}catch (Exception e){
e.printStackTrace();
}
}
public static int executeupdate(String sql,Object ...objects){
Connection connection=null;
PreparedStatement statement=null;
ResultSet resultSet=null;
try {
connection = createConnection();
statement = connection.prepareStatement(sql);
for (int i=0;i<objects.length;i++){
statement.setObject(i+1,objects);
}
return statement.executeUpdate();
}catch (Exception e){
e.printStackTrace();
}finally {
closeAll(connection,statement,resultSet);
}
return 0;
}
}
|
|