【问题标题】:Syntax to insert values in MS SQL SERVER在 MS SQL SERVER 中插入值的语法
【发布时间】: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


【解决方案1】:

你必须对 sql 做一些事情。目前它只是一个String,并没有用它做任何其他事情。
您需要将查询传递给语句并触发它的执行。

     // 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+\"')";

     stmt.executeQuery(sql); // <--- add this
     System.out.println(" SUCCESS!\n");

同时更改您的 finally 块。
有一个错字(您关闭 conn 两次)
并确保两个(stmtconn)都真正关闭,它们必须位于级联的 finally 块中(在第一次关闭操作中仍然可能有 RuntimeException,这将阻止关闭第二个资源)。

    } finally {
        try {
            if(stmt != null)
                stmt.close();
        } catch(SQLException se) {
        } finally {
           try {
              if(conn != null)
                  conn.close();
           } catch(SQLException se) {
                 se.printStackTrace();
           }
        }
    }

finally 块的替代解决方案是对它们都使用try with resource。阅读它。

【讨论】:

  • 他的查询也不会执行。右括号和 VALUES 之间没有空格
  • @juice 这样做完全没问题。列列表的右括号和values(之间不需要空格
  • @GuidoG 绝对不行。这不是 SSMS。是Java。这就是所有的一行,并且将像这样执行INSERT INTO course (course_code, course_desc, course_chair)VALUES ('\"+course_code+\"','\"+course_desc+\"','\"+course_chair+\"')
  • @juice 我不懂java,但我是否理解正确,将发送到数据库的命令看起来像INSERT INTO course (course_code, course_desc, course_chair)VALUES (...?如果是这样,那么我没有看到问题。 Sql-Server 会理解这一点
  • 但正如我所说,我不懂java,所以如果我不理解正确请纠正我
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多