package one;
import java.awt.Font;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.SwingConstants;
public class demo_dm extends JFrame implements ActionListener{
private static final long serialVersionUID = 1L;
private JButton but ;
private JButton butt ;
private JPanel jp;
private JPanel jps;
private JLabel label;
private JLabel show;
private JButton reset;
private List<String> list = new ArrayList<String>();
private List<String> les=new ArrayList<String>();
private String le = null;
public Connection conn=null;
Random ran;
private String strFilename="G:\\qq.txt";
//一些可变动的变量可以独立设置,便于修改,而不至于改动一个就需要改大部分代码
public static void main(String[] args) throws IOException {
new demo_dm();
}
demo_dm() throws IOException{
init();
initListDate();
}
public void init(){
//初始化swing框架
JFrame jf=new JFrame();
jp=new JPanel();
jf.getContentPane().add(jp);
jps=new JPanel();
jf.getContentPane().add(jps);
jf.setLayout(new GridLayout(2,5,5,5));
jp.setLayout(new GridLayout(2,5,5,5));
jps.setLayout(new GridLayout(1,3,5,5));
but = new JButton("开始点名");
but.addActionListener(this);
butt = new JButton("缺席");
butt.addActionListener(this);
label = new JLabel("随机点名",SwingConstants.CENTER);
label.setFont(new Font("楷体",Font.BOLD,40));
show = new JLabel("",SwingConstants.CENTER);
show.setFont(new Font("楷体",Font.BOLD,30));
reset = new JButton("重新点名");
reset.addActionListener(this);
jp.add(label);
jp.add(show);
jps.add(but);
jps.add(butt);
jps.add(reset);
jf.setVisible(true);
jf.setResizable(true);
jf.setBounds(100,100,550,350);
jf.setTitle("点名");
jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public Connection getConnection(){
try {
Class.forName("com.mysql.jdbc.Driver");
String url="jdbc:mysql://localhost:3306/wkk";//这个数据库的database是自己创建的
String user="root";
String password="";
conn=DriverManager.getConnection(url,user,password);
if(conn!=null)
{System.out.println("数据库连接成功!!");}
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}
return conn;
}
public void serchdatas(){
String sql="select * from java;";
ResultSet rs=null;
try {
Statement stas=conn.createStatement();
rs=stas.executeQuery(sql);
while(rs.next()){
list.add(rs.getString("id")+","+rs.getString("name")+
","+rs.getString("class"));
}
} catch (SQLException e) {
e.printStackTrace();
}
}
public void initListDate() throws IOException{
//初始化导入的数据
getConnection();
serchdatas();
}
/**
* 传入文件名以及字符串, 将字符串信息保存到文件中
*
* @param strFilename
* @param strBuffer
*/
public void TextToFile( String strBuffer)
{
try
{
// 创建文件对象
File fileText = new File(strFilename);
if(!fileText.exists())
{fileText.createNewFile();}
// 向文件写入对象写入信息
FileWriter fileWriter = new FileWriter(fileText);
// 写文件
fileWriter.write(strBuffer);
// 关闭
fileWriter.close();
}
catch (IOException e)
{
//
e.printStackTrace();
}
}
public void actionPerformed(ActionEvent e){
/*
* */
Object obj = e.getSource();
if(obj == but){
if(list.size()==0){
JOptionPane.showMessageDialog(null,"没有人了!");
return ;
}else{
ran = new Random();
int num = ran.nextInt(list.size());
String ss=list.get(num);
le=ss;//将数据传给字符串le然后显示在缺席按钮上
les.add(ss+";");//将数据传给链表,然后再写入到txt文本中
// num++;
show.setText(ss);
list.remove(ss);
}
}
if(obj==butt){
// for(int i=0;i<num;i++){
TextToFile(les.toString());
show.setText(le+"缺席!");
}
if(obj == reset){
show.setText(" ");
les.clear();//清空les链表,然后将空表传给文本
TextToFile(les.toString());
try {
initListDate();
} catch (IOException e1) {
e1.printStackTrace();
}
}
}
}
import java.awt.Font;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.SwingConstants;
public class demo_dm extends JFrame implements ActionListener{
private static final long serialVersionUID = 1L;
private JButton but ;
private JButton butt ;
private JPanel jp;
private JPanel jps;
private JLabel label;
private JLabel show;
private JButton reset;
private List<String> list = new ArrayList<String>();
private List<String> les=new ArrayList<String>();
private String le = null;
public Connection conn=null;
Random ran;
private String strFilename="G:\\qq.txt";
//一些可变动的变量可以独立设置,便于修改,而不至于改动一个就需要改大部分代码
public static void main(String[] args) throws IOException {
new demo_dm();
}
demo_dm() throws IOException{
init();
initListDate();
}
public void init(){
//初始化swing框架
JFrame jf=new JFrame();
jp=new JPanel();
jf.getContentPane().add(jp);
jps=new JPanel();
jf.getContentPane().add(jps);
jf.setLayout(new GridLayout(2,5,5,5));
jp.setLayout(new GridLayout(2,5,5,5));
jps.setLayout(new GridLayout(1,3,5,5));
but = new JButton("开始点名");
but.addActionListener(this);
butt = new JButton("缺席");
butt.addActionListener(this);
label = new JLabel("随机点名",SwingConstants.CENTER);
label.setFont(new Font("楷体",Font.BOLD,40));
show = new JLabel("",SwingConstants.CENTER);
show.setFont(new Font("楷体",Font.BOLD,30));
reset = new JButton("重新点名");
reset.addActionListener(this);
jp.add(label);
jp.add(show);
jps.add(but);
jps.add(butt);
jps.add(reset);
jf.setVisible(true);
jf.setResizable(true);
jf.setBounds(100,100,550,350);
jf.setTitle("点名");
jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public Connection getConnection(){
try {
Class.forName("com.mysql.jdbc.Driver");
String url="jdbc:mysql://localhost:3306/wkk";//这个数据库的database是自己创建的
String user="root";
String password="";
conn=DriverManager.getConnection(url,user,password);
if(conn!=null)
{System.out.println("数据库连接成功!!");}
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}
return conn;
}
public void serchdatas(){
String sql="select * from java;";
ResultSet rs=null;
try {
Statement stas=conn.createStatement();
rs=stas.executeQuery(sql);
while(rs.next()){
list.add(rs.getString("id")+","+rs.getString("name")+
","+rs.getString("class"));
}
} catch (SQLException e) {
e.printStackTrace();
}
}
public void initListDate() throws IOException{
//初始化导入的数据
getConnection();
serchdatas();
}
/**
* 传入文件名以及字符串, 将字符串信息保存到文件中
*
* @param strFilename
* @param strBuffer
*/
public void TextToFile( String strBuffer)
{
try
{
// 创建文件对象
File fileText = new File(strFilename);
if(!fileText.exists())
{fileText.createNewFile();}
// 向文件写入对象写入信息
FileWriter fileWriter = new FileWriter(fileText);
// 写文件
fileWriter.write(strBuffer);
// 关闭
fileWriter.close();
}
catch (IOException e)
{
//
e.printStackTrace();
}
}
public void actionPerformed(ActionEvent e){
/*
* */
Object obj = e.getSource();
if(obj == but){
if(list.size()==0){
JOptionPane.showMessageDialog(null,"没有人了!");
return ;
}else{
ran = new Random();
int num = ran.nextInt(list.size());
String ss=list.get(num);
le=ss;//将数据传给字符串le然后显示在缺席按钮上
les.add(ss+";");//将数据传给链表,然后再写入到txt文本中
// num++;
show.setText(ss);
list.remove(ss);
}
}
if(obj==butt){
// for(int i=0;i<num;i++){
TextToFile(les.toString());
show.setText(le+"缺席!");
}
if(obj == reset){
show.setText(" ");
les.clear();//清空les链表,然后将空表传给文本
TextToFile(les.toString());
try {
initListDate();
} catch (IOException e1) {
e1.printStackTrace();
}
}
}
}
/*基本思路:1.将数据导入数据库,然后运用java连接数据库,运用查询语句将其存入到String类型的list中
2,初始化导入数据,然后再初始化swing,布局相关窗体,添加监听事件
3,将初始化函数加入构造函数中,当类实例化就执行所有初始化方法
缺陷:导入到Txt中没办法换行,导致数据一行一行存放,显示很混乱
还可以加:缺席次数,放到另一个文本中,
*/