社区!
我目前正在使用强大的Intellij IDEA Ultimate版学习Java。我试图连接到本地SQL Server数据库,但是我做得很困难。起初,我尝试使用内置工具(我成功连接了,但似乎只能在Intellij控制台上运行SQL查询)。更具体地说,我创建了一个基本的GUI,在其中客户可以输入他们的个人信息,我希望该信息存储在Customers SQL表中。我还尝试使用JDBC,但出现错误“com.microsoft.sqlserver.jdbc.SQLServerDriver”。我已经下载了所需的驱动程序,并将其放置在项目的lib文件夹中,但我不知道还要做什么。我的猜测是我没有正确链接或将带有驱动程序的jar文件放置在项目中。JetBrain的Intellij文档在SQL工具方面非常有限。它只涉及基本功能。如果有人能确定我的JDBC方法中的错误,或者对Intellij的SQL工具有更深入的了解,我会非常感激。感谢您花费时间帮助这个6个月的新手 :)
以下是我的代码:
import java.sql.*;
import javafx.application.Application;
import javafx.geometry.Insets;
import javafx.scene.*;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.TextField;
import javafx.scene.layout.GridPane;
import javafx.stage.Stage;
public class Main extends Application {
String name;
Stage window;
Scene scene;
Button submitButton = new Button("Submit");
Label fNameLabel = new Label("First Name:");
Label lNameLabel = new Label("Last Name:");
Label birthDateLabel = new Label("DOB:");
Label addressLabel = new Label("Address:");
Label emailLabel = new Label("E-mail:");
Label phoneLabel = new Label("Phone:");
TextField fNameTextField = new TextField();
TextField lNameTextField = new TextField();
TextField birthDateTextField = new TextField();
TextField addressTextField = new TextField();
TextField emailTextField = new TextField();
TextField phoneTextField = new TextField();
public static void main(String args[]) {
launch(args);
}
@Override
public void start(Stage primaryStage) throws Exception {
window = primaryStage;
window.setTitle("Customer Information");
window.setOnCloseRequest(e -> {
e.consume();
closeWindow();
});
//create GripPane scene
GridPane grid = new GridPane();
grid.setPadding(new Insets(10,10,10,10));
grid.setVgap(10);
grid.setHgap(10);
//set location of labels
GridPane.setConstraints(fNameLabel,3,0);
GridPane.setConstraints(lNameLabel,3,1);
GridPane.setConstraints(birthDateLabel,3,2);
GridPane.setConstraints(addressLabel,3,3);
GridPane.setConstraints(emailLabel,3,4);
GridPane.setConstraints(phoneLabel,3,5);
//set location of TextFields
GridPane.setConstraints(fNameTextField,4,0);
GridPane.setConstraints(lNameTextField,4,1);
GridPane.setConstraints(birthDateTextField,4,2);
GridPane.setConstraints(addressTextField,4,3);
GridPane.setConstraints(emailTextField,4,4);
GridPane.setConstraints(phoneTextField,4,5);
//set PromptText
birthDateTextField.setPromptText("mm/dd/yyyy");
emailTextField.setPromptText("example@example.com");
//set button location
GridPane.setConstraints(submitButton, 4, 6);
//add all elements to grid
grid.getChildren().addAll(fNameLabel,fNameTextField,lNameLabel,lNameTextField,
birthDateLabel,birthDateTextField,addressLabel,addressTextField,
emailLabel,emailTextField,phoneLabel,phoneTextField,submitButton);
scene = new Scene(grid, 400, 400);
window.setScene(scene);
window.show();
}
//properly exit out of app
private void closeWindow() {
boolean answer = ConfirmationBox.display("title", "Are you sure you want to exit?");
if (answer) {
window.close();
}
}
}
尝试连接的SQL类:
import java.sql.*;
public class SQLMethods {
public static void main(String[] args) {
try {
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
String url = "jdbc:sqlserver://localhost\\SQLEXPRESS:1433;databaseName=CaiMaster";
Connection conn = DriverManager.getConnection(url);
Statement statement = conn.createStatement();
ResultSet resultSet;
resultSet = statement.executeQuery("SELECT Fname, Address FROM Customers WHERE Fname = 'Cai'");
String lastName = resultSet.getString("Fname");
String address = resultSet.getString("Address");
System.out.println(lastName);
System.out.println(address);
conn.close();
} catch (Exception e) {
System.err.println("Got an exception! ");
System.err.println(e.getMessage());
}
}
}