Java MySQL表中的ArrayIndexOutOfBoundsException
我在MySQL中创建了一个包含一个表的数据库:Java MySQL表中的ArrayIndexOutOfBoundsException,java,indexoutofboundsexception,Java,Indexoutofboundsexception,我在MySQL中创建了一个包含一个表的数据库: CREATE DATABASE iac_enrollment_system; USE iac_enrollment_system; CREATE TABLE course( course_code CHAR(7), course_desc VARCHAR(255) NOT NULL, course_chair VARCHAR(255), PRIMARY KEY(course_code) ); 我使用以下Java
CREATE DATABASE iac_enrollment_system;
USE iac_enrollment_system;
CREATE TABLE course(
course_code CHAR(7),
course_desc VARCHAR(255) NOT NULL,
course_chair VARCHAR(255),
PRIMARY KEY(course_code)
);
我使用以下Java代码将记录插入表中:
// Insert multiple records with user input into a table
// With separate methods
import java.sql.*;
import java.util.*;
class InsertSQL3 {
static final String JDBC_DRIVER = "com.mysql.jdbc.Driver";
static final String DB_URL = "jdbc:mysql://localhost:3306/iac_enrollment_system";
static final String USER = "root";
static final String PASS = "1234";
static int no_of_records;
static String[] saCourseCode = new String[no_of_records];
static String[] saCourseDesc = new String[no_of_records];
static String[] saCourseChair = new String[no_of_records];
static Connection conn = null;
static Statement stmt = null;
static void getInput() {
Scanner scn = new Scanner(System.in);
// Get number of records
System.out.print("How many records do you want to insert? ");
no_of_records = scn.nextInt();
scn.nextLine();
// Get values
for(int i = 0; i < no_of_records; i++) {
System.out.print("\nEnter course code: ");
saCourseCode[i] = scn.nextLine();
System.out.print("Enter course description: ");
saCourseDesc[i] = scn.nextLine();
System.out.print("Enter course chair: ");
saCourseChair[i] = scn.nextLine();
}
}
static void executeQuery() {
System.out.print("\nInserting records into table...");
try {
stmt = conn.createStatement();
for(int i = 0; i < no_of_records; i++) {
String sql = "INSERT INTO course(course_code, course_desc, course_chair)" +
"VALUES(?, ?, ?)";
PreparedStatement ps = conn.prepareStatement(sql);
ps.setString(1, saCourseCode[i]);
ps.setString(2, saCourseDesc[i]);
ps.setString(3, saCourseChair[i]);
ps.executeUpdate();
}
} catch(SQLException se) {
se.printStackTrace();
}
System.out.println(" SUCCESS!\n");
}
public static void main(String[] args) {
try {
Class.forName("com.mysql.jdbc.Driver");
System.out.print("\nConnecting to database...");
conn = DriverManager.getConnection(DB_URL, USER, PASS);
System.out.println(" SUCCESS!\n");
getInput();
executeQuery();
} catch(SQLException se) {
se.printStackTrace();
} catch(Exception e) {
e.printStackTrace();
} finally {
try {
if(stmt != null)
conn.close();
} catch(SQLException se) {
}
try {
if(conn != null)
conn.close();
} catch(SQLException se) {
se.printStackTrace();
}
}
System.out.println("Thank you for your patronage!");
}
}
//将用户输入的多条记录插入表中
//用不同的方法
导入java.sql.*;
导入java.util.*;
类InsertSQL3{
静态最终字符串JDBC_DRIVER=“com.mysql.JDBC.DRIVER”;
静态最终字符串DB_URL=“jdbc:mysql://localhost:3306/iac_enrollment_system";
静态最终字符串USER=“root”;
静态最终字符串传递=“1234”;
记录的静态int no_;
静态字符串[]saCourseCode=新字符串[没有记录];
静态字符串[]saCourseDesc=新字符串[没有记录];
静态字符串[]saCourseChair=新字符串[没有记录];
静态连接conn=null;
静态语句stmt=null;
静态void getInput(){
扫描仪scn=新扫描仪(System.in);
//获取记录数
System.out.print(“要插入多少条记录?”);
没有任何记录=scn.nextInt();
scn.nextLine();
//获取价值
for(int i=0;i
输入课程代码后,例如,BSCS-SE
,它返回以下错误:
为什么它是不允许的?非常感谢您的帮助。谢谢。数值原语在Java中有一个默认值
0
,因此数组saCourseCode
是一个大小为零的数组,不能分配任何值。在接受无记录后初始化数组
no_of_records = scn.nextInt();
saCourseCode = new String[no_of_records];
这也适用于数组saCourseDesc
和saCourseChair
编辑:
下面是一个用于初始化数组的模板。省略了一些代码,但您应该能够理解其余代码:
class InsertSQL3 {
static final String JDBC_DRIVER = "com.mysql.jdbc.Driver";
static final String DB_URL = "jdbc:mysql://localhost:3306/iac_enrollment_system";
static final String USER = "root";
static final String PASS = "***";
private int noOfRecords;
private String[] saCourseCode;
private String[] saCourseDesc;
private String[] saCourseChair;
private Connection conn = null;
private Statement stmt = null;
private Scanner scn;
public InsertSQL3() {
scn = new Scanner(System.in);
}
private void initConnection() {
...
}
private void getInput() {
// Get number of records
System.out.print("How many records do you want to insert? ");
noOfRecords = Integer.parseInt(scn.nextLine());
saCourseCode = new String[noOfRecords];
saCourseDesc = new String[noOfRecords];
saCourseChair = new String[noOfRecords];
...
}
private void executeQuery() {
...
}
public static void main(String[] args) {
InsertSQL3 insertApp = new InsertSQL3();
insertApp.initConnection();
insertApp.getInput();
insertApp.executeQuery();
System.out.println("Thank you for your patronage!");
}
}
在Java中,数值原语的默认值为
0
,因此数组saCourseCode
是一个大小为零的数组,不能为其分配任何值。在接受无记录后初始化数组
no_of_records = scn.nextInt();
saCourseCode = new String[no_of_records];
这也适用于数组saCourseDesc
和saCourseChair
编辑:
下面是一个用于初始化数组的模板。省略了一些代码,但您应该能够理解其余代码:
class InsertSQL3 {
static final String JDBC_DRIVER = "com.mysql.jdbc.Driver";
static final String DB_URL = "jdbc:mysql://localhost:3306/iac_enrollment_system";
static final String USER = "root";
static final String PASS = "***";
private int noOfRecords;
private String[] saCourseCode;
private String[] saCourseDesc;
private String[] saCourseChair;
private Connection conn = null;
private Statement stmt = null;
private Scanner scn;
public InsertSQL3() {
scn = new Scanner(System.in);
}
private void initConnection() {
...
}
private void getInput() {
// Get number of records
System.out.print("How many records do you want to insert? ");
noOfRecords = Integer.parseInt(scn.nextLine());
saCourseCode = new String[noOfRecords];
saCourseDesc = new String[noOfRecords];
saCourseChair = new String[noOfRecords];
...
}
private void executeQuery() {
...
}
public static void main(String[] args) {
InsertSQL3 insertApp = new InsertSQL3();
insertApp.initConnection();
insertApp.getInput();
insertApp.executeQuery();
System.out.println("Thank you for your patronage!");
}
}
您的阵列将始终为零长度:
static int no_of_records;
static String[] saCourseCode = new String[no_of_records];
static String[] saCourseDesc = new String[no_of_records];
static String[] saCourseChair = new String[no_of_records];
数组立即创建,长度为no\u of_records
,当然是0
,因为您没有为其分配任何内容,Java将int
的默认值设置为0
如果编译器没有警告你,我会非常惊讶
只有在知道有多少条记录后,才能创建数组
另外:我强烈建议不要使用
静态变量。这是您应该包含在实例中的内容。您的数组长度始终为零:
static int no_of_records;
static String[] saCourseCode = new String[no_of_records];
static String[] saCourseDesc = new String[no_of_records];
static String[] saCourseChair = new String[no_of_records];
static int no_of_records;
static String[] saCourseCode = new String[no_of_records];
数组立即创建,长度为no\u of_records
,当然是0
,因为您没有为其分配任何内容,Java将int
的默认值设置为0
如果编译器没有警告你,我会非常惊讶
只有在知道有多少条记录后,才能创建数组
另外:我强烈建议不要使用静态变量。这是您应该在实例中完成的事情
static int no_of_records;
static String[] saCourseCode = new String[no_of_records];
相当于
static int no_of_records = 0;
static String[] saCourseCode = new String[no_of_records];
因此,所有数组的长度都是0no\u of_记录
仅在getInput()
方法中的之后初始化。在没有任何记录被初始化后初始化数组
请遵守Java命名约定
相当于
static int no_of_records = 0;
static String[] saCourseCode = new String[no_of_records];
因此,所有数组的长度都是0no\u of_记录
仅在getInput()
方法中的之后初始化。在没有任何记录被初始化后初始化数组
并且请遵守Java命名约定。Astatic int
最初是0
,直到它被赋值为止。
您需要在了解阵列的大小后创建阵列,即:
static void getInput() {
Scanner scn = new Scanner(System.in);
// Get number of records
System.out.print("How many records do you want to insert? ");
no_of_records = scn.nextInt();
scn.nextLine();
// Now we know how many records we need to handle!
String[] saCourseCode = new String[no_of_records];
String[] saCourseDesc = new String[no_of_records];
String[] saCourseChair = new String[no_of_records];
// Get values
for(int i = 0; i < no_of_records; i++) {
static void getInput(){
扫描仪scn=新扫描仪(System.in);
//得到怒火