社區!將intellij想法連接到sql server數據庫
我目前正在使用真棒Intellij IDEA終極版學習Java。我試圖連接到本地的SQL Server數據庫,但我很難做到這一點。最初,我嘗試使用內置的工具(我能夠成功連接,但它看起來像是讓我能夠在它的Intellij控制檯上運行SQL查詢。更具體地說,我創建了一個基本的GUI客戶可以在其中輸入他們的個人信息,並且我希望將這些信息存儲在Customers SQL表中。我也嘗試使用JDBC,但出於某種原因,我得到錯誤「com.microsoft.sqlserver.jdbc.SQLServerDriver 「我沒有下載所需的驅動程序,並將它放在我的項目的lib文件夾中,但我不知道還有什麼可以做的,我的猜測是我沒有正確鏈接或將jar文件與驅動程序放在我的項目中。 Intellij文檔對於SQL Tools來說是非常有限的,它只是基本的功能,如果有人能夠在我的JDBC方法中指出我的錯誤,或者深入瞭解Intellij的SQL Tools,我會很感激。你先進的時間試圖幫助這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("[email protected]");
//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類試圖連接:關於選擇的IntelliJ
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());
}
}
}
謝謝!這開始看起來更好。在項目依賴關係中添加jar文件有所幫助。在您的建議和其他人指出我缺少憑據後,它不會給我缺少的驅動程序錯誤,但是現在我收到錯誤「結果集沒有當前行」。它的確如此。實際上它只有一行。有什麼想法嗎? –
是的,你不使用典型的習慣用法來使用ResultSet。看一下JDBC教程。你需要調用next()來移動光標。 – duffymo
先生你真棒,謝謝。出於某種原因,我雖然因爲我只想要第一行,我不需要告訴光標移動。它現在像一種魅力。超級感謝! –