【发布时间】:2019-11-28 12:11:24
【问题描述】:
我想让自己沉浸在数据库项目中,所以我进入了 MS SQL SERVER MANAGEMENT 并陷入了我朋友插入语句的部分,其中代码的逻辑应该向表中插入值(dbo.course)但我的朋友离开了它,我不知道它是如何工作的..现在我检查了表格..它仍然是空白的..
就在这里
导入 java.sql.; 导入 java.util.;
公共类主{
static final String JDBC_DRIVER = "com.microsoft.sqlserver.jdbc.SQLServerDriver";
static final String DB_URL = "jdbc:sqlserver://localhost:1434;" +
"databaseName=database_sample;integratedSecurity=true";
// Database credentials
/*
* static final String USER = "root"; static final String PASS = "root";
*/
public static void main(String[] args) {
Connection conn = null;
Statement stmt = null;
Scanner scn = new Scanner(System.in);
String course_code = null, course_desc = null, course_chair = null;
try {
// STEP 2: Register JDBC driver
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
// STEP 3: Open a connection
System.out.print("\nConnecting to database...");
conn = DriverManager.getConnection(DB_URL);
/*
* PreparedStatement ps = conn.prepareStatement("CREATE DATABASE databasename");
* int result = ps.executeUpdate();
*/
System.out.println(" SUCCESS!\n");
// STEP 4: Ask for user input
System.out.print("Enter course code: ");
course_code = scn.nextLine();
System.out.print("Enter course description: ");
course_desc = scn.nextLine();
System.out.print("Enter course chair: ");
course_chair = scn.nextLine();
// STEP 5: Execute query
System.out.print("\nInserting records into table...");
stmt = conn.createStatement();
String sql = "INSERT INTO course (course_code, course_desc, course_chair)" +
"VALUES ('\"+course_code+\"','\"+course_desc+\"','\"+course_chair+\"')"; /*
* "INSERT INTO course " + "VALUES (course_code, course_desc, course_chair)";
*/
System.out.println(" SUCCESS!\n");
} 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!");
}
}
我再说一遍,它不插入值..我看一下表格(dbo.course)..那里什么都没有
【问题讨论】:
-
我不知道 java,但我仍然认为您缺少实际执行插入语句的语句。看起来你只是打印它,而不是执行它
标签: java sql-server eclipse