darkeruser 2021-12-26 09:51 采纳率: 70%
浏览 46
已结题

在Java中执行数据库的插入操作

问题遇到的现象和发生背景

img


img

问题相关代码,请勿粘贴截图

package 实训;

/**
 * Launch the application.
 */
public static void main(String[] args) {
    EventQueue.invokeLater(new Runnable() {
        public void run() {
            try {
                studentstelylogin window = new studentstelylogin();
                window.frame.setVisible(true);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    });
}

/**
 * Create the application.
 */
public studentstelylogin() {
    initialize();
}

/**
 * Initialize the contents of the frame.
 */
private void initialize() {

    frame = new JFrame();
    frame.setBounds(400, 200, 450, 300);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setVisible(true);
    frame.setResizable(false);
    frame.getContentPane().setLayout(null);

    JLabel j1 = new JLabel("***欢迎登陆学生信息管理系统***");
    j1.setBounds(120, 26, 180, 15);
    frame.getContentPane().add(j1);

    JLabel lblNewLabel = new JLabel("用户名:");
    lblNewLabel.setBounds(100, 56, 54, 15);
    frame.getContentPane().add(lblNewLabel);

    JLabel lblNewLabel_1 = new JLabel("密码:");
    lblNewLabel_1.setBounds(100, 102, 54, 15);
    frame.getContentPane().add(lblNewLabel_1);

    textField = new JTextField();
    textField.setBounds(196, 53, 100, 21);
    frame.getContentPane().add(textField);
    textField.setColumns(10);

    passwordField = new JPasswordField();
    passwordField.setBounds(196, 96, 100, 21);
    frame.getContentPane().add(passwordField);

    JButton btnNewButton = new JButton("登录");
    btnNewButton.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {

            shujuku c = new shujuku();

            Connection conn = c.getConnect();

            PreparedStatement ps = null;
            ResultSet rs = null;
            long ino;

            try {
                ps = conn.prepareStatement("select *from userinfo where username=? and password=?");

                ps.setString(1, textField.getText());
                ps.setString(2, passwordField.getText());

                rs = ps.executeQuery();

                if (rs.next()) {
                    new studentstelylogin();
                    int b=1;
                if(b==1)    {frame.removeNotify();
                    new logincure();
                    System.out.println("1、查询 \t2、添加 \t3、删除\t4、修改\t0、退出系统");
                    @SuppressWarnings("resource")
                    Scanner in = new Scanner(System.in);
                    int a = in.nextInt();

                    switch (a) {
                    case 1: {

                        Statement stmt = null;
                        try {
                            Class.forName("com.mysql.jdbc.Driver");
                        } catch (ClassNotFoundException e1) {
                            // TODO 自动生成的 catch 块
                            e1.printStackTrace();
                        }
                        // 2.通过DriverManager获取数据库连接
                        String url = "jdbc:mysql://localhost:3306/jdbc?useUnicode=true&characterEncoding=utf8";
                        String username = "root";
                        String password = "root";
                        conn = DriverManager.getConnection(url, username, password);
                        stmt = conn.createStatement();
                        String sql = "select * from sc";
                        rs = stmt.executeQuery(sql);
                        while (rs.next()) {
                            String id = rs.getString("学号");
                            int cid = rs.getInt("课程号");
                            int sc = rs.getInt("成绩");
                            System.out.println("学号:" + id + "课程号:" + cid + "成绩:" + sc);
                        }

                    }
                    case 2: {

                        Statement stmt = null;
                        try {
                            Class.forName("com.mysql.jdbc.Driver");
                        } catch (ClassNotFoundException e1) {
                            // TODO 自动生成的 catch 块
                            e1.printStackTrace();
                        }
                        // 2.通过DriverManager获取数据库连接
                        String url = "jdbc:mysql://localhost:3306/jdbc?useUnicode=true&characterEncoding=utf8";
                        String username = "root";
                        String password = "root";
                        conn = DriverManager.getConnection(url, username, password);
                        stmt = conn.createStatement();
                        String sql = "insert into sc(学号,课程号,成绩) values(?,?)";
                        ino = stmt.executeLargeUpdate(sql);
                        stmt.execute(sql);
                        while (rs.next()) {
                            String id = rs.getString("学号");
                            int cid = rs.getInt("课程号");
                            int sc = rs.getInt("成绩");
                            System.out.println("学号:" + id + "课程号:" + cid + "成绩:" + sc);
                        }

                    }}
                    }

                } else {
                    JOptionPane pane = new JOptionPane("用户或密码错误");
                    JDialog dialog = pane.createDialog("警告");
                    dialog.show();
                }
            } catch (SQLException e1) {
                // TODO Auto-generated catch block
                e1.printStackTrace();
            }

        }

    });
    btnNewButton.setBounds(41, 169, 93, 23);
    frame.getContentPane().add(btnNewButton);

    JButton btnNewButton_1 = new JButton("注册");
    btnNewButton_1.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {

            frame.removeNotify();

        }
    });
    btnNewButton_1.setBounds(183, 169, 93, 23);
    frame.getContentPane().add(btnNewButton_1);

    JButton j2 = new JButton("退出");
    j2.setBounds(323, 169, 93, 23);
    frame.getContentPane().add(j2);
    j2.addActionListener(new ActionListener() {

        @Override
        public void actionPerformed(ActionEvent e) {
            // TODO 自动生成的方法存根
            if (e.getActionCommand().equals("退出")) {
                System.exit(0);
            }
        }
    });

}

@SuppressWarnings("unused")
private void add(JButton quitBt2) {
    // TODO Auto-generated method stub

}

private Container getContentPane() {
    // TODO Auto-generated method stub
    return null;
}

}

运行结果及报错内容

1、查询 2、添加 3、删除 4、修改 0、退出系统
2
Exception in thread "AWT-EventQueue-0" java.lang.UnsupportedOperationException: executeLargeUpdate not implemented
at java.sql/java.sql.Statement.executeLargeUpdate(Statement.java:1236)
at qmks/实训.studentstelylogin$2.actionPerformed(studentstelylogin.java:161)
at java.desktop/javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:1967)
at java.desktop/javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2308)
at java.desktop/javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:405)
at java.desktop/javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:262)
at java.desktop/javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:279)
at java.desktop/java.awt.Component.processMouseEvent(Component.java:6614)
at java.desktop/javax.swing.JComponent.processMouseEvent(JComponent.java:3342)
at java.desktop/java.awt.Component.processEvent(Component.java:6379)
at java.desktop/java.awt.Container.processEvent(Container.java:2263)
at java.desktop/java.awt.Component.dispatchEventImpl(Component.java:4990)
at java.desktop/java.awt.Container.dispatchEventImpl(Container.java:2321)
at java.desktop/java.awt.Component.dispatchEvent(Component.java:4822)
at java.desktop/java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4919)
at java.desktop/java.awt.LightweightDispatcher.processMouseEvent(Container.java:4548)
at java.desktop/java.awt.LightweightDispatcher.dispatchEvent(Container.java:4489)
at java.desktop/java.awt.Container.dispatchEventImpl(Container.java:2307)
at java.desktop/java.awt.Window.dispatchEventImpl(Window.java:2769)
at java.desktop/java.awt.Component.dispatchEvent(Component.java:4822)
at java.desktop/java.awt.EventQueue.dispatchEventImpl(EventQueue.java:772)
at java.desktop/java.awt.EventQueue$4.run(EventQueue.java:721)
at java.desktop/java.awt.EventQueue$4.run(EventQueue.java:715)
at java.base/java.security.AccessController.doPrivileged(AccessController.java:391)
at java.base/java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:85)
at java.base/java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:95)
at java.desktop/java.awt.EventQueue$5.run(EventQueue.java:745)
at java.desktop/java.awt.EventQueue$5.run(EventQueue.java:743)
at java.base/java.security.AccessController.doPrivileged(AccessController.java:391)
at java.base/java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:85)
at java.desktop/java.awt.EventQueue.dispatchEvent(EventQueue.java:742)
at java.desktop/java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:203)
at java.desktop/java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:124)
at java.desktop/java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:113)
at java.desktop/java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:109)
at java.desktop/java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:101)
at java.desktop/java.awt.EventDispatchThread.run(EventDispatchThread.java:90)

我的解答思路和尝试过的方法

我用查询时候是可以的,所以粘贴查询代码,修改了一下语句可是可是提示我错误

我想要达到的结果

完成在Java中对数据库的表进行插入操作

  • 写回答

1条回答 默认 最新

  • 未聞花名丶 2021-12-26 18:12
    关注

    改成execute

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

问题事件

  • 系统已结题 1月3日
  • 已采纳回答 12月26日
  • 创建了问题 12月26日

悬赏问题

  • ¥170 如图所示配置eNSP
  • ¥20 docker里部署springboot项目,访问不到扬声器
  • ¥15 netty整合springboot之后自动重连失效
  • ¥15 悬赏!微信开发者工具报错,求帮改
  • ¥20 wireshark抓不到vlan
  • ¥20 关于#stm32#的问题:需要指导自动酸碱滴定仪的原理图程序代码及仿真
  • ¥20 设计一款异域新娘的视频相亲软件需要哪些技术支持
  • ¥15 stata安慰剂检验作图但是真实值不出现在图上
  • ¥15 c程序不知道为什么得不到结果
  • ¥15 键盘指令混乱情况下的启动盘系统重装