当前位置:网站首页>数据库内容输出有问题怎么解决
数据库内容输出有问题怎么解决
2022-07-02 11:48:00 【CSDN问答】
为什么代码一可以运行,代码二却不行
代码一javaimport java.sql.Connection;import java.sql.DriverManager;//import java.sql.Statement;import java.util.ArrayList;import com.mysql.jdbc.Statement;import java.sql.ResultSet;//import java.sql.SQLException;/** * * @author * */public class sql { ArrayList<Book> tushu=new ArrayList<>(); ArrayList<CD> b=new ArrayList<>(); ArrayList<Picture> c=new ArrayList<>(); Connection con=null; Statement st=null; public static void main(String[] args) throws Exception { // TODO Auto-generated method stub ResultSet rs=null; java.sql.Statement st=null; Connection con=null; Class.forName("com.mysql.jdbc.Driver"); con=DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/chengxusheji?useSSL=false","root","03579631"); st= con.createStatement(); String sql="select * from a"; rs=st.executeQuery(sql); while(rs.next()) { System.out.print(rs.getString(1)+"\t"); System.out.print(rs.getString(2)+"\t"); System.out.print(rs.getString(3)+"\t"); System.out.print(rs.getString(4)+"\t"); System.out.print(rs.getString(5)+"\t"); System.out.print(rs.getString(6)+"\t"); System.out.print(rs.getInt(7)+"\t"); System.out.print("\n"); } }}代码二java import java.util.ArrayList;import java.util.Scanner;import java.sql.*;import java.sql.Connection;import java.sql.DriverManager;//import java.sql.Statement;import com.mysql.jdbc.Statement;import java.sql.ResultSet;public class KSone { Scanner ad=new Scanner(System.in); ArrayList<Book> a=new ArrayList<>(); ArrayList<CD> b=new ArrayList<>(); ArrayList<Picture> c=new ArrayList<>(); /* * */ PreparedStatement ps1=null; PreparedStatement ps2=null; PreparedStatement ps3=null; String sql="insert into a values(?,?,?,?,?,?,?)"; String sql2="insert into b values(?,?,?,?,?,?,?)"; String sql3="insert into c values(?,?,?,?,?,?,?)"; /* * */ public static void main(String[] args) throws Exception { // TODO Auto-generated method stub Scanner ad=new Scanner(System.in); KSone p=new KSone(); /* * 数据库定义 */ Class.forName("com.mysql.jdbc.Driver"); Connection con=null; PreparedStatement ps1=null; PreparedStatement ps2=null; PreparedStatement ps3=null; con=DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/chengxusheji?useSSL=false","root","03579631"); String sql="insert into a values(?,?,?,?,?,?,?)"; String sql2="insert into b values(?,?,?,?,?,?,?)"; String sql3="insert into c values(?,?,?,?,?,?,?)"; ps1=con.prepareStatement(sql); ps2=con.prepareStatement(sql2); ps3=con.prepareStatement(sql3); /* * */ while(true) { System.out.println("***********媒体库管理系统*************"); System.out.println("\t1.添加物品"); System.out.println("\t2.查询物品"); System.out.println("\t3.显示物品库"); System.out.println("\t4.编辑物品"); System.out.println("\t5.删除物品"); System.out.println("\t6.统计信息"); System.out.println("\t7.物品存盘"); System.out.println("\t8.读出物品"); System.out.println("**********************************"); int s=ad.nextInt(); switch (s) { case 1: p.function1(); break; case 2: p.function2(); break; case 3: p.function3(); break; case 4: p.function4(); break; case 5: p.function5(); break; case 6: p.function6(); break; case 7: p.function7(); break; } } }//........public void function8() throws Exception { // ResultSet rs=null; java.sql.Statement st=null; Connection con=null; Class.forName("com.mysql.jdbc.Driver"); con=DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/chengxusheji?useSSL=false","root","03579631"); st= con.createStatement(); String sql="select * from a"; rs=st.executeQuery(sql); // ResultSet rs2=null; java.sql.Statement st2=null; st2= con.createStatement(); String sql2="select * from b"; rs2=st2.executeQuery(sql2); // ResultSet rs3=null; java.sql.Statement st3=null; st3= con.createStatement(); String sql3="select * from c"; rs3=st3.executeQuery(sql3); // while(rs.next()) { System.out.print(rs.getString(1)+"\t"); System.out.print(rs.getString(2)+"\t"); System.out.print(rs.getString(3)+"\t"); System.out.print(rs.getString(4)+"\t"); System.out.print(rs.getString(5)+"\t"); System.out.print(rs.getString(6)+"\t"); System.out.print(rs.getInt(7)+"\t"); System.out.print("\n"); } if (rs != null) { rs.close(); } if (st != null) { st.close(); } // while(rs2.next()) { System.out.print(rs2.getString(1)+"\t"); System.out.print(rs2.getString(2)+"\t"); System.out.print(rs2.getString(3)+"\t"); System.out.print(rs2.getString(4)+"\t"); System.out.print(rs2.getString(5)+"\t"); System.out.print(rs2.getString(6)+"\t"); System.out.print(rs2.getString(7)+"\t"); System.out.print("\n"); } if (rs2 != null) { rs2.close(); } if (st2 != null) { st2.close(); } // while(rs3.next()) { System.out.print(rs3.getString(1)+"\t"); System.out.print(rs3.getString(2)+"\t"); System.out.print(rs3.getString(3)+"\t"); System.out.print(rs3.getString(4)+"\t"); System.out.print(rs3.getString(5)+"\t"); System.out.print(rs3.getInt(6)+"\t"); System.out.print(rs3.getInt(7)+"\t"); System.out.print("\n"); } if (rs3 != null) { rs3.close(); } if (st3 != null) { st3.close(); } if (con != null) { con.close(); }}
运行结果及报错内容
我的解答思路和尝试过的方法
我想要达到的结果
边栏推荐
- Advanced C language (realize simple address book)
- ONNX+TensorRT:将预处理操作写入ONNX并完成TRT部署
- mathjax 入门(web显示数学公式,矢量的)
- 检查密码
- jmeter脚本参数化
- socket(套接字)与socket地址
- 为什么只会编程的程序员无法成为优秀的开发者?
- Fabric.js 自由绘制圆形
- tmall.product.schema.get( 产品信息获取schema获取 ),淘宝店铺上传商品API接口,淘宝商品发布接口,淘宝商品上传API接口,店铺上传接口,oAuth2.0接口
- Threejs controller cube space basic controller + inertia control + flight control
猜你喜欢
MathML to latex
[development environment] install the visual studio community 2013 development environment (download the installation package of visual studio community 2013 with update 5 version)
STM32库函数进行GPIO初始化
jmeter脚本参数化
ONNX+TensorRT:将预处理操作写入ONNX并完成TRT部署
Kityformula editor configure font size and spacing
What is erdma? Popular science cartoon illustration
关于网页中的文本选择以及统计选中文本长度
微信小程序使用towxml显示公式
geoserver离线地图服务搭建和图层发布
随机推荐
Fabric. JS manual bold text iText
fatal: unsafe repository is owned by someone else 的解决方法
STM32标准固件库函数名(一)
用户隐私协议有些汉字编码不规范导致网页显示乱码,需要统一找出来处理一下
Tmall product details interface (APP, H5 end)
2. Const pointer
[apipost] tutorial
Find the maximum inscribed circle of the contour
天猫商品详情接口(APP,H5端)
LeetCode 2310. 个位数字为 K 的整数之和
Chapter 9: xshell free version installation
LeetCode 2320. 统计放置房子的方式数
STM32 library function for GPIO initialization
删除元素(带过渡动画)
NLA natural language analysis realizes zero threshold of data analysis
Tip: SQL Server blocked the state 'openrowset/opendatasource' of component 'ad hoc distributed queries'
Xilinx Vivado set *. svh as SystemVerilog Header
C code audit practice + pre knowledge
使用mathtype编辑公式,复制粘贴时设置成仅包含mathjax语法的公式
Thoroughly master prototype__ proto__、 Relationship before constructor (JS prototype, prototype chain)