为什么查询所有结果错误提示是找不到列名为id的?
import com.mysql.jdbc.TimeUtil;
import java.sql.*;
import java.util.Date;
public class test4 {
public static void main(String[] args) {
demo1();
demo2();
}
public static void demo1() {
/**
*添加三条数据*/
Connection connection = null;
PreparedStatement preparedStatement = null;
ResultSet resultSet = null;
try {
//1加载驱动
Class.forName("com.mysql.jdbc.Driver");
//2获取链接
connection = DriverManager.getConnection("jdbc:mysql:///jdbc", "root", "root");
//创建SQL语句
String sql = "insert course(name,category,desp,creatTime) values(?,?,?,?),(?,?,?,?),(?,?,?,?)";
//预处理SQL语句
preparedStatement = connection.prepareStatement(sql);
//添加的具体数据
Date date = new Date();
preparedStatement.setString(1, "JAVA零基础");
preparedStatement.setString(2, "JAVA");
preparedStatement.setString(3, "JAVA基础语法");
preparedStatement.setTimestamp(4, new Timestamp(System.currentTimeMillis()));
preparedStatement.setString(5, "JAVAWEB");
preparedStatement.setString(6, "JAVA");
preparedStatement.setString(7, "JSP和SERVLET");
preparedStatement.setTimestamp(8, new Timestamp(System.currentTimeMillis()));
preparedStatement.setString(9, "前端小白");
preparedStatement.setString(10, "前端");
preparedStatement.setString(11, "HTML/CSS/JS");
preparedStatement.setTimestamp(12, new Timestamp(System.currentTimeMillis()));
int i = preparedStatement.executeUpdate();
if (i > 0) {
System.out.println("录入成功");
} else {
System.out.println("录入失败");
}
} catch (Exception e) {
e.printStackTrace();
} finally {
//释放资源
if (resultSet != null) {
try {
resultSet.close();
} catch (SQLException e) {
e.printStackTrace();
}
resultSet = null;
}
if (preparedStatement != null) {
try {
preparedStatement.close();
} catch (SQLException e) {
e.printStackTrace();
}
preparedStatement = null;
}
if (connection != null) {
try {
connection.close();
} catch (SQLException e) {
e.printStackTrace();
}
connection = null;
}
}
}
public static void demo2() {
/**
* 显示所有数据
*/
Connection connection = null;
PreparedStatement preparedStatement = null;
ResultSet resultSet = null;
//1加载驱动
try {
Class.forName("com.mysql.jdbc.Driver");
//2获取链接
connection = DriverManager.getConnection("jdbc:mysql:///jdbc", "root", "root");
//创建SQL语句
String sql = "select ? from course";
//预处理
preparedStatement = connection.prepareStatement(sql);
//实现语句
preparedStatement.setString(1, "*");
resultSet = preparedStatement.executeQuery();
if(resultSet!=null){
while (resultSet.next()) {
System.out.println("id" + resultSet.getInt("id")
+ "课程名称" + resultSet.getString("name") +
"课程描述" + resultSet.getString("category") +
"创建时间" + resultSet.getDate("creatTime"));
}}else {
System.out.println("集合为空");
}
} catch (Exception e) {
e.printStackTrace();
}finally {
//释放资源
if (resultSet != null) {
try {
resultSet.close();
} catch (SQLException e) {
e.printStackTrace();
}
resultSet = null;
}
if (preparedStatement != null) {
try {
preparedStatement.close();
} catch (SQLException e) {
e.printStackTrace();
}
preparedStatement = null;
}
if (connection != null) {
try {
connection.close();
} catch (SQLException e) {
e.printStackTrace();
}
connection = null;
}
}
}
}
正在回答 回答被采纳积分+1
- 参与学习 人
- 提交作业 357 份
- 解答问题 8016 个
本阶段将带你学习MySQL数据库,JDBC接口,MyBatis框架等,带你掌握的数据的存放和管理。
了解课程
恭喜解决一个难题,获得1积分~
来为老师/同学的回答评分吧
0 星