import javax.sql.RowSetMetaData;
import javax.swing.SwingUtilities;
import java.awt.BorderLayout;
import javax.swing.JPanel;
import javax.swing.JFrame;
import java.awt.Dimension;
import java.awt.Rectangle;
import java.awt.GridBagLayout;
import javax.swing.JLabel;
import java.awt.Font;
import java.awt.GridBagConstraints;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import javax.swing.JOptionPane;
import javax.swing.JTextField;
import javax.swing.JButton;
import javax.swing.JMenuBar;
import javax.swing.JMenu;
import javax.swing.JMenuItem;
import javax.swing.SwingConstants;
public class search extends JFrame {
private static final long serialVersionUID = 1L;
private JPanel jContentPane = null;
private JPanel jPanel = null;
private JPanel jPanel1 = null;
private JLabel jLabel = null;
private JTextField jTextField = null;
private JButton jButton = null;
private JMenuBar jJMenuBar = null;
private JMenu jMenu = null;
private JMenu jMenu1 = null;
private JMenu jMenu2 = null;
private JMenuItem jMenuItem = null;
private JMenuItem jMenuItem1 = null;
private JMenuItem jMenuItem2 = null;
private JMenuItem jMenuItem3 = null;
private JMenuItem jMenuItem4 = null;
private JMenuItem jMenuItem5 = null;
private JLabel jLabel1 = null;
private JLabel jLabel2 = null;
private JLabel jLabel3 = null;
private JLabel jLabel4 = null;
private JLabel jLabel5 = null;
private JLabel jLabel6 = null;
private JLabel jLabel7 = null;
private JTextField jTextField1 = null;
private JTextField jTextField2 = null;
private JTextField jTextField3 = null;
private JTextField jTextField4 = null;
private JTextField jTextField5 = null;
private JTextField jTextField6 = null;
private JTextField jTextField7 = null;
private JButton jButton1 = null;
private JButton jButton2 = null;
private JButton jButton3 = null;
private JLabel jLabel8 = null;
private JLabel jLabel9 = null;
/**
* This method initializes jPanel
*
* @return javax.swing.JPanel
*/
private JPanel getJPanel() {
if (jPanel == null) {
jLabel9 = new JLabel();
jLabel9.setBounds(new Rectangle(349, 150, 82, 21));
jLabel9.setText("限选:男或女");
jLabel8 = new JLabel();
jLabel8.setBounds(new Rectangle(259, 150, 82, 21));
jLabel8.setText("如2007-07-05");
jLabel7 = new JLabel();
jLabel7.setBounds(new Rectangle(524, 36, 93, 43));
jLabel7.setHorizontalAlignment(SwingConstants.CENTER);
jLabel7.setText("用户说明(可选)");
jLabel6 = new JLabel();
jLabel6.setBounds(new Rectangle(437, 36, 82, 43));
jLabel6.setHorizontalAlignment(SwingConstants.CENTER);
jLabel6.setText("电话(可选)");
jLabel5 = new JLabel();
jLabel5.setBounds(new Rectangle(347, 36, 82, 43));
jLabel5.setHorizontalAlignment(SwingConstants.CENTER);
jLabel5.setText("性别(可选)");
jLabel4 = new JLabel();
jLabel4.setBounds(new Rectangle(258, 36, 82, 43));
jLabel4.setHorizontalAlignment(SwingConstants.CENTER);
jLabel4.setText("生日(必填)");
jLabel3 = new JLabel();
jLabel3.setBounds(new Rectangle(173, 36, 82, 43));
jLabel3.setHorizontalAlignment(SwingConstants.CENTER);
jLabel3.setText("权限(必填)");
jLabel2 = new JLabel();
jLabel2.setBounds(new Rectangle(87, 36, 82, 43));
jLabel2.setHorizontalTextPosition(SwingConstants.TRAILING);
jLabel2.setHorizontalAlignment(SwingConstants.CENTER);
jLabel2.setText("密码(必填)");
jLabel1 = new JLabel();
jLabel1.setBounds(new Rectangle(3, 36, 82, 43));
jLabel1.setHorizontalAlignment(SwingConstants.CENTER);
jLabel1.setText("用户名(必填)");
jPanel = new JPanel();
jPanel.setLayout(null);
jPanel.setBounds(new Rectangle(2, 152, 628, 179));
jPanel.add(jLabel1, null);
jPanel.add(jLabel2, null);
jPanel.add(jLabel3, null);
jPanel.add(jLabel4, null);
jPanel.add(jLabel5, null);
jPanel.add(jLabel6, null);
jPanel.add(jLabel7, null);
jPanel.add(getJTextField1(), null);
jPanel.add(getJTextField2(), null);
jPanel.add(getJTextField3(), null);
jPanel.add(getJTextField4(), null);
jPanel.add(getJTextField5(), null);
jPanel.add(getJTextField6(), null);
jPanel.add(getJTextField7(), null);
jPanel.add(jLabel8, null);
jPanel.add(jLabel9, null);
}
return jPanel;
}
/**
* This method initializes jPanel1
*
* @return javax.swing.JPanel
*/
private JPanel getJPanel1() {
if (jPanel1 == null) {
jPanel1 = new JPanel();
jPanel1.setLayout(null);
jPanel1.setBounds(new Rectangle(338, 10, 381, 91));
jPanel1.add(jLabel, null);
jPanel1.add(getJTextField(), null);
jPanel1.add(getJButton(), null);
}
return jPanel1;
}
/**
* This method initializes jTextField
*
* @return javax.swing.JTextField
*/
private JTextField getJTextField() {
if (jTextField == null) {
jTextField = new JTextField();
jTextField.setBounds(new Rectangle(137, 28, 142, 34));
}
return jTextField;
}
/**
* This method initializes jButton
*
* @return javax.swing.JButton
*/
private JButton getJButton() {
if (jButton == null) {
jButton = new JButton();
jButton.setBounds(new Rectangle(304, 20, 65, 51));
jButton.setText("查询");
jButton.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent e) {
System.out.println("actionPerformed()"); // TODO
// Auto-generated
// Event stub
// actionPerformed()
String uname = "";
String upass = "";
String upriv = "";
String ubirth = "";
String usex = "";
String utel = "";
String usay = "";
jTextField1.setText("");
jTextField2.setText("");
jTextField3.setText("");
jTextField4.setText("");
jTextField5.setText("");
jTextField6.setText("");
jTextField7.setText("");
uname=jTextField.getText();
// DBConnected a=new DBConnected();
DBConnected.connect();
String sql = "select * from T_USER where USERNAME='"
+ uname + "'";
ResultSet msg = DBConnected.getMsg(sql);
ResultSet res = msg;
// //
// //
try {
//String s=jTextField.getText();
if (res.next()) {//res有数据就执行 没有就不执行 到数据结尾
if(!res.getString(2).equals("")){System.out.println("1");
uname = res.getString(2);
upass = res.getString(3);
upriv = res.getString(4);
ubirth = res.getString(5);
usex = res.getString(6);
utel = res.getString(7);
usay = res.getString(8);
if(usex.equals("1")){jTextField5.setText("男");}
else if(usex.equals("2")){jTextField5.setText("女");}
else {jTextField5.setText("");}
jTextField1.setText("");
jTextField1.setText(uname);
jTextField2.setText("");
jTextField2.setText(upass);
jTextField3.setText("");
jTextField3.setText(upriv);
jTextField4.setText("");
if(ubirth.equals("")){ubirth="";}
else{
ubirth=ubirth.substring(0,10);
}
jTextField4.setText(ubirth);
//jTextField5.setText(usex);
//jTextField6.setText("");
jTextField6.setText(utel);
jTextField7.setText("");
jTextField7.setText(usay);
}
//
}
else
{
JOptionPane.showMessageDialog(null, "用户不存在");
jTextField.setText("");
}
//
//
} catch (SQLException e1) {
// // TODO Auto-generated catch block
e1.printStackTrace();
}
//
//
//
// //
// // uname=jTextField.getText();
// // upass=testdb.connectedb(uname,3);
// // upriv=testdb.connectedb(uname,4);
// // ubirth=testdb.connectedb(uname,5);
// // usex=testdb.connectedb(uname,6);
// // utel=testdb.connectedb(uname,7);
// // usay=testdb.connectedb(uname,8);
// // jTextField1.setText(uname);
// // jTextField2.setText(upass);
// // jTextField3.setText(upriv);
// // jTextField4.setText(ubirth);
// // jTextField5.setText(usex);
// // jTextField6.setText(utel);
// // jTextField7.setText(usay);
// //
// System.out.println("actionPerformed()");
// DBConnected.connect();
// System.out.print("ghg");
String sql2="select * from t_user ";
ResultSet msg2 = DBConnected.getMsg(sql2);
//
ResultSetMetaData rsmd = null;
//
//
//
try {
while(msg2.next())
//
{
rsmd = msg2.getMetaData();
for(int j=1;j<rsmd.getColumnCount();j++)
{
System.out.print(msg2.getString(j) + "/t");
}
System.out.println();
//
//
//
//
//
//
//
}
} catch (SQLException e1) {
// // TODO Auto-generated catch block
e1.printStackTrace();
}
//
}
}
);
}
return jButton;
}
/**
* This method initializes jJMenuBar
*
* @return javax.swing.JMenuBar
*/
private JMenuBar getJJMenuBar() {
if (jJMenuBar == null) {
jJMenuBar = new JMenuBar();
jJMenuBar.add(getJMenu());
jJMenuBar.add(getJMenu1());
jJMenuBar.add(getJMenu2());
}
return jJMenuBar;
}
/**
* This method initializes jMenu
*
* @return javax.swing.JMenu
*/
private JMenu getJMenu() {
if (jMenu == null) {
jMenu = new JMenu();
jMenu.setText("用户");
jMenu.add(getJMenuItem());
jMenu.add(getJMenuItem1());
}
return jMenu;
}
/**
* This method initializes jMenu1
*
* @return javax.swing.JMenu
*/
private JMenu getJMenu1() {
if (jMenu1 == null) {
jMenu1 = new JMenu();
jMenu1.setText("帮助");
jMenu1.add(getJMenuItem2());
jMenu1.add(getJMenuItem3());
}
return jMenu1;
}
/**
* This method initializes jMenu2
*
* @return javax.swing.JMenu
*/
private JMenu getJMenu2() {
if (jMenu2 == null) {
jMenu2 = new JMenu();
jMenu2.add(getJMenuItem4());
jMenu2.add(getJMenuItem5());
}
return jMenu2;
}
/**
* This method initializes jMenuItem
*
* @return javax.swing.JMenuItem
*/
private JMenuItem getJMenuItem() {
if (jMenuItem == null) {
jMenuItem = new JMenuItem();
jMenuItem.setText("登陆");
}
return jMenuItem;
}
/**
* This method initializes jMenuItem1
*
* @return javax.swing.JMenuItem
*/
private JMenuItem getJMenuItem1() {
if (jMenuItem1 == null) {
jMenuItem1 = new JMenuItem();
jMenuItem1.setText("退出");
}
return jMenuItem1;
}
/**
* This method initializes jMenuItem2
*
* @return javax.swing.JMenuItem
*/
private JMenuItem getJMenuItem2() {
if (jMenuItem2 == null) {
jMenuItem2 = new JMenuItem();
jMenuItem2.setText("有关帮助");
}
return jMenuItem2;
}
/**
* This method initializes jMenuItem3
*
* @return javax.swing.JMenuItem
*/
private JMenuItem getJMenuItem3() {
if (jMenuItem3 == null) {
jMenuItem3 = new JMenuItem();
jMenuItem3.setText("作者");
}
return jMenuItem3;
}
/**
* This method initializes jMenuItem4
*
* @return javax.swing.JMenuItem
*/
private JMenuItem getJMenuItem4() {
if (jMenuItem4 == null) {
jMenuItem4 = new JMenuItem();
}
return jMenuItem4;
}
/**
* This method initializes jMenuItem5
*
* @return javax.swing.JMenuItem
*/
private JMenuItem getJMenuItem5() {
if (jMenuItem5 == null) {
jMenuItem5 = new JMenuItem();
}
return jMenuItem5;
}
/**
* This method initializes jTextField1
*
* @return javax.swing.JTextField
*/
private JTextField getJTextField1() {
if (jTextField1 == null) {
jTextField1 = new JTextField();
jTextField1.setBounds(new Rectangle(3, 97, 82, 48));
}
return jTextField1;
}
/**
* This method initializes jTextField2
*
* @return javax.swing.JTextField
*/
private JTextField getJTextField2() {
if (jTextField2 == null) {
jTextField2 = new JTextField();
jTextField2.setBounds(new Rectangle(87, 97, 82, 48));
}
return jTextField2;
}
/**
* This method initializes jTextField3
*
* @return javax.swing.JTextField
*/
private JTextField getJTextField3() {
if (jTextField3 == null) {
jTextField3 = new JTextField();
jTextField3.setBounds(new Rectangle(173, 97, 82, 48));
}
return jTextField3;
}
/**
* This method initializes jTextField4
*
* @return javax.swing.JTextField
*/
private JTextField getJTextField4() {
if (jTextField4 == null) {
jTextField4 = new JTextField();
jTextField4.setBounds(new Rectangle(258, 97, 82, 48));
}
return jTextField4;
}
/**
* This method initializes jTextField5
*
* @return javax.swing.JTextField
*/
private JTextField getJTextField5() {
if (jTextField5 == null) {
jTextField5 = new JTextField();
jTextField5.setBounds(new Rectangle(347, 97, 82, 48));
}
return jTextField5;
}
/**
* This method initializes jTextField6
*
* @return javax.swing.JTextField
*/
private JTextField getJTextField6() {
if (jTextField6 == null) {
jTextField6 = new JTextField();
jTextField6.setBounds(new Rectangle(437, 97, 82, 48));
}
return jTextField6;
}
/**
* This method initializes jTextField7
*
* @return javax.swing.JTextField
*/
private JTextField getJTextField7() {
if (jTextField7 == null) {
jTextField7 = new JTextField();
jTextField7.setBounds(new Rectangle(524, 97, 93, 48));
}
return jTextField7;
}
/**
* This method initializes jButton1
*
* @return javax.swing.JButton
*/
private JButton getJButton1() {
if (jButton1 == null) {
jButton1 = new JButton();
jButton1.setBounds(new Rectangle(642, 122, 65, 51));
jButton1.setText("添加");
jButton1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent e) {
System.out.println("actionPerformed()"); // TODO Auto-generated Event stub actionPerformed()
String uname="";
String upass="";
String upriv="";
String ubirth="";
String usex="";
String utel="";
String usay="";
DBConnected.connect();
String sql="";
String uid="";
uname=jTextField1.getText();
upass=jTextField2.getText();
upriv=jTextField3.getText();
ubirth=jTextField4.getText();
usex=jTextField5.getText();
utel=jTextField6.getText();
usay=jTextField7.getText();
String una="select * from t_user where username='"+uname+"'";
String birth="to_date('"+ubirth+"','yyyy-mm-dd ')";
ResultSet res=null;
res=DBConnected.getMsg(una);
try {
if(res.next())
{
JOptionPane.showMessageDialog(null, "用户存在");
jTextField1.setText("");
jTextField2.setText("");
jTextField3.setText("");
jTextField4.setText("");
jTextField5.setText("");
jTextField6.setText("");
jTextField7.setText("");
}
else{
String sqluid="select max(id) from t_user ";
ResultSet uuid=DBConnected.getMsg(sqluid);
try {
if(uuid.next())
{
uid=uuid.getString(1);
int i=Integer.parseInt(uid);
i++;
uid=String.valueOf(i);
}
} catch (SQLException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
if(usex.equals("男")){usex="1";}
else if(usex.equals("女")){usex="2";}
else {usex="";}
sql="insert into t_user values("+uid+",'"+uname+"','"+upass+"',"+upriv+","+birth+",'"+usex+"','"
+utel+"','"+usay+"')";
DBConnected.commit(sql);
//ResultSet msg=DBConnected.getMsg(sql);
System.out.println(sql);
}
} catch (SQLException e2) {
e2.printStackTrace();
}
}
});
}
return jButton1;
}
/**
* This method initializes jButton2
*
* @return javax.swing.JButton
*/
private JButton getJButton2() {
if (jButton2 == null) {
jButton2 = new JButton();
jButton2.setBounds(new Rectangle(642, 187, 65, 51));
jButton2.setText("删除");
jButton2.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent e) {
System.out.println("actionPerformed()");
delete a=new delete();
a.setVisible(true);
}
});
}
return jButton2;
}
/**
* This method initializes jButton3
*
* @return javax.swing.JButton
*/
private JButton getJButton3() {
if (jButton3 == null) {
jButton3 = new JButton();
jButton3.setBounds(new Rectangle(642, 250, 65, 51));
jButton3.setText("更改");
jButton3.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent e) {
System.out.println("actionPerformed()"); // TODO Auto-generated Event stub actionPerformed()
DBConnected.connect();
String uname="";
String upass="";
String upriv="";
String ubirth="";
String usex="";
String utel="";
String usay="";
uname=jTextField1.getText();
upass=jTextField2.getText();
upriv=jTextField3.getText();
ubirth=jTextField4.getText();
usex=jTextField5.getText();
utel=jTextField6.getText();
usay=jTextField7.getText();
String sql="";
String sql1="";
String sql2="";
String sql3="where username='"+uname+"' and password='"+upass+"'";
System.out.println(sql3);
sql1="update t_user set ";
if(usex.equals("男")){usex="1";}
else if(usex.equals("女")){usex="2";}
else {JOptionPane.showMessageDialog(null, "请输入男或女");}
String s="to_date"+"('"+ubirth+"','yyyy-mm-dd')";
if(!upriv.equals("")){sql2="priv_id="+upriv;}
if(!ubirth.equals("")){sql2=sql2+" , birthday="+s;}
if(!usex.equals("")){sql2=sql2+" , gender='"+usex+"'";}
if(!utel.equals("")){sql2=sql2+" , tele_num='"+utel+"'"; }
if(!usay.equals("")){sql2=sql2+" , user_desc='"+usay+"'"+" ";}
//System.out.println(sql2);
sql=sql1+sql2+sql3;
System.out.println(sql);
String ss="select * from t_user where username='"+uname+"' and password='"+upass+"'";
System.out.println(ss);
PreparedStatement psmt;
ResultSet res=null;
res=DBConnected.getMsg(ss);
try {
if(res.next())
{
System.out.println("1");
DBConnected.commit(sql);
System.out.println("2");
}
} catch (SQLException e1) {
JOptionPane.showMessageDialog(null, "用户或密码错误");// TODO 自动生成 catch 块
e1.printStackTrace();
}
// DBConnected.connect();
// sql="delete from t_user where username='"+uname+"' and password='"+upass+"'";
// System.out.println(sql);
// DBConnected.commit(sql);
}
});
}
return jButton3;
}
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
SwingUtilities.invokeLater(new Runnable() {
public void run() {
search thisClass = new search();
thisClass.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
thisClass.setVisible(true);
}
});
}
/**
* This is the default constructor
*/
public search() {
super();
initialize();
}
/**
* This method initializes this
*
* @return void
*/
private void initialize() {
this.setSize(729, 390);
this.setJMenuBar(getJJMenuBar());
this.setContentPane(getJContentPane());
this.setTitle("图书管理系统");
}
/**
* This method initializes jContentPane
*
* @return javax.swing.JPanel
*/
private JPanel getJContentPane() {
if (jContentPane == null) {
jLabel = new JLabel();
jLabel.setFont(new Font("Dialog", Font.BOLD, 14));
jLabel.setBounds(new Rectangle(3, 28, 130, 34));
jLabel.setText("输入查询的用户名");
jContentPane = new JPanel();
jContentPane.setLayout(null);
jContentPane.add(getJPanel(), null);
jContentPane.add(getJPanel1(), null);
jContentPane.add(getJButton1(), null);
jContentPane.add(getJButton2(), null);
jContentPane.add(getJButton3(), null);
}
return jContentPane;
}
} // @jve:decl-index=0:visual-constraint="10,10"