gpt4 book ai didi

jdbc项目中的java.lang.NumberFormatException

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

我正在尝试运行 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)

欢迎任何建议,请帮忙

最佳答案

您必须在运行代码时传递一些整数值,请尝试在运行时传递一些参数。如果您使用的是 eclipse IDE,那么您必须去运行配置并单击参数选项卡来传递参数。

关于jdbc项目中的java.lang.NumberFormatException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45485200/

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