📄 tabledisplay.java
字号:
//TableDisplay.java
//数据库,用于添加、删除、查询记录,提供客户端需要的信息
package com.deitel.jhtp3.ch08;
//import java.sql.Types;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.*;
import java.sql.*;
public class TableDisplay extends JFrame {
private Connection connection;//管理程序与数据库之间的连接
private JTable table;
int result;
String q;
public TableDisplay()
{
q="SELECT * FROM Trustee";
//测试
//deleteRecord(6789);
//deleteRecord(7890);
//deleteRecord(5678);
//deleteRecord(4567);
//addRecord(3456);---
//findRecord3(28);
//updateRecord(2345);
//findRecord2();
//getTable(q);
//updateRecord7(2345);
//findRecord4(9000);
setSize(500,300);//(长,宽)
show();
}
//将数据库显示在桌面上
public void getTable(String query)
{
Statement statement;
ResultSet resultSet;
try {
//String query="SELECT * FROM Trustee";
statement = connection.createStatement();
resultSet = statement.executeQuery(query);
displayResultSet(resultSet);
statement.close();
}
catch (SQLException sqlex) {
sqlex.printStackTrace();
}
}
/*注册时判断两种情况:1、端口已有记录,则将状态设为上线;
2、添加新记录,调用addRecord()
参数分别是:端口号*/
public String updateRecord7(int p7)
{
String url="jdbc:odbc:Trustee";//定位数据库
String username=""; //用户名
String password=""; //密码
String g="";
try{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");//装入驱动程序
//
connection=DriverManager.getConnection(url,username,password);//尝试建立连接
}
catch (ClassNotFoundException cnfex){
System.err.println("Failed to load JDBC/ODBC driver.");
cnfex.printStackTrace();
System.exit(1);
}
catch (SQLException sqlex){
System.err.println("Unable to connect");
sqlex.printStackTrace();
}
ResultSet resultSet7;//查询结果
try{
Statement statement7=connection.createStatement();//递交查询
//更新语言,将状态置为上线状态
String query7="UPDATE Trustee SET State=1 "+
"WHERE Port="+p7;
connection.nativeSQL(query7);//连接
result=statement7.executeUpdate(query7);//用新的记录更新数据库
if(result==1)
{
getTable(q);
g=findRecord4(p7);
//System.out.print("good");
//g=0;
}
else
{
addRecord(p7);
g=findRecord4(p7);
//g=1;
}
}
catch (SQLException sqlex7) {
sqlex7.printStackTrace();
}
return g;
}
//为注册的端口添加记录,参数是:端口号,被updateRecord7()调用
public void addRecord(int p1)
{
String url="jdbc:odbc:Trustee";
String username="";
String password="";
try{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
connection=DriverManager.getConnection(url,username,password);
}
catch (ClassNotFoundException cnfex){
System.err.println("Failed to load JDBC/ODBC driver.");
cnfex.printStackTrace();
System.exit(1);
}
catch (SQLException sqlex){
System.err.println("Unable to connect");
sqlex.printStackTrace();
}
ResultSet resultSet1;
//q="SELECT * FROM Trustee";
//getTable(q);
try{
Statement statement1=connection.createStatement();
//增添新记录
String query1="INSERT INTO Trustee (Port) VALUES("+
p1+")";
connection.nativeSQL(query1);
result=statement1.executeUpdate(query1);
//String qr="SELECT * FROM Trustee";
getTable(q);
if(result==1)
{
JOptionPane.showMessageDialog(this,"\nInsertion successful\n");
}
else
JOptionPane.showMessageDialog(this,"\nInsertion failed\n");
statement1.close();
}
catch (SQLException sqlex1) {
sqlex1.printStackTrace();
}
}
//注册结束时,返回假名,参数为端口号
public String findRecord4(int p44)
{
String url="jdbc:odbc:Trustee";
String username="";
String password="";
String fr="";
try{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
connection=DriverManager.getConnection(url,username,password);
}
catch (ClassNotFoundException cnfex){
System.err.println("Failed to load JDBC/ODBC driver.");
cnfex.printStackTrace();
System.exit(1);
}
catch (SQLException sqlex){
System.err.println("Unable to connect");
sqlex.printStackTrace();
}
ResultSet resultSet44;
try{
Statement statement44=connection.createStatement();
//查询指定端口的假名
String query44="SELECT * FROM Trustee WHERE Port="+p44;
resultSet44= statement44.executeQuery(query44);
//q=query44;
getTable(query44);
fr=test4(resultSet44);
//System.out.print(tt);
statement44.close();
}
catch (SQLException sqlex44) {
sqlex44.printStackTrace();
}
return fr;
}
//返回假名
public String test4(ResultSet rsts)
{
String xd1="";
String xd11="";
try{
//ResultSetMetaData rsmd= rsts.getMetaData();
boolean more=rsts.next();//如果还有下一条记录,则为真
while(more){
//for (int j=1;j<= rsmd.getColumnCount();++j)
//xdt=rst.getString(j)+" ";
xd1=xd1+rsts.getString("Alias");//读取假名的值
//System.out.print(xdt);
more=rsts.next();
}
//System.out.print(xdtt);
rsts.close();
}
catch(Exception e11)
{
System.out.println(e11.getMessage());
}
return xd1;
}
//注销
//更新1:将下线了的端口的状态更新为0,参数是端口号
public void updateRecord(int p4)
{
String url="jdbc:odbc:Trustee";
String username="";
String password="";
try{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
connection=DriverManager.getConnection(url,username,password);
}
catch (ClassNotFoundException cnfex){
System.err.println("Failed to load JDBC/ODBC driver.");
cnfex.printStackTrace();
System.exit(1);
}
catch (SQLException sqlex){
System.err.println("Unable to connect");
sqlex.printStackTrace();
}
ResultSet resultSet4;
try{
Statement statement4=connection.createStatement();
String query4="UPDATE Trustee SET State=0 "+
"WHERE Port="+p4;
connection.nativeSQL(query4);
result=statement4.executeUpdate(query4);
getTable(q);
if(result==1)
{
JOptionPane.showMessageDialog(this,"\nUpdate successful\n");
}
else
JOptionPane.showMessageDialog(this,"\nUpdate failed\n");
statement4.close();
}
catch (SQLException sqlex4) {
sqlex4.printStackTrace();
}
}
//退出
//将退出了的端口记录从数据库中删除,参数是端口号
public void deleteRecord(int p5)
{
String url="jdbc:odbc:Trustee";
String username="";
String password="";
try{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
connection=DriverManager.getConnection(url,username,password);
}
catch (ClassNotFoundException cnfex){
System.err.println("Failed to load JDBC/ODBC driver.");
cnfex.printStackTrace();
System.exit(1);
}
catch (SQLException sqlex){
System.err.println("Unable to connect");
sqlex.printStackTrace();
}
ResultSet resultSet5;
try{
Statement statement5=connection.createStatement();
String query5="DELETE FROM Trustee WHERE Port="+p5;
connection.nativeSQL(query5);
result=statement5.executeUpdate(query5);
//String query6="SELECT * FROM Trustee";
getTable(q);
if(result==1)
{
JOptionPane.showMessageDialog(this,"\nDelete successful\n");
}
else
JOptionPane.showMessageDialog(this,"\nDelete failed\n");
statement5.close();
}
catch (SQLException sqlex5) {
sqlex5.printStackTrace();
}
}
//请求假名
//查找某假名的主机,找到始发者,返回端口号,参数是假名
public String findRecord3(int a3)
{
String url="jdbc:odbc:Trustee";
String username="";
String password="";
String tt="";
try{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
connection=DriverManager.getConnection(url,username,password);
}
catch (ClassNotFoundException cnfex){
System.err.println("Failed to load JDBC/ODBC driver.");
cnfex.printStackTrace();
System.exit(1);
}
catch (SQLException sqlex){
System.err.println("Unable to connect");
sqlex.printStackTrace();
}
ResultSet resultSet3;
try{
Statement statement3=connection.createStatement();
String query3="SELECT * FROM Trustee WHERE Alias="+a3;
resultSet3= statement3.executeQuery(query3);
q=query3;
getTable(query3);
tt=test3(resultSet3);
//System.out.print(tt);
statement3.close();
}
catch (SQLException sqlex3) {
sqlex3.printStackTrace();
}
return tt;
}
//findRecord3()要调用的方法,在这里获得数据
public String test3(ResultSet rs)
{
String xd="";
try{
ResultSetMetaData rsmd = rs.getMetaData();
boolean more=rs.next();
while(more){
//for (int j=1;j<= rsmd.getColumnCount();++j)
//xd=rs.getString(j)+" ";
xd=rs.getString("Port")+";";
//System.out.print(xd);
//System.out.println();
more=rs.next();
}
rs.close();
}
catch(Exception e)
{
System.out.println(e.getMessage());
}
return xd;
}
//请求匿名
//查找在线端口的记录,以字符串形式返回端口号
public String findRecord2()
{
String url="jdbc:odbc:Trustee";
String username="";
String password="";
String tt2="";
try{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
connection=DriverManager.getConnection(url,username,password);
}
catch (ClassNotFoundException cnfex){
System.err.println("Failed to load JDBC/ODBC driver.");
cnfex.printStackTrace();
System.exit(1);
}
catch (SQLException sqlex){
System.err.print("Unable to connect");
sqlex.printStackTrace();
}
ResultSet resultSet2;
try{
Statement statement2=connection.createStatement();
String query2="SELECT * FROM Trustee WHERE State=1";
resultSet2= statement2.executeQuery(query2);
//String query22="SELECT * FROM Trustee";
getTable(query2);
tt2=test2(resultSet2);
//System.out.print(tt2);
statement2.close();
}
catch (SQLException sqlex2) {
sqlex2.printStackTrace();
}
return tt2;
}
//findRecord2()要调用的方法,在这里获得数据
public String test2(ResultSet rst){
String xdt="";
String xdtt="";
try{
ResultSetMetaData rsmd = rst.getMetaData();
boolean more=rst.next();
while(more){
//for (int j=1;j<= rsmd.getColumnCount();++j)
//xdt=rst.getString(j)+" ";
xdt=rst.getString("Port");
//System.out.print(xdt);
xdtt=xdtt+xdt;
more=rst.next();
if(more)
xdtt=xdtt+";";
}
//System.out.print(xdtt);
rst.close();
}
catch(Exception e)
{
System.out.println(e.getMessage());
}
xdtt=xdtt.trim();
for(int i=0;i<3;i++)
System.out.println(xdtt);
return xdtt;
}
private void displayResultSet(ResultSet rs) throws SQLException
{
boolean moreRecords = rs.next();
if(!moreRecords){
JOptionPane.showMessageDialog(this,"ResultSet contained no records");
setTitle("No records to display");
return;
}
setTitle("Trustee Table");
Vector columnHeads = new Vector();
Vector rows = new Vector();
try{
//获得meta数据并赋给ResultSetMetaData,
//getMetaData的meta数据里描述了ResultSetMetaData的内容
ResultSetMetaData rsmd = rs.getMetaData();
for (int i=1;i<= rsmd.getColumnCount();++i)
columnHeads.addElement(rsmd.getColumnName(i));//得到列名
do{
rows.addElement(getNextRow(rs,rsmd));//包含了一行数据
}while(rs.next());
table=new JTable(rows,columnHeads);
JScrollPane scroller=new JScrollPane(table);
getContentPane().add(scroller,BorderLayout.CENTER);
validate();//保证当前库中表和索引位置的正确
}
catch (SQLException sqlex){
sqlex.printStackTrace();
}
}
//一行一行地加数据
private Vector getNextRow(ResultSet rs,ResultSetMetaData rsmd) throws SQLException
{
Vector currentRow = new Vector();
for (int i=1;i<=rsmd.getColumnCount();++i)
switch(rsmd.getColumnType(i)){
case Types.INTEGER://端口号
//getLong从Types.INTEGER的列中取回数据
currentRow.addElement(rs.getLong(i));
break;
case Types.SMALLINT://短整数,存储大小为 2 个字节,假名
currentRow.addElement(rs.getInt(i));
break;
case Types.BIT://状态
currentRow.addElement(rs.getByte(i));
break;
default:
System.out.println("Type was: "+rsmd.getColumnTypeName(i));
}
return currentRow;
}
public void shutDown()
{
try{
connection.close();
}
catch (SQLException sqlex){
System.err.println("Unable to disconnect");
sqlex.printStackTrace();
}
}
public static void main(String args[])
{
final TableDisplay app = new TableDisplay();
app.addWindowListener(
new WindowAdapter(){
public void windowClosing (WindowEvent e)
{
app.shutDown();
System.exit(0);
}
}
);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -