我正在尋找一些在下面的幫助。如何「簡化」MySQL數據庫程序的Java代碼?
這裏是我的代碼:
package com.company;
import com.mysql.jdbc.exceptions.MySQLDataException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Connection mysql = null;
PreparedStatement pst = null;
ResultSet data = null;
try
{
Class.forName("com.mysql.jdbc.Driver");
String connectionStringURL = "jdbc:mysql://us-cdbr-azure-west-b.cleardb.com:3306/database";
mysql = DriverManager.getConnection(connectionStringURL, "username", "password");
if(mysql == null)
System.out.println("Connection Failed");
else
System.out.println("Success");
System.out.println("What would you like to do?");
System.out.println("Type 1 to display all students.");
System.out.println("Type 2 to Add/Update students.");
System.out.println("Type 3 to Delete students.");
System.out.println("Type 4 to Search for a student.");
Scanner scan = new Scanner(System.in);
int input = scan.nextInt();
if(input == 1)
{
pst = mysql.prepareStatement("SELECT * FROM Student;");
data = pst.executeQuery();
while (data.next()) {
System.out.println(data.getString("studentId"));
System.out.println(data.getString("firstname"));
System.out.println(data.getString("lastname"));
System.out.println(data.getInt("gpa"));
System.out.println(data.getString("major"));
System.out.println(data.getString("facultyAdvisor"));
System.out.println();
}
data.close();
}
if(input == 2)
{
System.out.println("What would you like to do?");
System.out.println("Type Add to add a student.");
System.out.println("Type Update to update a student's information.");
String answer = scan.next();
if(answer.equals("Add"))
{
System.out.println("Enter the student First Name");
String firstname = scan.next();
System.out.println("Enter the student's Last Name");
String lastname = scan.next();
System.out.println("Enter the student's GPA");
int GPA = scan.nextInt();
System.out.println("Enter the student's Major");
String Major = scan.next();
System.out.println("Enter the student's Faculty Advisor");
String FacultyAdvisor = scan.next();
pst = mysql.prepareStatement("INSERT INTO Student (FirstName, LastName, GPA, Major, FacultyAdvisor) VALUES (?, ?, ?, ?, ?)");
pst.setString(1, firstname);
pst.setString(2, lastname);
pst.setInt(3, GPA);
pst.setString(4, Major);
pst.setString(5, FacultyAdvisor);
pst.executeUpdate();
}
if(answer.equals("Update"))
{
System.out.println("Please enter the student's Id");
int id = scan.nextInt();
System.out.println("You may only update the Major or the Advisor. Which would you like to update?");
System.out.println("Enter Major to update the major.");
System.out.println("Enter Advisor to update the advisor.");
String result = scan.next();
if(result.equals("Major"))
{
System.out.println("Please enter the new Major");
String Major = scan.next();
pst = mysql.prepareStatement("UPDATE Student SET Major = ? WHERE StudentId = ?");
pst.setString(1, Major);
pst.setInt(2, id);
pst.executeUpdate();
}
if(result.equals("Advisor"))
{
System.out.println("Please enter the new Advisor");
String Advisor = scan.next();
pst = mysql.prepareStatement("UPDATE Student SEt FacultyAdvisor = ? WHERE StudentId = ?");
pst.setString(1, Advisor);
pst.setInt(2, id);
pst.executeUpdate();
}
}
}
if(input == 3)
{
System.out.println("Enter the student's ID");
int Studentid = scan.nextInt();
pst = mysql.prepareStatement("DELETE FROM Student WHERE StudentId = ?");
pst.setInt(1, Studentid);
pst.executeUpdate();
}
if(input == 4) {
System.out.println("How would you like to search for students?");
System.out.println("Type Major to search by the student's Major.");
System.out.println("Type GPA to search by the student's GPA.");
System.out.println("Type Advisor to search by the student's Faculty Advisor.");
String search = scan.next();
if (search.equals("Major")) {
System.out.println("Enter the Student's Major");
String major = scan.next();
pst = mysql.prepareStatement("SELECT * FROM Student WHERE Major = ?");
pst.setString(1, major);
data = pst.executeQuery();
while (data.next())
{
System.out.println(data.getString("studentId"));
System.out.println(data.getString("firstname"));
System.out.println(data.getString("lastname"));
System.out.println(data.getInt("gpa"));
System.out.println(data.getString("major"));
System.out.println(data.getString("facultyAdvisor"));
System.out.println();
}
data.close();
}
if (search.equals("GPA")) {
System.out.println("Enter the Student's GPA");
int GPA = scan.nextInt();
pst = mysql.prepareStatement("SELECT * FROM Student WHERE GPA = ?");
pst.setInt(1, GPA);
data = pst.executeQuery();
while (data.next())
{
System.out.println(data.getString("studentId"));
System.out.println(data.getString("firstname"));
System.out.println(data.getString("lastname"));
System.out.println(data.getInt("gpa"));
System.out.println(data.getString("major"));
System.out.println(data.getString("facultyAdvisor"));
System.out.println();
}
data.close();
}
if (search.equals("Advisor")) {
System.out.println("Enter the Student's Advisor");
String advisor = scan.next();
pst = mysql.prepareStatement("SELECT * FROM Student WHERE FacultyAdvisor = ?");
pst.setString(1, advisor);
data = pst.executeQuery();
while (data.next())
{
System.out.println(data.getString("studentId"));
System.out.println(data.getString("firstname"));
System.out.println(data.getString("lastname"));
System.out.println(data.getInt("gpa"));
System.out.println(data.getString("major"));
System.out.println(data.getString("facultyAdvisor"));
System.out.println();
}
data.close();
}
}
mysql.close();
}
catch(Exception ex)
{
ex.printStackTrace();
}
}
}
我需要什麼正在以連接到我的MySQL數據庫到它自己的方法(或東西的那種(如果你不頂部的部分幫助相信這是真的有必要請讓我知道))。我已經在這裏(和其他網站)上看過其他人的代碼,以瞭解如何做到這一點,但是每當我嘗試做類似於他們所做的事情時,我都會遇到大量的錯誤,所以這就是爲什麼我'已經離開它了。
此外,我想使每個部分(如果(輸入= 1)等..)到他們自己的方法,以便我可以從主要方法調用它們。我多年前就學習了Java,迄今爲止我所做的研究並沒有真正幫助我。最後一件事,如果任何人有任何提示如何使這更「用戶友好」(< - 只有我能想到的東西),那麼請讓我知道。
非常感謝你提前。
P.S.我使用的軟件是JetBrains IntelliJ(用於實際編碼)和JetBrains DataGrip(用於製作表格/數據庫並檢查是否發生變化)
P.P.S.此外,一切似乎都在正常工作,並按照預期進行。然而,在IntelliJ中,只要在整個代碼中有一個「.prepareStatement(」某些SQL代碼「)」,它就會突出顯示它,並告訴我「此方法可能調用java.lang.NullPointerException」,這是我應該擔心的?
編輯:
所以這裏是我現在有:
public class Main {
private static Connection mysql;
private static PreparedStatement pst;
private static ResultSet data;
private static void method1()
{
PreparedStatement pst = null;
pst = mysql.prepareStatement("SELECT * FROM Student;");
data = pst.executeQuery();
while (data.next()) {
System.out.println(data.getString("studentId"));
System.out.println(data.getString("firstname"));
System.out.println(data.getString("lastname"));
System.out.println(data.getInt("gpa"));
System.out.println(data.getString("major"));
System.out.println(data.getString("facultyAdvisor"));
System.out.println();
}
}
private static void method2()
{
System.out.println("What would you like to do?");
System.out.println("Type Add to add a student.");
System.out.println("Type Update to update a student's information.");
String answer = scan.next();
if(answer.equals("Add"))
{
System.out.println("Enter the student First Name");
String firstname = scan.next();
System.out.println("Enter the student's Last Name");
String lastname = scan.next();
System.out.println("Enter the student's GPA");
int GPA = scan.nextInt();
System.out.println("Enter the student's Major");
String Major = scan.next();
System.out.println("Enter the student's Faculty Advisor");
String FacultyAdvisor = scan.next();
pst = mysql.prepareStatement("INSERT INTO Student (FirstName, LastName, GPA, Major, FacultyAdvisor) VALUES (?, ?, ?, ?, ?)");
pst.setString(1, firstname);
pst.setString(2, lastname);
pst.setInt(3, GPA);
pst.setString(4, Major);
pst.setString(5, FacultyAdvisor);
pst.executeUpdate();
}
if(answer.equals("Update"))
{
System.out.println("Please enter the student's Id");
int id = scan.nextInt();
System.out.println("You may only update the Major or the Advisor. Which would you like to update?");
System.out.println("Enter Major to update the major.");
System.out.println("Enter Advisor to update the advisor.");
Scanner scanner= new Scanner(System.in);
String result = scanner.next();
if(result.equals("Major"))
{
System.out.println("Please enter the new Major");
String Major = scanner.next();
pst = mysql.prepareStatement("UPDATE Student SET Major = ? WHERE StudentId = ?");
pst.setString(1, Major);
pst.setInt(2, id);
pst.executeUpdate();
}
if(result.equals("Advisor"))
{
System.out.println("Please enter the new Advisor");
String Advisor = scanner.next();
pst = mysql.prepareStatement("UPDATE Student SEt FacultyAdvisor = ? WHERE StudentId = ?");
pst.setString(1, Advisor);
pst.setInt(2, id);
pst.executeUpdate();
}
}
}
private static void method3()
{
System.out.println("Enter the student's ID");
int Studentid = scan.nextInt();
pst = mysql.prepareStatement("DELETE FROM Student WHERE StudentId = ?");
pst.setInt(1, Studentid);
pst.executeUpdate();
}
private static void method4()
{
System.out.println("How would you like to search for students?");
System.out.println("Type Major to search by the student's Major.");
System.out.println("Type GPA to search by the student's GPA.");
System.out.println("Type Advisor to search by the student's Faculty Advisor.");
Scanner find = new Scanner(System.in);
String search = find.next();
if (search.equals("Major")) {
System.out.println("Enter the Student's Major");
String major = find.next();
pst = mysql.prepareStatement("SELECT * FROM Student WHERE Major = ?");
pst.setString(1, major);
data = pst.executeQuery();
while (data.next())
{
System.out.println(data.getString("studentId"));
System.out.println(data.getString("firstname"));
System.out.println(data.getString("lastname"));
System.out.println(data.getInt("gpa"));
System.out.println(data.getString("major"));
System.out.println(data.getString("facultyAdvisor"));
System.out.println();
}
data.close();
}
if (search.equals("GPA")) {
System.out.println("Enter the Student's GPA");
int GPA = find.nextInt();
pst = mysql.prepareStatement("SELECT * FROM Student WHERE GPA = ?");
pst.setInt(1, GPA);
data = pst.executeQuery();
while (data.next())
{
System.out.println(data.getString("studentId"));
System.out.println(data.getString("firstname"));
System.out.println(data.getString("lastname"));
System.out.println(data.getInt("gpa"));
System.out.println(data.getString("major"));
System.out.println(data.getString("facultyAdvisor"));
System.out.println();
}
data.close();
}
if (search.equals("Advisor")) {
System.out.println("Enter the Student's Advisor");
String advisor = find.next();
pst = mysql.prepareStatement("SELECT * FROM Student WHERE FacultyAdvisor = ?");
pst.setString(1, advisor);
data = pst.executeQuery();
while (data.next())
{
System.out.println(data.getString("studentId"));
System.out.println(data.getString("firstname"));
System.out.println(data.getString("lastname"));
System.out.println(data.getInt("gpa"));
System.out.println(data.getString("major"));
System.out.println(data.getString("facultyAdvisor"));
System.out.println();
}
data.close();
}
}
public static void main(String[] args) {
connect();
try
{
System.out.println("What would you like to do?");
System.out.println("Type 1 to Display all students.");
System.out.println("Type 2 to Add/Update students.");
System.out.println("Type 3 to Delete students.");
System.out.println("Type 4 to Search for a student.");
Scanner scan = new Scanner(System.in);
int input = scan.nextInt();
switch (input)
{
case 1:
method1();
break;
case 2:
method2();
break;
case 3:
method3();
break;
case 4:
method4();
break;
default:
System.out.println("You chose incorrectly and this program will now terminate.");
}
}
catch(Exception ex)
{
ex.printStackTrace();
}
}
private static void connect()
{
Class.forName("com.mysql.jdbc.Driver");
String connectionStringURL = "jdbc:mysql://us-cdbr-azure-west-b.cleardb.com:3306/acsm_0a00c1270f36f77";
mysql = DriverManager.getConnection(connectionStringURL, "b448ce74b4a1f1", "595da2aa");
if(mysql == null)
System.out.println("Connection Failed");
else
System.out.println("Success");
}
}
到處它說: 「mysql.preparestatement(」 // ... 「)」 或「數據。 // ...「或」DriverManager.getConnection(// ...)「他們都給我了同樣的錯誤:未處理的異常:java.sql.SQLException。
它說「Class.forname(// ...)」它說:未處理的異常:java.lang.ClassNotFoundException。我如何解決這些問題?
是該方法內的連接字段?如果是這樣,我需要把它放在所有的方法? –
抱歉,我的意思是'班'字段。然後,您可以在類中的任何地方使用它,例如在使用連接方法初始化它之後。請參閱編輯。 – Tokazio