下面是一个实例:也许对你有用 命名为Bookvalency.java
成都创新互联主营寻乌网站建设的网络公司,主营网站建设方案,成都App定制开发,寻乌h5小程序开发搭建,寻乌网站营销推广欢迎寻乌等地区企业咨询
import java.awt.*;
import javax.swing.*;
public class Bookvalency extends JFrame{
private JTable jtblBooks; //定义表格对象
//定义表格结构中的各字段名称
private String[] structureFields={"书籍名称","作者","书籍类型","定价"};
//定义表格每行的数据记录
private Object[][] dataRecords={
{"战争与和平","列夫.托尔斯泰","小说",new Float(135.5F)},
{"沙与沫","纪伯伦","诗集",new Float(32.0F)},
{"未来时速","比尔.盖茨","随想",new Float(41.5F)}
};
public Bookvalency(String title) {
super(title);
//创建表格实例对象
jtblBooks=new JTable(dataRecords,structureFields);
//设置表格的前景色与背景色
jtblBooks.setForeground(Color.BLUE);
jtblBooks.setBackground(Color.YELLOW);
//将表格对象转入一个滚动窗格内
JScrollPane jscpTable4Books=new JScrollPane(jtblBooks);
//获取窗口的内容窗格对象
Container contentPane=this.getContentPane();
//将滚动窗格添加到窗口的内容窗格中
contentPane.add(jscpTable4Books,BorderLayout.CENTER);
//设置窗口的关闭操作行为
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
//主方法
public static void main(String[] args) {
Bookvalency app=new Bookvalency("JTable Usage Demo");
app.setSize(300,100);
app.setVisible(true);
}
}
最近好多人要这份源码啊感觉..有建表脚本..使用mysql数据库..建表之后就可以直接运行了.
/
用户登陆数据库代码:
import java.sql.*;
class Database {
Connection con;
ResultSet rs;
Statement stmt;
public Database() {
try {
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
//
加载
JDBC-ODBC
桥驱动程序
String url = "jdbcdbc:HDB";
con = DriverManager.getConnection(url);
//
连接数据库
HDB //stmt
提供一个创建
SQL
查询、执行查询、得到返回结果
的空间
stmt =
// con.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,
// ResultSet.CONCUR_READ_ONLY);
} catch (Exception ex) {
System.out.println(ex);
}
}
/*
根据用户名,获取密码
*/
public String getPswd(String name) {
try {
// rs
为执行
SQL
语句所查询的结果赋给结果集对象
rs
rs = stmt.executeQuery("SELECT pswd FROM User WHERE Name = '"
+ name + "'");
rs.last();
//
把
rs
的指针移到最后一行
if (rs.getRow() == 0) {
return null;
} else {
String pswdDB = rs.getString("Pswd");
//
获取
rs
结果集中的
pswd
列的数据
return pswdDB;
}
} catch (Exception e) {
System.out.println(e);
return null;
}
}
/*
增加一行用户名、密码数据
*/
public boolean insertData(String name, String pswd) {
try {
String s = getPswd(name);
if (s == null) {
int rtn = stmt.executeUpdate("INSERT INTO User VALUES('" + name
+ "','" + pswd + "')");
if (rtn != 0)
return true;
} else {
return false;
}
return false;
} catch (Exception et) {
System.out.println(et);
return false;
}
}
}
//
进库数据库代码:
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
class BookDatabase {
Connection con;
ResultSet rs;
Statement stmt;
public BookDatabase() {
try {
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
//
加载
JDBC-ODBC
桥驱动程序
String url = "jdbcdbc:Book";
con = DriverManager.getConnection(url);
//
连接数据库
HDB //stmt
提供一个创建
SQL
查询、执行查询、得到返回结果
的空间
stmt = con.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,
ResultSet.CONCUR_READ_ONLY);
} catch (Exception ex) {
System.out.println(ex);
}
}
/*
增加一行用户名、密码数据
*/
public boolean insertData(String id, String name, String where, String
price, String date) {
try {
int rtn = stmt.executeUpdate("INSERT INTO Book VALUES('" + id
+ "','"
+ name + "','" + where + "','" + price + "','" + date
+ "')");
if (rtn != 0) {
return true;
} else {
return false;
}
} catch (Exception et) {
System.out.println(et);
return false;
}
}
/*
* public boolean deleteData(String id){ try{ int rtn =
* stmt.executeUpdate("DELETE FROM Book WHERE id=001" ); if( rtn != 0 ){
* return true; } else{ return false; } } catch(Exception e){
* System.out.println( e ) return false; } } public boolean Select(String
* id){ try{ int rtn = stmt.executeUpdate("SELECT * FROM BookIn WHERE
* id='"+id+"'");
*
* if( rtn != 0 ){ return true; } else{ return false; } } catch(Exception
* e){ System.out.println( e ) return false; } }
*/
}
//
出库数据库代码:
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
java swing 登陆界面code
/*
* Login.java
*
* Created on __DATE__, __TIME__
*/
package com.agen.library.window;
import java.awt.Image;
import java.awt.Toolkit;
import javax.swing.JOptionPane;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import com.agen.library.factory.DAOFactory;
import com.agen.library.util.GlobalUser;
import com.agen.library.vo.User;
/**
*
* @author __USER__
*/
public class Login extends javax.swing.JFrame {
/**
*
*/
private static final long serialVersionUID = -2176093732040600809L;
/** Creates new form Login */
public Login() {
super("易云图书管理软件V1.0");
Image ime = Toolkit.getDefaultToolkit().getImage(
getClass().getResource("/images/ico.png"));
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (InstantiationException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalAccessException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (UnsupportedLookAndFeelException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
setIconImage(ime);
initComponents();
setLocationRelativeTo(null);
this.setResizable(false);
}
/**
* This method is called from within the constructor to initialize the form.
* WARNING: Do NOT modify this code. The content of this method is always
* regenerated by the Form Editor.
*/
// GEN-BEGIN:initComponents
// editor-fold defaultstate="collapsed" desc="Generated Code"
private void initComponents() {
jLabel2 = new javax.swing.JLabel();
jLabel3 = new javax.swing.JLabel();
jTextField1 = new javax.swing.JTextField();
jPasswordField1 = new javax.swing.JPasswordField();
jButton1 = new javax.swing.JButton();
jButton2 = new javax.swing.JButton();
jLabel1 = new javax.swing.JLabel();
jMenuBar1 = new javax.swing.JMenuBar();
jMenu1 = new javax.swing.JMenu();
jMenuItem1 = new javax.swing.JMenuItem();
jMenu2 = new javax.swing.JMenu();
jMenuItem2 = new javax.swing.JMenuItem();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
addKeyListener(new java.awt.event.KeyAdapter() {
public void keyPressed(java.awt.event.KeyEvent evt) {
formKeyPressed(evt);
}
public void keyTyped(java.awt.event.KeyEvent evt) {
formKeyTyped(evt);
}
});
jLabel2.setFont(new java.awt.Font("微软雅黑", 0, 14));
jLabel2.setText("\u7528\u6237\u540d\uff1a");
jLabel3.setFont(new java.awt.Font("微软雅黑", 0, 14));
jLabel3.setText("\u5bc6 \u7801\uff1a");
jTextField1.setFont(new java.awt.Font("微软雅黑", 0, 14));
jTextField1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jTextField1ActionPerformed(evt);
}
});
jPasswordField1.setFont(new java.awt.Font("微软雅黑", 0, 12));
jPasswordField1.addKeyListener(new java.awt.event.KeyAdapter() {
public void keyTyped(java.awt.event.KeyEvent evt) {
jPasswordField1KeyTyped(evt);
}
});
jButton1.setBackground(new java.awt.Color(223, 216, 216));
jButton1.setFont(new java.awt.Font("微软雅黑", 0, 14));
jButton1.setText("\u767b\u9646");
jButton1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton1ActionPerformed(evt);
}
});
jButton2.setBackground(new java.awt.Color(223, 216, 216));
jButton2.setFont(new java.awt.Font("微软雅黑", 0, 14));
jButton2.setText("\u53d6\u6d88");
jButton2.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton2ActionPerformed(evt);
}
});
jLabel1.setIcon(new javax.swing.ImageIcon(
getClass().getResource("/images/login_main.jpg"))); // NOI18N
jMenu1.setText("File");
jMenu1.setFont(new java.awt.Font("微软雅黑", 0, 14));
jMenuItem1.setAccelerator(javax.swing.KeyStroke.getKeyStroke(
java.awt.event.KeyEvent.VK_Q,
java.awt.event.InputEvent.CTRL_MASK));
jMenuItem1.setText("Exit");
jMenuItem1.addMouseListener(new java.awt.event.MouseAdapter() {
public void mouseClicked(java.awt.event.MouseEvent evt) {
jMenuItem1MouseClicked(evt);
}
});
jMenuItem1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jMenuItem1ActionPerformed(evt);
}
});
jMenu1.add(jMenuItem1);
jMenuBar1.add(jMenu1);
jMenu2.setText("Help");
jMenu2.setFont(new java.awt.Font("微软雅黑", 0, 14));
jMenuItem2.setText("About");
jMenuItem2.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jMenuItem2ActionPerformed(evt);
}
});
jMenu2.add(jMenuItem2);
jMenuBar1.add(jMenu2);
setJMenuBar(jMenuBar1);
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(
getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(layout
.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jLabel1)
.addGroup(
layout.createSequentialGroup()
.addContainerGap()
.addGroup(
layout.createParallelGroup(
javax.swing.GroupLayout.Alignment.TRAILING,
false)
.addComponent(
jLabel2,
javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(
javax.swing.GroupLayout.Alignment.LEADING,
layout.createSequentialGroup()
.addComponent(
jLabel3)
.addPreferredGap(
javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(
layout.createParallelGroup(
javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(
layout.createSequentialGroup()
.addGap(10,
10,
10)
.addComponent(
jButton1)
.addGap(47,
47,
47)
.addComponent(
jButton2))
.addComponent(
jPasswordField1)
.addComponent(
jTextField1,
javax.swing.GroupLayout.DEFAULT_SIZE,
197,
Short.MAX_VALUE))
.addContainerGap()))));
layout.setVerticalGroup(layout
.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(
layout.createSequentialGroup()
.addComponent(jLabel1)
.addPreferredGap(
javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(
layout.createParallelGroup(
javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jLabel2)
.addComponent(
jTextField1,
javax.swing.GroupLayout.PREFERRED_SIZE,
24,
javax.swing.GroupLayout.PREFERRED_SIZE))
.addPreferredGap(
javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(
layout.createParallelGroup(
javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jLabel3)
.addComponent(
jPasswordField1,
javax.swing.GroupLayout.PREFERRED_SIZE,
23,
javax.swing.GroupLayout.PREFERRED_SIZE))
.addPreferredGap(
javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(
layout.createParallelGroup(
javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jButton1)
.addComponent(jButton2))
.addContainerGap(
javax.swing.GroupLayout.DEFAULT_SIZE,
Short.MAX_VALUE)));
pack();
}// /editor-fold
// GEN-END:initComponents
private void jMenuItem2ActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
About.start();
}
private void jPasswordField1KeyTyped(java.awt.event.KeyEvent evt) {
if (evt.getKeyChar() == '\n') {
String name = jTextField1.getText(); // 获取用户名
String pass = String.valueOf(jPasswordField1.getPassword());// 获取密码
User user = null;
// 未输入用户名
if (name.equals("") || name == null) {
JOptionPane.showMessageDialog(this, "用户名不允许为空!", "cuowu", 0);
return;
}
try {
user = DAOFactory.getIUserDAOInstance().findById(name);
if (user != null) {
if (user.getPass() != null user.getPass().equals(pass)) {
GlobalUser.LOGIN_USER = user; // 记录当前用户
// 进入主界面
Main.start();
this.dispose();
} else {
JOptionPane.showMessageDialog(this, "用户名或密码错误!");
return;
}
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
if (user == null) {
JOptionPane.showMessageDialog(this, "用户名或密码错误!", "消息", 0);
return;
}
}
}
private void formKeyTyped(java.awt.event.KeyEvent evt) {
}
private void formKeyPressed(java.awt.event.KeyEvent evt) {
}
private void jTextField1ActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
}
private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
jTextField1.setText("");
jPasswordField1.setText("");
jTextField1.requestFocus();
}
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
String name = jTextField1.getText(); // 获取用户名
String pass = String.valueOf(jPasswordField1.getPassword());// 获取密码
User user = null;
// 未输入用户名
if (name.equals("")) {
JOptionPane.showMessageDialog(this, "用户名不允许为空!");
return;
}
try {
user = DAOFactory.getIUserDAOInstance().findById(name);
if (user != null) {
if (user.getPass() != null user.getPass().equals(pass)) {
GlobalUser.LOGIN_USER = user; // 记录当前用户
// 进入主界面
Main.start();
this.dispose();
} else {
JOptionPane.showMessageDialog(this, "用户名或密码错误!", "消息", 0);
return;
}
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
if (user == null) {
JOptionPane.showMessageDialog(this, "用户名或密码错误!", "消息", 0);
return;
}
}
private void jMenuItem1ActionPerformed(java.awt.event.ActionEvent evt) {
if (JOptionPane.showConfirmDialog(this, "你确定要退出吗?", "提示",
JOptionPane.YES_NO_OPTION) == JOptionPane.YES_OPTION) {
System.exit(0);
}
}
private void jMenuItem1MouseClicked(java.awt.event.MouseEvent evt) {
System.exit(1);
}
/**
* @param args
* the command line arguments
*/
public static void main(String args[]) {
// System.out.println(Login.class.getResource("src/images/images/login_main.jpg"));
// new javax.swing.ImageIcon(
// Login.class.getResource("../../../../images/login_main.jpg"));
// new Login().setVisible(true);
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new Login().setVisible(true);
}
});
}
// GEN-BEGIN:variables
// Variables declaration - do not modify
private javax.swing.JButton jButton1;
private javax.swing.JButton jButton2;
private javax.swing.JLabel jLabel1;
private javax.swing.JLabel jLabel2;
private javax.swing.JLabel jLabel3;
private javax.swing.JMenu jMenu1;
private javax.swing.JMenu jMenu2;
private javax.swing.JMenuBar jMenuBar1;
private javax.swing.JMenuItem jMenuItem1;
private javax.swing.JMenuItem jMenuItem2;
private javax.swing.JPasswordField jPasswordField1;
private javax.swing.JTextField jTextField1;
// End of variables declaration//GEN-END:variables
}
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
public class BookWork {
static ListBook data;
private static Scanner input;
public static void main(String[] args) {
if (!initBook("d:/book.txt")) {
System.out.println("初始图书列表失败 ..");
return;
}
input = new Scanner(System.in);
while (true) {
try {
System.out.println("请输入操作:");
System.out.println("1.找书 2.删除图书 3.退出");
int number = Integer.parseInt(input.next());
if (number == 1) {
findBook();
} else if (number == 2) {
delBook();
} else if (number == 3) {
System.out.println("退出");
break;
} else {
System.out.println("这个不是我要的...重来...");
System.out.println();
}
} catch (Exception e) {
e.printStackTrace();
System.out.println("这个不是我要的...重来...");
System.out.println();
}
}
}
private static void delBook() {
System.out.println("请输入要删除的书名或编号:");
String key = input.next();
if (key != null !key.equals("")) {
for (Book book : data) {
if (book.number.equals(key) || book.name.contains(key)) {
data.remove(book);
System.out.println(" 图书 " + book.toString() + " 已删除");
return;
}
}
}
System.out.println("没有您要删除的");
}
private static void findBook() {
System.out.println("请输入要查找的书名或编号:");
String key = input.next();
if (key != null !key.equals("")) {
for (Book book : data) {
if (book.number.equals(key) || book.name.contains(key)) {
System.out.println("找到了 图书 " + book.toString());
return;
}
}
}
System.out.println("没有您要找的");
}
private static boolean initBook(String string) {
try {
System.out.println("图书导入中...");
System.out.println("列表文件 -- " + string);
File file = new File(string);
if (!file.exists()) {
return false;
}
data = new ArrayListBook();
BufferedReader bufferedReader = new BufferedReader(new FileReader(file));
String line = "";
while ((line = bufferedReader.readLine()) != null) {
String[] strings = line.split(",");
Book b = new Book(strings[0], strings[1]);
data.add(b);
System.out.println("导入" + b.toString());
}
} catch (Exception e) {
e.printStackTrace();
return false;
}
return true;
}
public static class Book {
String number;
String name;
public Book(String number, String name) {
super();
this.number = number;
this.name = name;
}
@Override
public String toString() {
return "Book [编码:" + number + ", 名称:" + name + "]";
}
}
}
001,金瓶梅
002,杂事秘辛
003,飞燕外传
004,控鹤监秘记
005,汉宫春色