我在以下程序的运行时遇到错误。

编程入门 行业动态 更新时间:2024-10-09 15:22:55
本文介绍了我在以下程序的运行时遇到错误。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

private void jButton1ActionPerformed(java.awt。事件 .ActionEvent evt){ // TODO添加你的在此处理代码: 尝试 { Class.forName( java.sql.Driver); 连接con = DriverManager.getConnection( jdbc:mysql:// localhost:3306 / hotel_petrichor, root, 1234); int pday = 0 ; String rt; rt = c1.getSelectedItem()。toString(); if (rt.equals( General AC)) pday = 2000 ; else if (rt.equals( Delux)) pday = 2500 ; else if (rt.equals( Super Delux)) pday = 3500 ; else if (rt.equals( 特殊套房)) pday = 5000 ; jt6.setText( + pday); int book = Integer.parseInt(jt1.getText()); String name = jt2.getText(); long phone = Long.parseLong(jt3.getText()); int rno = Integer.parseInt(jt4.getText()); int days = Integer.parseInt(jt5.getText()); int total = pday * days; jt7.setText(total + ); 字符串 query = 插入hotelp( bno,name,phone,rno,rtype,days,perday,total)values(' + book + ',' + name + ',' + phone + ',' + rno + ',' + days + ',' + pday + ',' + total + ');; Statement stmt; stmt =(Statement)con.createStatement(); stmt.executeUpdate(查询); JOptionPane.showMessageDialog(null, 已成功保存); stmt.close(); con.close(); } catch (例外e1) { System.out.println(e1); JOptionPane.showMessageDialog(null, error); } $ b创建数据库hotel_petrichor; 使用hotel_petrichor; 创建表hotelp ( bno int ( 5 ), 名称varchar( 20 ), phone int ( 20 ), rno int ( 4 ), rtype varchar( 20 ), days int ( 2 ), perday int ( 4 ), 总计 int ( 6 ));

我尝试了什么: 我曾尝试编辑计数写作,但我得到同样的消息一遍又一遍gian java.sql.SQLException:列数与第1行的值计数不匹配

解决方案

String query = 插入hotelp(bno,name,phone,rno,rtype,days ,perday,total)values(' + book + ',' + name + ',' + phone + ',' + rno + ',' + days + ',' + pday + ',' + total + ');;

快速计算列名数和变量数表明你没有 rtype 。 如果你使用了正确的参数化查询,你会立即看到这个问题。

看看代码:

字符串查询=插入hotelp(bno,name,phone,rno,rtype,days,perday,total)的值('+ book +',' +名称+, ' +电话+', ' + RNO +', ' +天+', ' + PDAY +', ' +总+');;

现在将数据与列匹配:

bno book name name phone phone rno rno rtype days 天pday perday总计总计

rtype 发生了什么? 但是不要这样做! 永远不要连接字符串来构建SQL命令。它让您对意外或故意的SQL注入攻击持开放态度,这可能会破坏您的整个数据库。总是使用参数化查询。 连接字符串时会导致问题,因为SQL会收到如下命令:

SELECT * FROM MyTable WHERE StreetAddress = ' Baker' s Wood '

就SQL而言,用户添加的引号会终止字符串,并且您会遇到问题。但情况可能更糟。如果我来并改为输入:x'; DROP TABLE MyTable; - 然后SQL收到一个非常不同的命令:

SELECT * FROM MyTable WHERE StreetAddress = ' x'; DROP 表 MyTable; - '

哪个SQL看作三个单独的命令:

SELECT * FROM MyTable WHERE StreetAddress = ' x';

完全有效的SELECT

DROP TABLE MyTable;

完全有效的删除表格通讯和

- '

其他一切都是评论。 所以它确实:选择任何匹配的行,从数据库中删除表,并忽略其他任何内容。 所以总是使用参数化查询!或者准备好经常从备份中恢复数据库。您是否定期进行备份,不是吗?

String query = 插入hotelp(bno,name,phone,rno,rtype,days,perday,total)值(' + book + ',' + name + ',' + phone + ',' + rno + ',' + days + ',' + pday + ',' + total + ');;

不是你问题的解决方案,而是你遇到的另一个问题。 永远不要通过连接字符串来构建SQL查询。迟早,您将使用用户输入来执行此操作,这会打开一个名为SQL注入的漏洞,这对您的数据库很容易并且容易出错。 名称中的单引号你的程序崩溃。如果用户输入像Brian O'Conner这样的名称可能会使您的应用程序崩溃,那么这是一个SQL注入漏洞,崩溃是最少的问题,恶意用户输入,并且它被提升为具有所有凭据的SQL命令。 SQL注入 - 维基百科 [ ^ ] SQL注入 [ ^ ] 按示例进行SQL注入攻击 [ ^ ] PHP:SQL注入 - 手册 [ ^ ] SQL注入预防备忘单 - OWASP [ ^ ] 我该怎么办?解释没有技术术语的SQL注入? - 信息安全堆栈交换 [ ^ ]

private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) { // TODO add your handling code here: try { Class.forName("java.sql.Driver"); Connection con= DriverManager.getConnection("jdbc:mysql://localhost:3306/hotel_petrichor","root","1234"); int pday = 0; String rt; rt=c1.getSelectedItem().toString(); if(rt.equals("General AC")) pday = 2000; else if(rt.equals("Delux")) pday = 2500; else if(rt.equals("Super Delux")) pday = 3500; else if(rt.equals("Special Suite")) pday = 5000; jt6.setText(""+pday); int book= Integer.parseInt (jt1.getText()); String name= jt2.getText(); long phone= Long.parseLong (jt3.getText()); int rno = Integer.parseInt(jt4.getText()); int days = Integer.parseInt(jt5.getText()); int total= pday*days; jt7.setText(total+""); String query="insert into hotelp (bno,name,phone,rno,rtype,days,perday,total) values ( '"+book+"','"+name+"','"+phone+"','"+rno+"','"+days+"','"+pday+"','"+total+"');"; Statement stmt; stmt = (Statement) con.createStatement(); stmt.executeUpdate(query); JOptionPane.showMessageDialog(null,"Saved Successfully"); stmt.close(); con.close(); } catch(Exception e1) { System.out.println(e1); JOptionPane.showMessageDialog(null,"error"); } create database hotel_petrichor; use hotel_petrichor; create table hotelp ( bno int(5), name varchar(20), phone int(20), rno int(4), rtype varchar(20), days int(2), perday int(4), total int(6) );

What I have tried: ive tried editing counting re-writing but i get the same message over and over agian "java.sql.SQLException: Column count doesn't match value count at row 1"

解决方案

String query="insert into hotelp (bno,name,phone,rno,rtype,days,perday,total) values ( '"+book+"','"+name+"','"+phone+"','"+rno+"','"+days+"','"+pday+"','"+total+"');";

A quick count of the number of column names and the number of variables shows that you do not have a value for rtype. If you used proper parameterised queries you would see this issue immediately.

Look at the code:

String query="insert into hotelp (bno,name,phone,rno,rtype,days,perday,total) values ( '"+book+"','"+name+"','"+phone+"','"+rno+"','"+days+"','"+pday+"','"+total+"');";

Now match up the data with the columns:

bno book name name phone phone rno rno rtype days days pday perday total total

What happened to the rtype? But don't do it like that! Never concatenate strings to build a SQL command. It leaves you wide open to accidental or deliberate SQL Injection attack which can destroy your entire database. Always use Parameterized queries instead. When you concatenate strings, you cause problems because SQL receives commands like:

SELECT * FROM MyTable WHERE StreetAddress = 'Baker's Wood'

The quote the user added terminates the string as far as SQL is concerned and you get problems. But it could be worse. If I come along and type this instead: "x';DROP TABLE MyTable;--" Then SQL receives a very different command:

SELECT * FROM MyTable WHERE StreetAddress = 'x';DROP TABLE MyTable;--'

Which SQL sees as three separate commands:

SELECT * FROM MyTable WHERE StreetAddress = 'x';

A perfectly valid SELECT

DROP TABLE MyTable;

A perfectly valid "delete the table" command

--'

And everything else is a comment. So it does: selects any matching rows, deletes the table from the DB, and ignores anything else. So ALWAYS use parameterized queries! Or be prepared to restore your DB from backup frequently. You do take backups regularly, don't you?

String query="insert into hotelp (bno,name,phone,rno,rtype,days,perday,total) values ( '"+book+"','"+name+"','"+phone+"','"+rno+"','"+days+"','"+pday+"','"+total+"');";

Not a solution to your question, but another problem you have. Never build an SQL query by concatenating strings. Sooner or later, you will do it with user inputs, and this opens door to a vulnerability named "SQL injection", it is dangerous for your database and error prone. A single quote in a name and your program crash. If a user input a name like "Brian O'Conner" can crash your app, it is an SQL injection vulnerability, and the crash is the least of the problems, a malicious user input and it is promoted to SQL commands with all credentials. SQL injection - Wikipedia[^] SQL Injection[^] SQL Injection Attacks by Example[^] PHP: SQL Injection - Manual[^] SQL Injection Prevention Cheat Sheet - OWASP[^] How can I explain SQL injection without technical jargon? - Information Security Stack Exchange[^]

更多推荐

我在以下程序的运行时遇到错误。

本文发布于:2023-11-23 13:21:05,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1621588.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:我在   错误   程序

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!