我需要为经理创建一个Java JDBC GUI应用程序,以便能够为员工输入有关其家属的数据。我的代码在下面,一切正常。我无法弄清楚如何让用户能够为多个依赖者输入数据。我想我需要某种循环,但无法弄清楚如何将其添加到此代码中。到目前为止,我尝试过的所有内容都没有工作,只有最新输入的值才会插入到数据库中。
package database;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.util.Scanner;
public class addDependent {
public static void main(String[] args) throws Exception {
getConnection();
addEmplDependent();
}
public static void addEmplDependent() throws Exception{
Connection dbConnection = null;
PreparedStatement preparedStatement = null;
Scanner scanner = new Scanner(System.in);
System.out.print("Enter Employee SSN: ");
String ESSN = scanner.nextLine();
System.out.print("Enter Dependent Name: ");
String DNAME = scanner.nextLine();
System.out.print("Enter Sex: ");
String SEX = scanner.nextLine();
System.out.print("Enter Relationship: ");
String RELATIONSHIP = scanner.nextLine();
String insertSQL3 = "INSERT INTO DEPENDENT"
+ "(ESSN,DEPENDENT_NAME,SEX,RELATIONSHIP) VALUES"
+ "(?,?,?,?)";
scanner.close();
try {
dbConnection = getConnection();
preparedStatement = dbConnection.prepareStatement(insertSQL3);
preparedStatement.setString(1, ESSN);
preparedStatement.setString(2, DNAME);
preparedStatement.setString(3, SEX);
preparedStatement.setString(4, RELATIONSHIP);
preparedStatement.executeUpdate();
}
catch (Exception e){System.out.println(e);}
finally {
System.out.println("Insert Completed!");
}
}
public static Connection getConnection() throws Exception{
try {
String driver = "oracle.jdbc.driver.OracleDriver";
String url = "jdbc:oracle:thin:@apollo.vse.gmu.edu:1521:ite10g";
String username = "*******";
String password = "******";
Class.forName(driver);
Connection conn = DriverManager.getConnection(url, username,
password);
System.out.println("Connected");
return conn;
} catch(Exception e) {System.out.println(e);}
return null;
}
}
答案 0 :(得分:0)
您可以执行swing-GUI并在单击按钮时插入数据。 如果要在命令行上运行它,只需在每行上循环插入,直到用户决定退出为止。尽可能少地更改代码,它看起来可能是这样的:
public class addDependent {
public static void main(String[] args) throws Exception {
addEmplDependent();
}
public static void addEmplDependent() throws Exception{
Connection dbConnection = null;
PreparedStatement preparedStatement = null;
String insertSQL3 = "INSERT INTO DEPENDENT "
+ "(ESSN,DEPENDENT_NAME,SEX,RELATIONSHIP) VALUES "
+ "(?,?,?,?)";
try {
dbConnection = getConnection();
preparedStatement = dbConnection.prepareStatement(insertSQL3);
Scanner scanner = new Scanner(System.in);
boolean continue = true;
while ( continue ) {
System.out.print("Enter Employee SSN: ");
String ESSN = scanner.nextLine();
System.out.print("Enter Dependent Name: ");
String DNAME = scanner.nextLine();
System.out.print("Enter Sex: ");
String SEX = scanner.nextLine();
System.out.print("Enter Relationship: ");
String RELATIONSHIP = scanner.nextLine();
preparedStatement.setString(1, ESSN);
preparedStatement.setString(2, DNAME);
preparedStatement.setString(3, SEX);
preparedStatement.setString(4, RELATIONSHIP
preparedStatement.executeUpdate();
System.out.print("Continue ? (y/n) : ");
String goaHead = scanner.nextLine();
if ( goaHead.equalsIgnoreCase("n") {
continue = false;
}
}
scanner.close();
dbConnection.close();
preparedStatement.close();
}
catch (Exception e){System.out.println(e);}
finally {
System.out.println("Insert Completed!");
}
}