当前位置:网站首页>报错问题Parameter index out of range(0 < 1) (1 > number of parameters,which is 0
报错问题Parameter index out of range(0 < 1) (1 > number of parameters,which is 0
2022-06-26 06:55:00 【左海峰博客】
jdbc连接数据库
public class DbUtil {
static{
try {
Class.forName("com.mysql.jdbc.Driver");
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
}
public static Connection getConnection(){
String url = "jdbc:mysql://127.0.0.1:3306/test?serverTimezone=GMT%2B8";
String account = "root";
String pwd = "root";
Connection connection = null;
try {
connection = DriverManager.getConnection(url,account,pwd);
} catch (SQLException e) {
e.printStackTrace();
}
return connection;
}使用Db.Util工具类插入数据的两种方法。
String sql = "insert into student(name,phone) values (?,?)";
PreparedStatement ps = connection.prepareStatement(sql);
ps.setString(1,firstname);
ps.setString(2,lastname);
ps.executeUpdate();占位。setString表示第几个占位符。从1开始。
或者采用直接sql拼接。
String sql = "insert into student(name,phone) values('"+firstname+"','"+lastname+"')";
PreparedStatement ps = connection.prepareStatement(sql);
ps.executeUpdate();setString可能会报的两个错误。
Parameter index out of range(0 < 1) 代表索引从1开始。
Parameter index out of range(1 > number of parameters,which is 0) 代表找不到占位符就是那个 ?号。
下面这个错记得加
![]()

边栏推荐
- Bugku练习题---MISC---富强民主
- My SQL (II)
- How to make the main thread wait for the sub thread to execute before executing
- cocoscreator播放Spine动画
- Numpy learning challenge level 4 - numpy array attribute
- I caught a 10-year-old Alibaba test developer in the company. After chatting with him, I realized everything
- Market development status analysis and investment risk outlook report of China's battery industry 2022-2027
- MySQL
- Research Report on sales scale forecast and investment opportunities of China's jewelry industry 2022-2027
- 同步通信和异步通信的区别以及优缺点
猜你喜欢

Closure problem C Lua

Shell编程-用户信息管理

Differences, advantages and disadvantages between synchronous communication and asynchronous communication

一芯实现喷雾|WS2812驱动|按键触摸|LED显示|语音播报芯片等功能,简化加湿器产品设计

STM32F1与STM32CubeIDE编程实例-热敏传感器驱动

Installation and login of MySQL database

淺析一道經典題
![[path planning] robot path planning based on improved artificial potential field with matlab code](/img/82/b4cfb84de1cd56abe0c51205e99035.png)
[path planning] robot path planning based on improved artificial potential field with matlab code

Numpy learning challenge level 4 - numpy array attribute

Invalid problem of self defined map used by Gaode map
随机推荐
Dpdk - tcp/udp protocol stack server implementation (I)
Pagoda server setup and database remote connection
Distribution operation of D
Go language learning notes 1.1
STM 32 uses cube to generate Tim to trigger ADC and transmit through DMA
Custom reference formats used by Zotero
寶塔服務器搭建及數據庫遠程連接
Judgment of SQL null value
SQL Basics
一芯实现喷雾|WS2812驱动|按键触摸|LED显示|语音播报芯片等功能,简化加湿器产品设计
Research Report on market supply and demand and strategy of natural organic beauty industry in China
The four cores of the browser: Trident, gecko, WebKit, blink
Development trends and prospects of acrylamide crystallization market in the world and China 2022-2027
Guide to "avoid dismissal during probation period"
SecureCRT运行SparkShell 删除键出现乱码的解法
Laravel implements groupby to query the number of packets
Research Report on market supply and demand and strategy of China's pallet scale industry
【图像增强】基于人工多重曝光融合AMEF实现图像去雾附matlab代码
SHOW语句用法补充
How to choose securities companies for stock speculation? Is it safe to open a mobile account?