当前位置:网站首页>数据库内容输出有问题怎么解决
数据库内容输出有问题怎么解决
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(); }}运行结果及报错内容
我的解答思路和尝试过的方法
我想要达到的结果
边栏推荐
- String matching problem
- STM32标准固件库函数名(一)
- 删除元素(带过渡动画)
- Fabric.js 动态设置字号大小
- Fabric.js 自由绘制圆形
- There is no solution to the decryption error of the remote user 'sa' and the service master password mapped from the remote server 'to the local user' (null) /sa '
- Factal: Unsafe repository is owned by someone else Solution
- 871. Minimum refueling times: simple priority queue (heap) greedy question
- Bit by bit of OpenCV calling USB camera
- 871. 最低加油次数 : 简单优先队列(堆)贪心题
猜你喜欢

geoserver离线地图服务搭建和图层发布

fatal: unsafe repository is owned by someone else 的解决方法

【apipost】使用教程

LeetCode 2320. 统计放置房子的方式数

Fabric.js 缩放画布

Fatal: unsafe repository is owned by someone else

What is erdma? Popular science cartoon illustration

Obsidian installs third-party plug-ins - unable to load plug-ins

Ad20 cannot select the solution of component packaging in PCB editor

Tujia muniao meituan has a discount match in summer. Will it be fragrant if the threshold is low?
随机推荐
Add vector formula in rich text editor (MathType for TinyMCE, visual addition)
871. Minimum refueling times: simple priority queue (heap) greedy question
tmall.product.schema.get( 产品信息获取schema获取 ),淘宝店铺上传商品API接口,淘宝商品发布接口,淘宝商品上传API接口,店铺上传接口,oAuth2.0接口
Find the maximum inscribed circle of the contour
Ad20 cannot select the solution of component packaging in PCB editor
为什么只会编程的程序员无法成为优秀的开发者?
C# 线程传参
Fabric. Usage of JS eraser (including recovery function)
taobao.trade.memo.add( 对一笔交易添加备注 )接口,淘宝店铺插旗接口,淘宝订单插旗API接口,oAuth2.0接口
Kityformula editor configure font size and spacing
STM32 standard firmware library function name (I)
Tujia muniao meituan has a discount match in summer. Will it be fragrant if the threshold is low?
电脑怎么设置扬声器播放麦克风的声音
kityformula-editor 配置字号和间距
[Space & single cellomics] phase 1: single cell binding space transcriptome research PDAC tumor microenvironment
About text selection in web pages and counting the length of selected text
MathML to latex
871. 最低加油次数 : 简单优先队列(堆)贪心题
NLA natural language analysis realizes zero threshold of data analysis
btrace-(字节码)动态跟踪工具