Класс для обращения к базе данных:
public class StudentDao extends Dao<Student> {
ResultSet resultSet;
Connection connection;
PreparedStatement select;
PreparedStatement delete;
PreparedStatement update;
ConnectionFactory connectionFactory;
PreparedStatement insert;
public StudentDao() {
try {
Class.forName("com.mysql.jdbc.Driver");
if(connection==null) {
connectionFactory = new ConnectionFactory();
connection = connectionFactory.getConnection();
}}
catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (MyException e) {
e.printStackTrace();
}
}
private PreparedStatement getInsert(){
if(insert==null){
try {
insert= connection.prepareStatement( "insert into student (first_name,second_name) values (?,?);");
} catch (SQLException e) {
e.printStackTrace();
}
}
return insert;
}
private PreparedStatement getSelect(){
if(select==null){
try {
select=connection.prepareStatement("select id, first_name, second_name from student;");
} catch (SQLException e) {
e.printStackTrace();
}
}
return select;
}
private PreparedStatement getUpdate(){
if(update==null){
try {
update=connection.prepareStatement( "Update student set first_name=?,second_name=? where id=?;");
} catch (SQLException e) {
e.printStackTrace();
}
}
return update;
}
private PreparedStatement getDelete(){
if(delete==null){
try {
delete=connection.prepareStatement("delete from student where id=?;");
} catch (SQLException e) {
e.printStackTrace();
}
}
return delete;
}
public void insert(Student obj) {
try {
getInsert();
insert.setString(1, obj.getFirst_name());
insert.setString(2, obj.getSecond_name());
insert.executeUpdate();
} catch (SQLException e) {
e.printStackTrace();
}
}
@Override
public List<Student> select() {
List<Student> students=new LinkedList<>();
Student student=null;
try {
getSelect();
resultSet=select.executeQuery();
while(resultSet.next()){
student=new Student();
student.setId(resultSet.getInt("id"));
student.setFirst_name(resultSet.getString("first_name"));
student.setSecond_name(resultSet.getString("second_name"));
students.add(student);
}
} catch (SQLException e) {
e.printStackTrace();
}finally {
if(resultSet!=null){
try {
resultSet.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
for(Student text:students)
System.out.print(text);
return students;
}
@Override
public void delete(Student obj, int id) {
try {
getDelete();
delete.setInt(1,id);
delete.executeUpdate();
} catch (SQLException e) {
e.printStackTrace();
}
}
@Override
public void update(Student obj) {
try {
getUpdate();
update.setString(1, obj.getFirst_name());
update.setString(2, obj.getSecond_name());
update.setInt(3, obj.getId());
update.executeUpdate();
} catch (SQLException e) {
e.printStackTrace();
}
}
public void closeConnection() {
try {
if (connection != null) {
connection.close();
}
} catch (Exception e) {
e.printStackTrace();
}
try{
if (insert!=null ){
insert.close();
}
}catch (SQLException e){
e.printStackTrace();
}
try{
if (select!=null ){
select.close();
}
}catch (SQLException e){
e.printStackTrace();
}
try{
if (update!=null ){
update.close();
}
}catch (SQLException e){
e.printStackTrace();
}
try{
if (delete!=null ){
delete.close();
}
}catch (SQLException e){
e.printStackTrace();
}
}
}
Получение соединения из файла Properties:
public class ConnectionFactory {
public Connection getConnection() throws MyException {
BufferedReader bufferedReader = null;
Properties properties = new Properties();
Connection connection=null;
try {
bufferedReader = new BufferedReader(new FileReader("D:\\Properties.txt"));
properties.load(bufferedReader);
connection = (Connection) DriverManager.getConnection(properties.getProperty("URL"),
properties.getProperty("USERNAME"), properties.getProperty("PASSWORD"));
return connection;
}finally {
throw new MyException();
}
}
}
class MyException extends Exception {
public MyException() {};
public MyException(String msg) {
super(msg);
};
}
Код стал нерабочим. Выскакивает ошибка MyException.
В чем проблема?
Где, что исправлять?
finally
бросаете исключение? Это все равно, что сходить в магазин за продуктами, вернуться, и перед тем как поставить пакеты в холодильник резко подбежать к окну и выкинуть в него пакеты с криком"MyException!"
. – awesoon 6 июл '15 в 17:54