当前位置:网站首页>数据库内容输出有问题怎么解决
数据库内容输出有问题怎么解决
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 usage of C language -- function pointer: callback function; Conversion table
- obsidian安装第三方插件——无法加载插件
- 关于网页中的文本选择以及统计选中文本长度
- 天猫商品详情接口(APP,H5端)
- geoserver离线地图服务搭建和图层发布
- LeetCode 2310. 个位数字为 K 的整数之和
- 【NOI模拟赛】伊莉斯elis(贪心,模拟)
- 华为面试题: 没有回文串
- btrace-(字节码)动态跟踪工具
- Edit the formula with MathType, and set it to include only mathjax syntax when copying and pasting
猜你喜欢

What is erdma? Popular science cartoon illustration

Uniapp automated test learning

复用和分用

Wechat applet uses towxml to display formula

Fabric. JS zoom canvas

Introduction to mathjax (web display of mathematical formulas, vector)

报错:npm WARN config global `--global`, `--local` are deprecated. Use `--location=global` instead.

CTO如何帮助业务?

About text selection in web pages and counting the length of selected text

【空间&单细胞组学】第1期:单细胞结合空间转录组研究PDAC肿瘤微环境
随机推荐
[QNX hypervisor 2.2 user manual]6.3 communication between guest and external
2、const 型指针
taobao. trades. sold. Get query the transaction data that the seller has sold (according to the creation time), Taobao store sales order query API interface, Taobao R2 interface, Taobao oauth2.0 trans
Fabric. JS upper dash, middle dash (strikethrough), underline
Fabric. JS zoom canvas
Implement a server with multi process concurrency
Kityformula editor configure font size and spacing
OpenCV调用USB摄像头的点滴
tmall.product.schema.get( 产品信息获取schema获取 ),淘宝店铺上传商品API接口,淘宝商品发布接口,淘宝商品上传API接口,店铺上传接口,oAuth2.0接口
富文本编辑器添加矢量公式(MathType for TinyMCE ,可视化添加)
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 '
Xilinx Vivado set *. svh as SystemVerilog Header
MFC 定时器使用
STM32标准固件库函数名(一)
Yolov6 training: various problems encountered in training your dataset
Socket and socket address
Xilinx Vivado set *.svh as SystemVerilog Header
LeetCode 2320. 统计放置房子的方式数
PTA question bank== > complex four operations, one for one, examination seat number (7-73)
Fabric.js 动态设置字号大小