Привет. Возникло пару вопросов. 1. Есть класс Field1 i TableManager. класс Field1 представляет собой таблицу в базе данных с одним полем. TableManager класс который работает с БД. Как правильно возвращать результат запроса так как в методе getAllTable или getAllField1. Код | package one;
/** * Created by IntelliJ IDEA. * User: Администратор * Date: 20 жовт 2009 * Time: 9:19:55 * To change this template use File | Settings | File Templates. */
public class Field1 { private int number;
public int getNumber() { return number; }
public void setNumber(int number) { this.number = number; }
}
|
Код |
package one;
import java.sql.*; import java.util.ArrayList; import java.util.Properties; import java.util.LinkedList;
/** * Created by IntelliJ IDEA. * User: Администратор * Date: 19 жовт 2009 * Time: 22:27:32 * To change this template use File | Settings | File Templates. */ public class TableManager { private Connection connection; private Properties property=new Properties(); private static TableManager instance=null;
public static synchronized TableManager getInstance(){ if(instance==null){ instance=new TableManager(); return instance; } else return instance;
}
public void setProperty(String user,String pass){ property.setProperty("user",user); property.setProperty("password",pass); } public ArrayList getAllField1() { ArrayList list = new ArrayList(5);
try {
Statement statement = connection.createStatement(); ResultSet rs = statement.executeQuery("SELECT field1 from oneTable");
while (rs.next()) { // System.out.println(rs.getString(1)); list.add(rs.getString(1)); }
rs.close(); statement.close(); connection.close();
} catch (SQLException e) { displaySQLException(e); //To change body of catch statement use File | Settings | File Templates. } finally { return list; }
} public ArrayList<Field1> getAllTable(){ ArrayList<Field1> list = new ArrayList<Field1>(5);
try {
Statement statement = connection.createStatement(); ResultSet rs = statement.executeQuery("SELECT field1 from oneTable"); Field1 field=new Field1(); while (rs.next()) { // System.out.println(rs.getString(1)); field.setNumber(rs.getInt(1)); list.add(field); }
rs.close(); statement.close(); connection.close();
} catch (SQLException e) { displaySQLException(e); //To change body of catch statement use File | Settings | File Templates. } finally {
return list; }
}
public void connectToDB(){ try{ connection = DriverManager.getConnection("jdbc:mysql://localhost/test",property);
} catch(SQLException e) { displaySQLException(e); } } private void displaySQLException(SQLException e){ System.out.println("SQLException :"+e.getMessage()); System.out.println("SQLState :"+e.getSQLState()); System.out.println("VendorError :"+e.getErrorCode()); } private TableManager() { try{ Class.forName("com.mysql.jdbc.Driver").newInstance(); } catch (ClassNotFoundException e) { e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates. } catch (IllegalAccessException e) { e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates. } catch (InstantiationException e) { e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates. } }
}
А вот сам мейн
|
Код | import one.TableManager;
import java.util.ArrayList;
/** * Created by IntelliJ IDEA. * User: Администратор * Date: 19 жовт 2009 * Time: 20:46:34 * To change this template use File | Settings | File Templates. */ public class Main { public static void main(String[] args){ TableManager table=TableManager.getInstance(); ArrayList getList=new ArrayList(5); table.setProperty("root",""); table.connectToDB(); getList= table.getAllField1(); for(Object s:getList){ System.out.println(s); }
} }
|
Это сообщение отредактировал(а) Neox_GeForce - 20.10.2009, 10:02
--------------------
 Челябинские программисты настолько суровы, что обходятся без компиляторов. Челябинские программисты настолько суровы, что считают ассемблер недопустительной роскошью - они вручную магнетизируют участки жесткого диска.
|