如何将数据库的内容用JAVA的JTable显示出来。

如何将数据库的内容用JAVA的JTable显示出来。,第1张

//package item;

import javaawtDimension;

import javaawtHeadlessException;

import javaawtPoint;

import javaxswingJButton;

import javaxswingJFrame;

import javaxswingJOptionPane;

import javaxswingJPanel;

import javaxswingJScrollPane;

import javaxswingSwingUtilities;

import javaxswingJTable;

import javaxswingtableDefaultTableModel;

import javaxxmlcryptoData;

//import dbConn;

import javaawtRectangle;

import javasqlResultSet;

import javasqlSQLException;

import javautilVector;

public class Manager extends JFrame {

String xnamearr[]={"课程编号","课程名称","开课时间"};

DefaultTableModel model;

private static final long serialVersionUID = 1L;

private JPanel jContentPane = null;

private JButton jButton = null;

private JButton jButton1 = null;

private JButton jButton2 = null;

private JTable jTable = null;

private JButton getJButton() {

if (jButton == null) {

jButton = new JButton();

jButtonsetLocation(new Point(20, 0));

jButtonsetText("修改信息");

jButtonsetSize(new Dimension(100, 30));

}

return jButton;

}

private JButton getJButton1() {

if (jButton1 == null) {

jButton1 = new JButton();

jButton1setLocation(new Point(140, 0));

jButton1setText("删除信息");

jButton1setSize(new Dimension(100, 30));

}

return jButton1;

}

private JButton getJButton2() {

if (jButton2 == null) {

jButton2 = new JButton();

jButton2setLocation(new Point(260, 0));

jButton2setText("显示全部");

jButton2setSize(new Dimension(100, 30));

}

return jButton2;

}

// private void Debark() {

// Conn conn=null;

// ResultSet rs=null;

// String sql=null;

// conn=new Conn();

// sql= "select from message where =";

// rs=connexecuteQuery(sql);

//

// try {

// if (rsnext())

// {

//

// }

// else

// JOptionPaneshowMessageDialog(null,"无对应信息");

// } catch (HeadlessException e) {

// eprintStackTrace();

// } catch (SQLException e) {

// eprintStackTrace();

// }

// }

private JTable getJTable() {

if (jTable == null) {

model=new DefaultTableModel();

jTable = new JTable(model);

modelsetDataVector(getVector(), getVe());//model 传入的是数据的二维数组 和列的数组!

// jTablesetBounds(new Rectangle(17, 97, 341, 82));

}

return jTable;

}

private JScrollPane getscol(){

JScrollPane sco=new JScrollPane(getJTable());

scosetBounds(new Rectangle(17, 97, 341, 82));

return sco;

}

public Vector getVe(){

Vector ve=new Vector();

for (int i = 0; i < xnamearrlength; i++) {

veadd(xnamearr[i]);

}

return ve;

}

private Vector getVector(){

Vector getVector=new Vector();

getVectoradd(getVe());

return getVector;

}

public static void main(String[] args) {

// TODO 自动生成方法存根

SwingUtilitiesinvokeLater(new Runnable() {

public void run() {

Manager thisClass = new Manager();

thisClasssetDefaultCloseOperation(JFrameEXIT_ON_CLOSE);

thisClasssetVisible(true);

}

});

}

public Manager() {

super();

initialize();

}

private void initialize() {

thissetSize(400, 300);

thissetContentPane(getJContentPane());

thissetTitle("管理员系统");

}

private JPanel getJContentPane() {

if (jContentPane == null) {

jContentPane = new JPanel();

jContentPanesetLayout(null);

jContentPaneadd(getJButton(), null);

jContentPaneadd(getJButton1(), null);

jContentPaneadd(getJButton2(), null);

jContentPaneadd(getscol(), null);

}

return jContentPane;

}

}

//只需要调用modelsetDataVector(传入数据库的二维数组,以及对应的列名称一维数组就可以了);

//如果实现按钮功能只需要做一个model的 get方法然后通过监听实现数据的变更

//只要你能正确获取数据就很简单了

//记得把我注释的部分取消

select 表1姓名,表1学号,这里也都得加上限定 一月总成绩,二月总成绩,三月总成绩。。。全年各月平均分 from 表1 inner join 表2 on 表1学号=表2学号 inner join 表3 on 表1学号=表3学号

使用lablesettext("str");将查询出来的数据放进jllabe。

1,给查询按钮一个监听事件,点击触发。

2,连接数据库进行查询 *** 作,返回rs结果集。

3,获取rs结果集中的数据,例如获取表中字段为name的数据:string

name

=

rsgetstring("name")。

4,将数据插入到jlable。name_lablesettext(name);

5,刷新界面显示数据。framesetvisible(true);

查看一下SQL Server的日志不就可以了:)

进入企业管理器,你的数据库点开,"管理">>"SQL Server日志",查看你那天的存档就可以了。

唉,我可没有说右击阿:)

你点开你的数据库,下面不是有一些选项么,"数据库","数据转换服务","管理"就是这个管理阿。

1提取单条记录

//importjavasql;

Connectionconn=null;

Statementstmt=null;

ResultSetrs=null;

try{

ClassforName("sunjdbcodbc");

Stringurl="jdbc:odbc:%%1";

con=(url,%%2,%%3);

stmt=conn();

stmt(%%4);

rs=stmt(%%5);

}catch(Exceptione){

e();

}

finally{

try{

if(rs!=null)

rsclose();

if(stmt!=null)

stmtclose();

if(conn!=null)

connclose();

}catch(e){

e();

}

}

3显示表格

/

importjavaawt;

importjavaxswing;

importjavasql;

importjavaxswingtable;

String[]colHeads=%%4;

Connectionconn=null;

Statementstmt=null;

ResultSetrs=null;

try{

ClassforName("sunjdbcodbc");

Stringurl="jdbc:odbc:%%1";

conn=(url,%%2,%%3);

stmt=conn();

rs=stmt("SELECTcount()asau_countfrom"%%5);

rsnext();

intiCount=rsgetInt("au_count");

Object[][]data=newObject[iCount][];

inti=0;

rs=stmt("SELECTfrom"%%5);

while(rsnext()){

data[i]=newObject[iCount];

data[i][0]=rsgetString("au_fname");

data[i][1]=rsgetString("Phone");

data[i][2]=rsgetString("City");

i;

}

JTabletable=newJTable(data,colHeads);

JScrollPanejsp=newJScrollPane(table);

()add(jsp);

}catch(Exceptione){

eprintStackTrace();

}

finally{

try{

if(rs!=null)

rsclose();

if(stmt!=null)

stmtclose();

if(conn!=null)

connclose();

}catch(SQLExceptione){

eprintStackTrace();

}

}

6关闭时关闭连接

//importjavasql;

addWindowListener(newWindowAdapter{

publicvoidwindowClosing(WindowEventwevent){

if(stmt!=null){

try{

if(rs!=null)

rsclose();

if(stmt!=null)

stmtclose();

if(conn!=null)

connclose();

}catch(SQLExceptione){

eprintStackTrace();

}

}

7执行命令

//importjavasql;

Connectionconn=null;

PreparedStatementpst=null;

try{

conn=DriverManagergetConnection(url);

pst=connprepareStatement("InsertIntograde(%%1)Values()");

pstsetInt(1,%%2);

//pstsetString(2,%%2);

pstaddBatch();

pstexecuteBatch();

}catch(SQLExceptione){

eprintStackTrace();

}

finally{

try{

if(pst!=null)

pstclose();

if(conn!=null)

connclose();

}catch(SQLExceptione){

//TODOAuto-generatedcatchblock

eprintStackTrace();

}

}

你数据库会连接吗?假设你会,参照下面的代码:

private static Connection getConnection()

{

//这里是连接数据库的代码,返回一个Connection对象。要按实际的数据库写。

}

…………

Connection conn=getConnection();

DefaultTableModel model=new DefaultTableModel();

JTable jTable=new JTable();

jTablesetModel(model);

try

{

Statement st=conncreateStatement();

String sql="select from user";//假设要读的数据库表为user

ResultSet results=stexecuteQuery(sql);

ResultSetMetaData metaData=resultsgetMetaData();

String[] headerData=new String[metaDatagetColumnCount()];

for(int i=1;i<metaDatagetColumnCount();i++)

{

headerData[i-1]=metaDatagetColumnName(i);

modeladdColumn(headerData[i-1]);

}

while(resultsnext())//假设表结构是这样的:id int,name varchar(12)

{

int id=resultsgetInt("id");

String name=resultsgetString("name");

modeladdRow(new Object[]{id,name});

}

}

catch(Exception e){}

代码大致如上。

查询数据库的,返回的是一行一行的数据,举个例子:

id name sex

1 张三 男

2 李四 男

在读取数据库记录的时候:

Vector<Vector<Ojbect>> data = new Vector<Vector<Object>>();

while(rsnext())

{

Vector<Object> row = new Vector<Object>(); //一行数据

rowadd(rsgetInt("id"));

rowadd(rsgetString("name"));

rowadd(rsgetString("sex"));

dataadd(row); //添加一行数据

}

JTablesetModel(new DefaultTableModel(data,name));

public void inithavesold(){//建立查询已销售商品表格、模型

try {

Vector<String> columnName = new Vector<String>();//字段名

Vector<Vector<Object>> dataVector = new

Vector<Vector<Object>>(); //存储所有数据,里面每个小的Vector是存单行的

columnNameadd("编号");

columnNameadd("商品名");

columnNameadd("数量");

columnNameadd("价格");

columnNameadd("总盈利");

conn = DataBasegetconnection();

stmt = conncreateStatement();

rs = stmtexecuteQuery("select from havesold");

while(rsnext()){

Vector<Object> vec = new Vector<Object>();//就是这个存单行的,最后放到上面的大的Vector里面

for(int i=1;i<=5;i++){

vecadd(rsgetObject(i));

}

dataVectoradd(vec);

}

tmhavesold = new DefaultTableModel();//建立默认的JTable模型

tmhavesoldsetDataVector(dataVector, columnName);//设定模型数据和字段

DataBasefree(conn, stmt, rs);

} catch (SQLException e) {

eprintStackTrace();

}

jtablesetModel(tmhavesold);

}

以上就是关于如何将数据库的内容用JAVA的JTable显示出来。全部的内容,包括:如何将数据库的内容用JAVA的JTable显示出来。、请问如果我想在几个数据库表中各取出一列,组成一个表显示出来该怎么做呢最后还要用JTable显示出来、java 如何实现点击一个查询成绩按钮后将数据库里相对应的内容显示在JTable里面等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/sjk/10179717.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-05-06
下一篇 2023-05-06

发表评论

登录后才能评论

评论列表(0条)

保存