gpt4 book ai didi

java - 用数据库值填充数组列表

转载 作者:太空宇宙 更新时间:2023-11-04 08:39:57 25 4
gpt4 key购买 nike

/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package XawalaManager;

import java.sql.*; // DB handling package
import java.util.*;
import javax.swing.table.*;
import javax.swing.table.AbstractTableModel;
import java.awt.*;
import javax.swing.*;
import javax.swing.event.TableModelEvent;

/**
*
* @author Abdi Aden
*/
public class DBHandler extends AbstractTableModel {


private static Connection connection;

private static Statement stmt;
Vector columnHeaders;
Vector tableData;
static int id;
public static int autokey = -1;
static String [] contactList;
static ArrayList senders = new ArrayList();

public DBHandler() {
Vector rowData;
try {
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
// Assumes Messages.mdb is in the same folder as MessageData.class
String sourceURL = "jdbc:odbc:Driver={Microsoft Access Driver (*.mdb)};DBQ=olympic.mdb;";
connection = DriverManager.getConnection(sourceURL, "admin", "");
stmt = connection.createStatement();
String sql = "Select * FROM senderTable";
Statement stmt = connection.createStatement();
ResultSet rs = stmt.executeQuery(sql);
ResultSetMetaData md = rs.getMetaData();

int count = md.getColumnCount();
columnHeaders = new Vector(count);
tableData = new Vector();
for (int i = 1; i <= count; i++) {
columnHeaders.addElement(md.getColumnName(i));
}
while (rs.next()) {
rowData = new Vector(count);
for (int i = 1; i <= count; i++) {
rowData.addElement(rs.getObject(i));
}
tableData.addElement(rowData);
}


} catch (Exception e) {
System.out.println("There is an connection error: " +e );
}
}

public int getColumnCount() {
return columnHeaders.size();
}

public int getRowCount() {
return tableData.size();
}

public Object getValueAt(int row, int column) {
Vector rowData = (Vector) (tableData.elementAt(row));
return rowData.elementAt(column);
}



public boolean isCellEditable(int row, int column) {
return false;
}

public String getColumnName(int column) {
return (String) (columnHeaders.elementAt(column));
}






public static ArrayList getSend(){

try{
ResultSet res = stmt.executeQuery("SELECT * FROM senderTable");
//ArrayList senders = new ArrayList();
while (res.next()){
String send = res.getString(2);
senders.add(send);

}
}catch (Exception e ){
System.out.println("getSend "+e);
return null;
}
return senders;
}

// close the database

public static void close() {
try {
connection.close();
} catch (Exception e) {
// this shouldn't happen
System.out.println("close"+e);
}
}
}

这是我的完整代码,我的表模型单词很好地填满了我的表,但数组未满,并给了我一个堆栈跟踪:

getSend java.lang.NullPointerException
Exception in thread "main" java.lang.NullPointerException
at XawalaManager.mainView.<init>(mainView.java:86)
at XawalaManager.XawalaManager.<init>(XawalaManager.java:40)
at XawalaManager.XawalaManager.main(XawalaManager.java:108)
Java Result: 1

像这样

最佳答案

注释掉这一行:

//ArrayList senders =  new ArrayList();
<小时/>

将类型更改为 List 并使用泛型

List<String> senders =  new ArrayList<String>();
<小时/>

添加以下行:(DBUrl 是您的数据库地址)

Connection conn = null;
Statement stmt = null;
conn = DriverManager.getConnection("DBUrl");
stmt = con.createStatement();
<小时/>

这是您修改后的代码:

public static ArrayList getSend(){

Connection conn = null;
Statement stmt = null;
ResultSet res = null;
List<String> senders = new ArrayList<String>();

try{
conn = DriverManager.getConnection("DBUrl");
stmt = con.createStatement();
res = stmt.executeQuery("SELECT * FROM senderTable");

while (res.next()){
String send = res.getString(2);
senders.add(send);

}
}catch (Exception e ){
System.out.println("getSend "+e);
return null;
}
return senders;
}

如果您需要更多帮助,只需发布​​异常的堆栈跟踪即可。

编辑:我的本地电脑上没有数据库驱动程序和环境,但以下代码应该可以工作。

import java.sql.*;
import java.util.*;
import javax.swing.table.AbstractTableModel;

public class DBHandler extends AbstractTableModel {

private static Connection connection;
private static Statement stmt;
List<String> columnHeaders;
List<List<String>> tableData;
static int id;
public static int autokey = -1;
static String[] contactList;
static ArrayList<String> senders = new ArrayList<String>();

public DBHandler() {
List rowData = null;
try {
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
String sourceURL = "jdbc:odbc:Driver={Microsoft Access Driver (*.mdb)};DBQ=olympic.mdb;";
connection = DriverManager.getConnection(sourceURL, "admin", "");
stmt = connection.createStatement();
String sql = "Select * FROM senderTable";
ResultSet rs = stmt.executeQuery(sql);
ResultSetMetaData md = rs.getMetaData();

int count = md.getColumnCount();
columnHeaders = new ArrayList<String>();
tableData = new ArrayList<List<String>>();
for (int i = 1; i <= count; i++) {
columnHeaders.add(md.getColumnName(i));
}
while (rs.next()) {
rowData = new ArrayList<String>();
for (int i = 1; i <= count; i++) {
rowData.add(rs.getObject(i));
}
tableData.add(rowData);
}


} catch (Exception e) {
System.out.println("There is an connection error: " + e);
}
}

public int getColumnCount() {
return columnHeaders.size();
}

public int getRowCount() {
return tableData.size();
}

public Object getValueAt(int row, int column) {
ArrayList rowData = (ArrayList) (tableData.get(row));
return rowData.get(column);
}

@Override
public boolean isCellEditable(int row, int column) {
return false;
}

@Override
public String getColumnName(int column) {
return (String) (columnHeaders.get(column));
}

public static ArrayList getSend() {

try {
ResultSet res = stmt.executeQuery("SELECT * FROM senderTable");
while (res.next()) {
String send = res.getString(2);
senders.add(send);
}
} catch (Exception e) {
System.out.println("getSend " + e);
return null;
}
return senders;
}

public static void close() {
try {
connection.close();
} catch (Exception e) {
System.out.println("close" + e);
}
}
}

关于java - 用数据库值填充数组列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5556518/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com