我有问题。当我尝试从数据库中选择数据时,我看到错误NullPointerException。要连接数据库,请使用Singleton模式。
Singleton类:
public class Database {
private static Database instance = new Database();
private Connection con;
private Database() {
}
public static Database getInstance() {
return instance;
}
public Connection getConnection(){
return con;
}
public void connect() throws Exception {
if (con != null)
return;
try {
Class.forName("com.mysql.jdbc.Driver");
} catch (ClassNotFoundException e) {
throw new Exception(e);
}
String url = String.format("jdbc:mysql://127.0.0.1:3306/newssystem");
con = DriverManager.getConnection(url, "root","");
}
public void disconnect() {
if (con != null) {
try {
con.close();
} catch (SQLException e) {
System.out.println("Nie można zakończyć połączenia");
}
}
con = null;
}
}
选择数据的类: 公共课ArtOpcje { public ArrayList getArtykul()抛出SQLException {
ArrayList<Artykul> news = new ArrayList<Artykul>();
Connection conn = Database.getInstance().getConnection();
String sql = "SELECT id, title, article FROM news";
Statement pa = conn.createStatement();
ResultSet results = pa.executeQuery(sql);
while(results.next()) {
int id = results.getInt("id");
String title = results.getString("title");
String article = results.getString("article");
Artykul artykul = new Artykul(id, title, article);
news.add(artykul);
}
results.close();
pa.close();
return news;
}
}
当我在Combobox中使用此选项时代码碎片:
combo.setBounds(150, 250, 350, 30);
combo.removeAllItems();
System.out.println("poza pętlą");
this.add(combo);
ArrayList<Artykul> artykul = new ArrayList<Artykul>();
artykul = art.getArtykul();
String[] aa = new String[artykul.size()];
for(int i =0; i<artykul.size(); i++){
aa[i] = artykul.get(i).getTitle();
}
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at model.ArtOpcje.getArtykul(ArtOpcje.java:40)
at view.View.<init>(View.java:82)
at aplication.Aplication.runApp(Aplication.java:34)
at aplication.Aplication$1.run(Aplication.java:21)
at java.awt.event.InvocationEvent.dispatch(Unknown Source)
at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
at java.awt.EventQueue.access$500(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue.dispatchEvent(Unknown Source)
at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.run(Unknown Source)
NullPointerException在这里:
Statement pa = conn.createStatement();
答案 0 :(得分:0)
如果仔细查看代码,那么您将意识到connection
对象是在connect方法中初始化的。但是在您的代码中永远不会调用该方法。因此呼叫
Connection conn = Database.getInstance().getConnection();
将返回Connection
Object的null实例。相反,你应该使用以下代码
Database db = Database.getInstance();
db.connect();
db.getConnection();
这可以解决您的问题。
注意:在理想情况下,您应该使用连接池或 数据源而不是编写自己的JDBC代码。
答案 1 :(得分:0)
谢谢。它的工作现在。
Database db;
db = Database.getInstance();
db.connect();
db.getConnection();
String sql = "SELECT id, title, article FROM news";
PreparedStatement pa = db.getConnection().prepareStatement(sql);