我正在尝试将我的java项目连接到数据库。我正在创建一个方法来使用以下代码创建表
package DataBaseConnectivity;
import java.sql.Connection;
import java.sql.Driver;
import java.sql.DriverManager;
import java.sql.Statement;
import java.util.Scanner;
public class DataBaseConnectivity {
public static void main(String args[]) {
// Creating the connection using Oracle DB
// Note: url syntax is standard, so do grasp
String url = "jdbc:sqlserver://localhost:1433;databaseName=SchoolMgt;encrypt=true;trustServerCertificate=true";
// Username and password to access DB
// Custom initialization
String user = "sa";
String pass = "root";
// Entering the data
Scanner scanner = new Scanner(System.in);
String tableCreationsql = " create table Students ( id integer PRIMARY KEY,fname VARCHAR (8),birthdate date,lname VARCHAR (8))";
Connection con = null;
// Try block to check for exceptions
try {
Driver driver = (Driver) Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver").newInstance();
// Registering drivers
DriverManager.registerDriver(driver);
// Reference to connection interface
con = DriverManager.getConnection(url, user, pass);
// Creating a statement
Statement st = con.createStatement();
// Executing query
int Executing = st.executeUpdate(tableCreationsql);
if (Executing >= 1) {
System.out.println("Created Successfully : " + tableCreationsql);
} else {
System.out.println("Creation Is Failed");
}
// Closing the connections
con.close();
}
// Catch block to handle exceptions
catch (Exception ex) {
// Display message when exceptions occurs
System.err.println(ex);
}
}
}
代码工作正常,但它从不输入if条件,而是直接转到else。此外,即使转到else,也会创建一些表
我希望创建成功,并应在数据库中创建表。
1条答案
按热度按时间wlzqhblo1#
executeUpdate
返回受影响的行数(参见文档)由于您正在创建表(DDL语句),因此0行受到影响,因此
Executing
等于0