【发布时间】:2017-05-05 18:57:14
【问题描述】:
我在检索 SQLite 数据库中的下一条记录时遇到了一些问题。我尝试创建了一个新的 ArrayList 和一个 Employee 的实例(一个带有 getter/setter 的类),但是我仍然有问题。我在网上搜索了最后一天半试图解决这个问题。我试过使用if(rs.next)/while(rs.next) 但是,只有while 似乎有效。我在网上看了几个教程,但遇到了SQLITE TYPE_FORWARD_ONLY.我已经实现的问题:
ResultSet.TYPE_SCROLL_SENSITIVE,
ResultSet.CONCUR_UPDATABLE
但仍然没有运气,所以我决定添加一个迭代器/计数器,因为我的 SQLite 数据库中的 ID 是唯一的。但是,问题似乎是迭代器/计数器没有增加,但是,如果我手动设置值,即 2,我会在数据库中获得第二条记录,依此类推。
nextEmployee.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
Connection connection = null;
PreparedStatement pst = null;
Statement statement = null;
ResultSet rs = null;
int i = 1;
try {
Class.forName("org.sqlite.JDBC");
connection = DriverManager.getConnection("jdbc:sqlite:employeeDatabase.sqlite");
connection.setAutoCommit(false);
String sql = "SELECT ID, Name, Gender, DOB, Address, Postcode, NIN, JobTitle, StartDate, Salary, Email from employees WHERE ID= " +(i);
pst = connection.prepareStatement(sql);
rs = pst.executeQuery();
for (i = 1; i <= 4; i++){
while (rs.next()){
String id = rs.getString(1);
String name = rs.getString(2);
String gender = rs.getString(3);
String dob = rs.getString(4);
String Address = rs.getString(5);
String Postcode = rs.getString(6);
String NIN = rs.getString(7);
String JobTitle = rs.getString(8);
String StartDate = rs.getString(9);
String Salary = rs.getString(10);
String email = rs.getString(11);
idTextField.setText(id);
nameTextField.setText(name);
genderTextField.setText(gender);
dobTextField.setText(dob);
addressTextField.setText(Address);
postcodeTextField.setText(Postcode);
ninTextField.setText(NIN);
jobtitleTextField.setText(JobTitle);
startdateTextField.setText(StartDate);
salaryTextField.setText(Salary);
emailTextField.setText(email);
// i++;
connection.commit();
JOptionPane.showMessageDialog(null, "Employee has been found");
}
}
rs.close();
pst.close();
connection.close();
}
catch ( Exception e1 ) {
JOptionPane.showMessageDialog(null, "No more records exist");
}
}});
我的想法已经不多了。这是最后一块拼图,我已经成功实现了创建/插入、删除、搜索、更新。有人可以告诉我为什么这个迭代器/计数器不会增加吗?
谢谢。
编辑 -
员工类
public class Employee extends Person {
private int id;
private float salary;
private String startDate;
private String title;
private String email;
public int getid(){
return id;
}
public void setid(int id){
this.id = id;
}
public float getsalary(){
return salary;
}
public void setsalary(float salary){
this.salary = salary;
}
public String getstartDate(){
return startDate;
}
public void setstartDate(String startDate){
this.startDate = startDate;
}
public String gettitle(){
return title;
}
public void settitle(String title){
this.title = title;
}
public String getemail(){
return email;
}
public void setemial(String email){
this.email = email;
}
public Employee(){
this("", 'N', "", new Date(), "", "", 0, 0, "", "", "");
}
public Employee( String Name, char Sex, String natIncsNumber, Date date, String address, String postcode, int ID, float Salary, String Startdate, String Title, String Email) {
super(Name, Sex,natIncsNumber, date, address, postcode);
this.id = ID;
salary = 0.0f;
this.startDate = Startdate;
this.title = Title;
this.email = Email;
}
public void setSalary(float salry){
salary = salry;
}
public String toString(){
return "Employee ID = " + id+ ", Employee Salary = " + salary + " Start Date = " + startDate +
" Title = " + title + " Email = " + email + super.toString();
}
}
人物类-
import java.io.*;
public class Person implements Serializable{
protected String name;
protected char gender;
protected String natIncsNo;
protected Date dob;
protected String Address;
protected String Postcode;
public String getName(){
return name;
}
public void setName(String name){
this.name = name;
}
public char getGender(){
return gender;
}
public void setGender(char gender){
this.gender = gender;
}
public String getnatIncsNo(){
return natIncsNo;
}
public void setnatIncsNo(String natIncsNo){
this.natIncsNo = natIncsNo;
}
public Date getdob(){
return dob;
}
public void setdob(Date dob){
this.dob = dob;
}
public String getAddress(){
return Address;
}
public void setAddress(String Address){
this.Address = Address;
}
public String getPostcode(){
return Postcode;
}
public void setPostcode(String Postcode){
this.Postcode = Postcode;
}
public Person(String Name, char Sex, String natIncsNumber, Date date, String address, String postcode) {
name = Name;
gender = Sex;
natIncsNo = natIncsNumber;
dob = date;
Address = address;
Postcode = postcode;
}
public Person(){
this("", 'N', "", new Date(), "", "");
}
public String toString(){
String output = " Name: " + name + " Gender: " + gender + " National Insurance Number: " + natIncsNo +
" Date Of Birth : " + dob +
" Address: " + Address + " Postcode: " + Postcode;
return output;
}
}
编辑 X 2-
EmployeeDAO 类
import java.security.spec.ECFieldF2m;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
public class EmployeeDAO extends Employee {
private Connection con;
private Statement st;
private ResultSet rs;
public EmployeeDAO() {
Connection connection = null;
try {
Class.forName("org.sqlite.JDBC");
connection = DriverManager.getConnection("jdbc:sqlite:employeeDatabase.sqlite");
} catch ( Exception e ) {
System.err.println( e.getClass().getName() + ": " + e.getMessage() );
System.exit(0);
}
System.out.println("Connection - database driver found");
//insertEmployeeAtID();
//selectAllEmployees();
//insertEmployee();
//deleteEmployeeByID();
}
public Statement getConnection() {
return st;
}
public void closeConnection() throws SQLException {
if (con != null) con.close();
}
public ArrayList<Employee> selectAllEmployees(){
Connection connection = null;
Statement statement = null;
try {
Class.forName("org.sqlite.JDBC");
connection = DriverManager.getConnection("jdbc:sqlite:employeeDatabase.sqlite");
connection.setAutoCommit(false);
System.out.println("Read operation - database successfully opened");
statement = connection.createStatement();
ResultSet resultset = statement.executeQuery( "SELECT * from employees" );
while ( resultset.next() ) {
int id = resultset.getInt("id");
String name = resultset.getString("name");
String email = resultset.getString("email");
String gender = resultset.getString("gender");
String dob = resultset.getString("dob");
String Address = resultset.getString("address");
String Postcode = resultset.getString("Postcode");
String NIN = resultset.getString("NIN");
String JobTitle = resultset.getString("JobTitle");
String StartDate = resultset.getString("StartDate");
String Salary = resultset.getString("Salary");
System.out.println( "ID : " + id );
System.out.println( "Name : " + name );
System.out.println( "Gender : " + gender );
System.out.println( "Date Of Birth : " + dob );
System.out.println( "Address : " + Address );
System.out.println( "Postcode : " + Postcode );
System.out.println( "National Insurance Number : " + NIN );
System.out.println( "Job Title : " + JobTitle );
System.out.println( "Start Date : " + StartDate );
System.out.println( "Salary : " + Salary );
System.out.println( "Email: " + email );
System.out.println();
}
resultset.close();
statement.close();
connection.close();
} catch ( Exception e ) {
System.err.println( e.getClass().getName() + ": " + e.getMessage() );
System.exit(0);
}
System.out.println("Read operation successfully done");
return new ArrayList<Employee>();
}
public String selectEmployeeByName() {
Connection connection = null;
Statement statement = null;
try {
Class.forName("org.sqlite.JDBC");
connection = DriverManager.getConnection("jdbc:sqlite:employeeDatabase.sqlite");
connection.setAutoCommit(false);
System.out.println("Read operation - database successfully opened");
statement = connection.createStatement();
ResultSet resultset = statement.executeQuery( "SELECT * from employees WHERE Name = 'Brad Stones';" );
while ( resultset.next() ) {
int id = resultset.getInt("id");
String name = resultset.getString("name");
String email = resultset.getString("email");
String gender = resultset.getString("gender");
String dob = resultset.getString("dob");
String Address = resultset.getString("address");
String Postcode = resultset.getString("Postcode");
String NIN = resultset.getString("NIN");
String JobTitle = resultset.getString("JobTitle");
String StartDate = resultset.getString("StartDate");
String Salary = resultset.getString("Salary");
/*
System.out.println( "ID : " + id );
System.out.println( "Name : " + name );
System.out.println( "Gender : " + gender );
System.out.println( "Date Of Birth : " + dob );
System.out.println( "Address : " + Address );
System.out.println( "Postcode : " + Postcode );
System.out.println( "National Insurance Number : " + NIN );
System.out.println( "Job Title : " + JobTitle );
System.out.println( "Start Date : " + StartDate );
System.out.println( "Salary : " + Salary );
System.out.println( "Email: " + email );
System.out.println();
*/
}
resultset.close();
statement.close();
connection.close();
} catch ( Exception e ) {
System.err.println( e.getClass().getName() + ": " + e.getMessage() );
System.exit(0);
}
System.out.println("Read operation successfully done");
return toString();
}
public boolean insertEmployee(){
Connection connection = null;
Statement statement = null;
try {
Class.forName("org.sqlite.JDBC");
connection = DriverManager.getConnection("jdbc:sqlite:employeeDatabase.sqlite");
connection.setAutoCommit(false);
System.out.println("Insert operation -database successfully opened");
statement = connection.createStatement();
String sql = "INSERT INTO employees (id, name, gender, NIN, dob, Address, Postcode,startDate, salary, email, JobTitle) "
+ "VALUES ('1', 'Brad Stones', 'M', 'YU6593864T', '09-01-1987', '100 Dalton Road', 'M1 7TA', '09-01-1981','25000', 'BradSmith@mail.com', 'Tutor')";
statement.executeUpdate(sql);
statement.close();
connection.commit();
connection.close();
} catch ( Exception e ) {
System.err.println( e.getClass().getName() + ": " + e.getMessage() );
System.exit(0);
}
System.out.println("Records successfully created");
return false;
}
public boolean insertEmployeeAtID(){
Connection connection = null;
Statement statement = null;
try {
Class.forName("org.sqlite.JDBC");
connection = DriverManager.getConnection("jdbc:sqlite:employeeDatabase.sqlite");
connection.setAutoCommit(false);
System.out.println("Update operation - database successfully opened");
statement = connection.createStatement();
String sql = "UPDATE employees set JobTitle = 'Potato' where ID=1;";
statement.executeUpdate(sql);
connection.commit();
statement.close();
connection.close();
} catch ( Exception e ) {
System.err.println( e.getClass().getName() + ": " + e.getMessage() );
System.exit(0);
}
System.out.println("Update operation successfully done");
return false;
}
public boolean deleteEmployeeByID(){
Connection connection = null;
Statement statement = null;
try {
Class.forName("org.sqlite.JDBC");
connection = DriverManager.getConnection("jdbc:sqlite:employeeDatabase.sqlite");
connection.setAutoCommit(false);
System.out.println("Delete operation -database successfully opened");
statement = connection.createStatement();
String sql = "DELETE from employees where ID= 1";
statement.executeUpdate(sql);
connection.commit();
statement.close();
connection.close();
} catch ( Exception e ) {
System.err.println( e.getClass().getName() + ": " + e.getMessage() );
System.exit(0);
}
System.out.println("Delete operation successfully done");
return false;
}
}
nextEmployee 方法-
List<Employee> list= empDAO.selectAllEmployees();
int counter=0;
nextEmployee.addActionListener(new ActionListener() {
boolean isfirstTime = true;
@Override
public void actionPerformed(ActionEvent e) {
if (isfirstTime) {
Connection connection = null;
PreparedStatement pst = null;
Statement statement = null;
ResultSet rs = null;
int i = 1;
try {
Class.forName("org.sqlite.JDBC");
connection = DriverManager.getConnection("jdbc:sqlite:employeeDatabase.sqlite");
connection.setAutoCommit(false);
String sql = "SELECT ID, Name, Gender, DOB, Address, Postcode, NIN, JobTitle, StartDate, Salary, Email from employees WHERE ID= "
+ (i);
pst = connection.prepareStatement(sql);
rs = pst.executeQuery();
while(rs.next()){
EmployeeDAO empDao=new EmployeeDAO();
//set the variables for the employee
String id = rs.getString(1);
String name = rs.getString(2);
String gender = rs.getString(3);
String dob = rs.getString(4);
String Address = rs.getString(5);
String Postcode = rs.getString(6);
String NIN = rs.getString(7);
String JobTitle = rs.getString(8);
String StartDate = rs.getString(9);
String Salary = rs.getString(10);
String email = rs.getString(11);
idTextField.setText(id);
nameTextField.setText(name);
genderTextField.setText(gender);
dobTextField.setText(dob);
addressTextField.setText(Address);
postcodeTextField.setText(Postcode);
ninTextField.setText(NIN);
jobtitleTextField.setText(JobTitle);
startdateTextField.setText(StartDate);
salaryTextField.setText(Salary);
emailTextField.setText(email);
list.add(empDao);
}
rs.close();
pst.close();
connection.close();
}
catch (Exception e1) {
JOptionPane.showMessageDialog(null, "No more records exist");
}
}else{
Employee emp=list.get(counter);
idTextField.setText(String.valueOf(emp.getid()));
//and so on. always get from emp
JOptionPane.showMessageDialog(null, "Employee has been found");
}
}
});
【问题讨论】:
-
尝试在
for循环之前移动connection.commit();。 -
@Berger 试过了,但是由于 i = 1,它只返回第一条记录,只是拒绝增加 i 的值。
-
for (i = 1; i <= 4; i++)循环的目的是什么? -
@Berger i = idValue 在数据库中,我拥有的最大记录数为 4。所以对于 i = 1 - 显示第一条记录,i = 2 - 显示第二条记录,依此类推。我希望每次点击按钮都会增加。
标签: java mysql swing sqlite jdbc