基本信息
源码名称:java 展示数据库中的数据 示例代码
源码大小:2.00KB
文件格式:.rar
开发语言:Java
更新时间:2016-12-05
   友情提示:(无需注册或充值,赞助后即可获取资源下载链接)

     嘿,亲!知识可是无价之宝呢,但咱这精心整理的资料也耗费了不少心血呀。小小地破费一下,绝对物超所值哦!如有下载和支付问题,请联系我们QQ(微信同号):813200300

本次赞助数额为: 4 元 
   源码介绍
本实例使用java语言,通过JBDC连接数据库。该实例简单,易懂,适合初学j者,学习怎样用java连接数据库,并将数据库中的数据展示到界面上来。


import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.util.*;
import java.sql.*;
public class shop extends JFrame implements ActionListener{
	String title[]={"产品序号","名字","制造时间","价格"};
	JTextField txtNum=new JTextField(8);
	JTextField txtName=new JTextField(10);
	JTextField txtMadetime=new JTextField(8);
	JTextField txtPrice=new JTextField(10);
	JButton next=new JButton("下一页");
	JButton prev=new JButton("上一页");
	JButton frist=new JButton("首页");
	JButton last=new JButton("尾页");
	Connection con;
	Statement stmt;//定义Statement类型的变量
	ResultSet rs;//定义Result类型的变量,用来保存结果集
	public shop() {
		// TODO 自动生成的构造函数存根
		super("产品信息查看窗口");
		try{  
			Class.forName("org.apache.derby.jdbc.EmbeddedDriver");
	    }catch(Exception e) {
	         System.out.print(e);
	    }
		setSize(300,200);
		setLocation(100, 100);
		try{
			con=DriverManager.getConnection("jdbc:derby:D:/DateExample/shop;create=false");
			stmt=con.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_READ_ONLY);
			rs=stmt.executeQuery("select * from goods");//执行查询,返回结果集
			Container conn=getContentPane();
			conn.setLayout(new BorderLayout(0,4));//设置边界面,构造一个具有指定组件间距的边框布局
			JPanel p[]=new JPanel[4];
			for(int i=0;i<4;i  ){
				p[i]=new JPanel(new FlowLayout(FlowLayout.LEFT,10,0));
				p[i].add(new JLabel(title[i]));//将4个产品信息描述作为标签,分别放到数组中
			}
			p[0].add(txtNum);
			p[1].add(txtName);
			p[2].add(txtMadetime);
			p[3].add(txtPrice);
			JPanel p1=new JPanel(new GridLayout(4,1,0,8));
			for(int i=0;i<4;i  )//将4个JPanel面板组合添加到p1中
				p1.add(p[i]);
			JPanel p3=new JPanel();
			p3.add(prev);//将4个按钮组合到p3中
			p3.add(next);
			p3.add(frist);
			p3.add(last);
			conn.add(p1,"North");//将p1,p3放到合适的位置
			conn.add(p3,"South");
			next.addActionListener(this);//设置各按钮的聆听者
			prev.addActionListener(this);
			frist.addActionListener(this);
			last.addActionListener(this);
			rs.first();//使结果集的指针标志指向第一条记录
			loadDate();
		}catch(Exception e){
			e.printStackTrace();
		}
		setVisible(true);
		setDefaultCloseOperation(DISPOSE_ON_CLOSE);
	}
	//将相关信息显示在文本框,获取此 ResultSet 对象的当前行中指定列的值
	boolean loadDate() {
		// TODO 自动生成的方法存根
		try{
			txtNum.setText(rs.getString("number"));
			txtName.setText(rs.getString("name"));
			txtMadetime.setText(rs.getString("madetime"));
			txtPrice.setText(rs.getString("price"));
		}catch(SQLException e){
			e.printStackTrace();
			return false;
		}
		return true;
	}
	@Override
	public void actionPerformed(ActionEvent e) {
		// TODO 自动生成的方法存根
		try{
			if(e.getSource()==next){
				if(rs.next()==false)
					return;
				loadDate();
			}
			else if (e.getSource()==prev){
				if(rs.previous()==false)
					return;
				loadDate();
			}
			else if(e.getSource()==frist){
				rs.first();
				loadDate();
			}
			else if(e.getSource()==last) {
				rs.last();
				loadDate();
			}
		}catch(Exception ee){}
	}
	public static void main(String args[]){
		//提供一个关于新创建的 JFrame 是否应该具有当前外观为其提供的 Window 装饰(如边框、关闭窗口的小部件、标题等等)的提示。
		JFrame.setDefaultLookAndFeelDecorated(true);
		//根据指定名称、样式和磅值大小,创建一个新 Font
		Font font=new Font("JFrame",Font.PLAIN,14);
		//说明了如何将外观设置为系统外观
		Enumeration keys=UIManager.getLookAndFeelDefaults().keys();
		//测试此枚举是否包含更多的元素
		while(keys.hasMoreElements()){
			//如果此枚举对象至少还有一个可提供的元素,则返回此枚举的下一个元素。
			Object key=keys.nextElement();
			if(UIManager.get(key)instanceof Font)
				UIManager.put(keys, font);
		}
		shop mainFrame=new shop();
	}
}