当前位置:网站首页>数据库内容输出有问题怎么解决
数据库内容输出有问题怎么解决
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(); }}
运行结果及报错内容
我的解答思路和尝试过的方法
我想要达到的结果
边栏推荐
- 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 '
- 871. 最低加油次数 : 简单优先队列(堆)贪心题
- Bit by bit of OpenCV calling USB camera
- LeetCode_字符串_简单_412.Fizz Buzz
- The use of TestNG, the testing framework (II): the use of TestNG XML
- Fabric.js 手动加粗文本iText
- mathML转latex
- Fabric. JS free draw circle
- 【空间&单细胞组学】第1期:单细胞结合空间转录组研究PDAC肿瘤微环境
- Threejs controller cube space basic controller + inertia control + flight control
猜你喜欢
[email protected]: The platform “win32“ is incompatible with this module."/>
info [email protected]: The platform “win32“ is incompatible with this module.
It's no exaggeration to say that this is the most user-friendly basic tutorial of pytest I've ever seen
【NOI模拟赛】伊莉斯elis(贪心,模拟)
taobao.trade.memo.add( 对一笔交易添加备注 )接口,淘宝店铺插旗接口,淘宝订单插旗API接口,oAuth2.0接口
Socket and socket address
Fabric.js 上划线、中划线(删除线)、下划线
Wechat applet uses towxml to display formula
Introduction to C language -- array
实现一个多进程并发的服务器
Large top heap, small top heap and heap sequencing
随机推荐
Delete element (with transition animation)
JMeter script parameterization
LeetCode - 搜索二维矩阵
富文本编辑器添加矢量公式(MathType for TinyMCE ,可视化添加)
Reuse and distribution
info [email protected]: The platform “win32“ is incompatible with this module.
threejs的控制器 立方體空間 基本控制器+慣性控制+飛行控制
Makefile separates file names and suffixes
Advanced C language (learn malloc & calloc & realloc & free in simple dynamic memory management)
Actual combat sharing of shutter screen acquisition
kityformula-editor 配置字号和间距
CTO如何帮助业务?
记一次报错解决经历依赖重复
NLA natural language analysis makes data analysis more intelligent
MathML to latex
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 '
threejs的控制器 立方体空间 基本控制器+惯性控制+飞行控制
taobao.trade.get( 获取单笔交易的部分信息),淘宝店铺订单接口,淘宝oAuth2.0接口,淘宝R2接口代码对接分享