6 Star 2 Fork 0

the-bug/超市货物管理系统

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
QueryALLStaff.java 3.14 KB
一键复制 编辑 原始数据 按行查看 历史
gdutwen 提交于 5年前 . 程序包 主类为LOGIN
package surpermarket;
/***
* 该类用于查询所有职工信息
*
* 具体使用方法请参考下方主函数
*/
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.Statement;
import java.sql.ResultSet;
import javax.swing.JOptionPane;
public class QueryALLStaff
{
// 加载数据库驱动 com.mysql.jdbc.Driver
public static final String DBDRIVER = "com.mysql.cj.jdbc.Driver";
// 获取mysql连接地址
public static final String DBURL = "jdbc:mysql://localhost:3306/supermarket?&useSSL=false&allowPublicKeyRetrieval=true&serverTimezone=UTC";
// 数据名称
public static final String DBUSER = "root";
// 数据库密码
public static final String DBPASS = "00000000";
private String[] str = new String[80]; //查询结果集保存于此字符串数组
public String[] getstr() { //向前端传递查询结果信息
return this.str;
}
public QueryALLStaff() throws Exception
{
Connection conn = null; // 数据的连接
Statement pstmt = null; // 数据库的操作
ResultSet rs = null;
String sql = "SELECT Sno,Sname,Sposition,Sphone FROM STAFF";
Class.forName(DBDRIVER); //加载驱动程序
conn = DriverManager.getConnection(DBURL,DBUSER,DBPASS); //连接MySQL数据库时,要写上连接的用户名和密码
pstmt = conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_UPDATABLE); //实例化statsment对象
rs = pstmt.executeQuery(sql); //执行数据库查询操作
if(rs.next()==false) //判断职工表中是否有信息
JOptionPane.showMessageDialog(null,"系统中还没有员工信息!请先添加员工相关信息。");
else {
rs.beforeFirst();
int i=0;
while(rs.next()){ //将查询结果保存到字符串数组中
for(int j=0;j<4;j++) {
str[i*4+j] = new String();
str[i*4+j] = rs.getString(j+1);
}
i++;
}
}
rs.close(); //关闭结果集
pstmt.close(); //关闭操作
conn.close(); //关闭数据库
}
//———————测试——————————
public static void main(String[] args) throws Exception{
QueryALLStaff qs = new QueryALLStaff(); //点击用户界面查询全体即可,不需要获取输入框中信息
int x=0;
String[] str = qs.getstr(); //接受查询结果
while(str[x]!=null) x=x+4;
//循环输出结果集中内容
for(int i=0;i<x/4;i++) {
String sno = str[i*4+0];
System.out.print("编号:"+sno+"\t");
String sname = str[i*4+1];
System.out.print("姓名:"+sname+"\t");
String sposition = str[i*4+2];
System.out.print("权限:"+sposition+"\t");
String sphone = str[i*4+3];
System.out.println("电话:"+sphone+"\t");
}
}
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Java
1
https://gitee.com/the-bug/surpermaket.git
git@gitee.com:the-bug/surpermaket.git
the-bug
surpermaket
超市货物管理系统
master

搜索帮助