我试图运行JDBC项目,sf员工管理和工资单 我发布了我的代码,任何人都可以请求帮助
package com.ibs.emp;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Properties;
public class DBConnectionTest {
static ResultSet rs;
static ResultSetMetaData rm;
static int ch, nof;
static double tax, total;
static EmpDetails empDetails = new EmpDetails();
public static double gross(){
if(EmpDetails.getHours() > 40)
return EmpDetails.getHours() * EmpDetails.getOrate();
else
return EmpDetails.getHours() * EmpDetails.getRate();
}
public static double net(){
if(EmpDetails.getStatus().equalsIgnoreCase("married"))
return total *= 0.75;
else
return total *= 0.70;
}
public static void display(double total, double tax){
System.out.println("Gross pay: " + total);
System.out.println("Net pay: " + tax);
}
public static void main(String[] args) throws NumberFormatException, IOException, SQLException, ClassNotFoundException {
DBConnection dbConnection = new DBConnection();
System.out.println("Connecting to a selected database...");
Connection con = dbConnection.getConnection();
System.out.println("Connected database successfully...");
Statement st = con.createStatement();
BufferedReader bin = new BufferedReader(new InputStreamReader(System.in));
while(true){
System.out.println("Choose Option");
System.out.println("1. Select");
System.out.println("2. Insert");
System.out.println("3. Update");
System.out.println("4. Delete");
System.out.println("5. Payslip");
System.out.println("0. Exit");
ch = Integer.parseInt(bin.readLine());
if (ch==1){
rs = st.executeQuery("select * from emp");
rm = rs.getMetaData();
nof = rm.getColumnCount();
for(int i=1; i<=nof; i++)
{
System.out.print(rm.getColumnName(i)+"\t\t");
}
System.out.println();
while(rs.next())
{
for(int i=1; i<=nof; i++)
{
System.out.print(rs.getString(i) +"\t\t");
}
System.out.println();
}
}
else if(ch==2)
{
System.out.println("Enter Name");
empDetails.setEmp_name(bin.readLine());
System.out.println("Enter Designation");
empDetails.setEmp_designation(bin.readLine());
System.out.println("Enter Emp_age");
empDetails.setEmp_age( bin.read());
System.out.println("Enter Salary");
empDetails.setEmp_salary(bin.read());
System.out.println("How many hours worked?");
empDetails.setHours(bin.read());
System.out.println("Rate of pay?");
empDetails.setRate(bin.read());
System.out.println("Over time rate?");
empDetails.setOrate(bin.read());
System.out.println("Married or Single?");
empDetails.setStatus(bin.readLine());
total = gross();
tax = net();
st.execute("insert into emp values('"+empDetails.getEmp_name()+"','"+empDetails.getEmp_age()+"','"+empDetails.getEmp_designation()+"','"+empDetails.getEmp_salary()+"','"+empDetails.getStatus()+"','"+empDetails.getOrate()+"','"+empDetails.getRate()+"','"+empDetails.getHours()+"','toatal','tax')");
System.out.println("1 Record inserted");
}
else if(ch==3)
{
System.out.println("Enter Emp_id");
empDetails.setEmp_id(bin.read());
System.out.println("Enter Name");
empDetails.setEmp_name( bin.readLine());
System.out.println("Enter Designation");
empDetails.setEmp_designation(bin.readLine());
System.out.println("Enter Emp_age");
empDetails.setEmp_age( bin.read());
System.out.println("Enter Salary");
empDetails.setEmp_salary(bin.read());
System.out.println("How many hours worked?");
empDetails.setHours(bin.read());
System.out.println("Rate of pay?");
empDetails.setEmp_name(bin.readLine());
System.out.println("Over time rate?");
empDetails.setEmp_name(bin.readLine());
System.out.println("Married or Single?");
empDetails.setEmp_name(bin.readLine());
total = gross();
tax = net();
st.execute("update emp set emp_name='"+empDetails.getEmp_name()+"',emp_age='"+empDetails.getEmp_age()+"',emp_designation='"+empDetails.getEmp_designation()+"',emp_salary='"+empDetails.getEmp_salary()+"',status='"+empDetails.getStatus()+"',orate='"+empDetails.getOrate()+"',rate='"+empDetails.getRate()+"',hours='"+empDetails.getHours()+"',taotal='toatal',tax='tax' where emp_id ="+empDetails.getEmp_id());
System.out.println("1 Record Updated");
}
else if(ch==4)
{
System.out.println("Enter Emp_id. to Delete :");
empDetails.setEmp_id(bin.read());
st.execute("delete from emp where eno ="+empDetails.getEmp_id());
System.out.println("Record Deleted");
}
else if(ch==5)
{
System.out.println("Enter Emp_id");
empDetails.setEmp_id(bin.read());
rs = st.executeQuery("select * from emp");
rm = rs.getMetaData();
nof = rm.getColumnCount();
for(int i=1; i<=nof; i++)
{
System.out.print(rm.getColumnName(i)+"\t\t");
}
System.out.println();
while(rs.next())
{
for(int i=1; i<=nof; i++)
{
System.out.print(rs.getString(i) +"\t\t");
}
System.out.println();
}
display(total, tax);
}
else if(ch==0)
{
bin.close();
con.close();
System.exit(0);
}
}
}
}
我收到了以下错误。 控制台
Exception in thread "main" java.lang.NumberFormatException: For input string: ""
at java.lang.NumberFormatException.forInputString(Unknown Source)
at java.lang.Integer.parseInt(Unknown Source)
at java.lang.Integer.parseInt(Unknown Source)
at com.ibs.emp.DBConnectionTest.main(DBConnectionTest.java)
欢迎任何建议,请帮助
答案 0 :(得分:0)
运行代码时必须传递一些整数值,尝试在运行时传递一些参数。如果您正在使用eclipse IDE,那么您必须运行配置并单击参数选项卡以传递参数。
答案 1 :(得分:0)
我认为如果没有选择选项会导致它。如果用户只是单击Enter而不选择选项,则可能会发生这种情况。
尝试替换该行:
ch = Integer.parseInt(bin.readLine());
通过
String readLine = bin.readLine();
ch = Integer.parseInt(readLine != null && !readLine.isEmpty() ? readLine : "0");