CREATE DATABASE mydatabase; USE mydatabase; CREATE TABLE customers ( customerID varchar(8) primary key, name varchar(40) default NULL, phone varchar(16) default NULL ); INSERT INTO customers VALUES ('ADDIFK01','Frank Addinsell', '(718) 555-3911'); INSERT INTO customers VALUES ('ALBIBB01','Bob Albinoni', '(213) 555-7566');package homework7_二_1_20221548; import java.sql.*; import javax.swing.JOptionPane; public class DemoInsert { public static void main(String args[]){ String url="jdbc:mysql://127.0.0.1:3306/mydatabase"; String user="root",pwd=""; Connection con; Statement sql; ResultSet rs; try {Class.forName("com.mysql.jdbc.Driver"); }catch (ClassNotFoundException e){ System.out.println("test"+e); } try { con = DriverManager.getConnection(url, user, pwd); sql = con.createStatement(); String mysql = "INSERT INTO customers VALUES('ALBRDE01','DAVE Albrechtsberger','(508) 555-1216"; int k = sql.executeUpdate(mysql); if (k == 1) JOptionPane.showMessageDialog(null, "插入记录成功", "成功", JOptionPane.PLAIN_MESSAGE); con.close(); }catch (SQLException ee){ JOptionPane.showMessageDialog(null, "插入记录失败", "失败", JOptionPane.ERROR_MESSAGE); } } }为什么显示插入数据失败?
时间: 2024-02-14 09:10:57 浏览: 190
sql2008sql查询语句大全[借鉴].pdf
在这个例子中,插入数据失败是因为 SQL 语句中缺少一个右括号。正确的 SQL 语句应该是:
```
String mysql = "INSERT INTO customers VALUES('ALBRDE01','DAVE Albrechtsberger','(508) 555-1216')";
```
你需要在插入数据时,确保 SQL 语句的正确性,否则插入数据会失败。
阅读全文