下面是我用JAVA编的第一个作品-心情留言坊,菜鸟的作品,大家不要见怪,代码很冗长,注释也不是很清楚,以后会慢慢改进的,希望大家给我一点点鼓励.谢谢.
//主程序
package customer;
import javax.swing.UIManager;
import java.awt.*;
public class Application1 {
boolean packFrame = false;
/**Construct the application*/
public Application1() {
Frame1 frame = new Frame1();
//Validate frames that have preset sizes
//Pack frames that have useful preferred size info, e.g. from their layout
if (packFrame) {
frame.pack();
}
else {
frame.validate();
}
//Center the window
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
Dimension frameSize = frame.getSize();
if (frameSize.height > screenSize.height) {
frameSize.height = screenSize.height;
}
if (frameSize.width > screenSize.width) {
frameSize.width = screenSize.width;
}
frame.setLocation((screenSize.width - frameSize.width) / 2, (screenSize.height - frameSize.height) / 2);
frame.setVisible(true);
}
/**Main method*/
public static void main(String[] args) {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
}
catch(Exception e) {
e.printStackTrace();
}
new Application1();
}
}
/
//登陆模块
package customer;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.sql.*;
import java.sql.DriverManager;
import javax.swing.border.*;
import java.text.SimpleDateFormat;
/**
* Title: customerForMysql
* Description: my java project on jdbc.
* Copyright: Copyright (c) 2005
* Company: stonehouse
* @author stone
* @version 1.0
*/
class readfunc extends JFrame {
JPanel contentPane;
JMenuBar jMenuBar1 = new JMenuBar();
String url="jdbc:odbc:testDSN",uname="",pass="";
JButton jButton2 = new JButton();
TitledBorder titledBorder1;
JLabel jLabel4 = new JLabel();
JLabel jLabel5 = new JLabel();
JButton lastpaper = new JButton();
JButton update = new JButton();
JScrollPane jScrollPane1 = new JScrollPane();
JTextPane jtext = new JTextPane();
JButton midify = new JButton();
JButton nextpaper = new JButton();
JButton oldpaper = new JButton();
TitledBorder titledBorder2;
JLabel jLabel8 = new JLabel();
JTextField jtitle = new JTextField();
JComboBox boxWeather = new JComboBox();
JLabel jLabel2 = new JLabel();
JLabel jLabel3 = new JLabel();
JTextField tDate = new JTextField();
JLabel jLabel6 = new JLabel();
JComboBox boxMood = new JComboBox();
JButton jButton1 = new JButton();
String username,userpwd;
int clickForward=1,clickBackward=0;
int idstr=0,count=0;
/**Construct the frame*/
readfunc(String inputname,String inputpwd) {
enableEvents(AWTEvent.WINDOW_EVENT_MASK);
try {
username=inputname;
userpwd=inputpwd;
jbInit();
}
catch(Exception e) {
e.printStackTrace();
}
}
/**Component initialization*/
private void jbInit() throws Exception {
//setIconImage(Toolkit.getDefaultToolkit().createImage(Frame1.class.getResource("[Your Icon]")));
contentPane = (JPanel) this.getContentPane();
titledBorder1 = new TitledBorder("");
titledBorder2 = new TitledBorder("");
titledBorder3 = new TitledBorder("");
contentPane.setLayout(null);
this.setJMenuBar(jMenuBar1);
this.setResizable(false);
this.setSize(new Dimension(400, 567));
this.setTitle("心情留言坊--修改日记");
this.addComponentListener(new java.awt.event.ComponentAdapter() {
public void componentShown(ComponentEvent e) {
this_componentShown(e);
}
});
this.addWindowListener(new java.awt.event.WindowAdapter() {
public void windowOpened(WindowEvent e) {
this_windowOpened(e);
}
});
contentPane.setBackground(SystemColor.inactiveCaptionText);
contentPane.setForeground(Color.red);
contentPane.setToolTipText("");
jMenuBar1.setBackground(UIManager.getColor("text"));
jButton2.setBorder(BorderFactory.createEtchedBorder());
jButton2.setIcon(new ImageIcon(new java.net.URL("file:///D:/customer/moodrest.JPG")));
jButton2.setBounds(new Rectangle(66, 18, 249, 56));
jLabel4.setFont(new java.awt.Font("Serif", 0, 13));
jLabel4.setText("2005 石头工作室 出品");
jLabel4.setBounds(new Rectangle(121, 485, 147, 18));
jLabel5.setFont(new java.awt.Font("Dialog", 0, 25));
jLabel5.setText("/u00AE");
jLabel5.setBounds(new Rectangle(149, 487, 15, 17));
lastpaper.setText("上一篇");
lastpaper.setBounds(new Rectangle(36, 356, 75, 39));
lastpaper.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(ActionEvent e) {
lastpaper_actionPerformed(e);
}
});
update.setBackground(Color.orange);
update.setText("修改个人资料");
update.setBounds(new Rectangle(269, 447, 107, 33));
update.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(ActionEvent e) {
update_actionPerformed(e);
}
});
jScrollPane1.setBounds(new Rectangle(31, 139, 337, 212));
jtext.setText(" ");
midify.setBackground(SystemColor.textHighlightText);
midify.setFont(new java.awt.Font("DialogInput", 0, 25));
midify.setForeground(SystemColor.windowBorder);
midify.setText("修改");
midify.setBounds(new Rectangle(280, 354, 88, 57));
midify.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(ActionEvent e) {
midify_actionPerformed(e);
}
});
nextpaper.setEnabled(false);
nextpaper.setText("下一篇");
nextpaper.setBounds(new Rectangle(35, 403, 76, 36));
nextpaper.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(ActionEvent e) {
nextpaper_actionPerformed(e);
}
});
oldpaper.setText("最早的一篇");
oldpaper.setBounds(new Rectangle(145, 402, 110, 37));
oldpaper.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(ActionEvent e) {
oldpaper_actionPerformed(e);
}
});
jLabel8.setBounds(new Rectangle(122, 112, 46, 20));
jLabel8.setText("标题:");
jLabel8.setFont(new java.awt.Font("Monospaced", 1, 15));
jtitle.setBounds(new Rectangle(181, 108, 93, 24));
jtitle.setHorizontalAlignment(SwingConstants.CENTER);
jtitle.setText("无题");
boxWeather.setBounds(new Rectangle(72, 81, 108, 25));
boxWeather.setEditable(true);
jLabel2.setText("创作日期:");
jLabel2.setBounds(new Rectangle(40, 451, 69, 18));
jLabel2.setFont(new java.awt.Font("Serif", 0, 15));
jLabel2.setForeground(Color.blue);
jLabel3.setBounds(new Rectangle(30, 81, 38, 26));
jLabel3.setText("天气:");
jLabel3.setFont(new java.awt.Font("Serif", 0, 15));
jLabel6.setFont(new java.awt.Font("Serif", 0, 15));
jLabel6.setText("心情:");
jLabel6.setBounds(new Rectangle(221, 82, 44, 25));
boxMood.setEditable(true);
boxMood.setBounds(new Rectangle(272, 78, 94, 24));
jButton1.setBackground(SystemColor.textInactiveText);
jButton1.setText("删 除");
jButton1.setBounds(new Rectangle(278, 414, 91, 25));
jButton1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(ActionEvent e) {
jButton1_actionPerformed(e);
}
});
tDate.setBounds(new Rectangle(116, 448, 137, 23));
jButton3.setBackground(Color.orange);
jButton3.setText("写日记");
jButton3.setBounds(new Rectangle(144, 356, 108, 39));
jButton3.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(ActionEvent e) {
jButton3_actionPerformed(e);
}
});
contentPane.add(jButton2, null);
contentPane.add(jScrollPane1, null);
contentPane.add(lastpaper, null);
contentPane.add(midify, null);
contentPane.add(oldpaper, null);
contentPane.add(jLabel3, null);
contentPane.add(jLabel8, null);
contentPane.add(jtitle, null);
contentPane.add(boxWeather, null);
contentPane.add(tDate, null);
contentPane.add(jLabel2, null);
contentPane.add(boxMood, null);
contentPane.add(jLabel4, null);
contentPane.add(jLabel5, null);
contentPane.add(jLabel6, null);
contentPane.add(jButton1, null);
contentPane.add(nextpaper, null);
contentPane.add(jButton3, null);
contentPane.add(update, null);
jScrollPane1.getViewport().add(jtext, null);
}
/**Overridden so we can exit when window is closed*/
protected void processWindowEvent(WindowEvent e) {
super.processWindowEvent(e);
if (e.getID() == WindowEvent.WINDOW_CLOSING) {
System.exit(0);
}
}
void update_actionPerformed(ActionEvent e) {
int x,y;
mymodify mymod=new mymodify(username,userpwd);
x=this.getX();
y=this.getY();
if(y<=0)
{y+=30;mymod.setLocation(x,y);}//y
else
mymod.setLocation(x,y);
mymod.show();
this.dispose();
}
void this_windowOpened(WindowEvent e) {
//先连接数据库取出相关信息填入窗口
try{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");}
catch(Exception exc){};
try{
Connection con=DriverManager.getConnection(url,uname,pass);//开始连接;
//设置对象
Statement st=con.createStatement();
String str="SELECT * FROM diary WHERE dAuthor='"+username+"'";
String weatherstr="";
String contentstr="";
String titlestr="";
String datestr="";
String moodstr="";
ResultSet rs=st.executeQuery(str);
//计算总数
while(rs.next())
{ count++; }//end of while
rs=null;
rs=st.executeQuery(str);
if(count>=1)
{for(int i=0;i<count-1;i++)rs.next();
if(rs.next()){
if(nextpaper.isEnabled())nextpaper.setEnabled(false);
if(!lastpaper.isEnabled())lastpaper.setEnabled(true);
weatherstr=rs.getString("dWeather").trim();
moodstr=rs.getString("dMood").trim();
titlestr=rs.getString("dTitle").trim();
titlestr=titlestr.trim();
contentstr=rs.getString("dContent");
datestr=rs.getString("dDate").trim();
idstr=rs.getInt("dID");
//开始布景;
boxWeather.addItem(weatherstr);
boxWeather.setSelectedItem(weatherstr);
boxMood.addItem(moodstr);
boxMood.setSelectedItem(moodstr);
jtitle.setText(titlestr);
jtext.setText(contentstr);
tDate.setText(datestr);
// paperlist.addItem(titlestr);
//paperlist.setSelectedItem(titlestr);
clickForward=count; }//end of if
}//end of big if
else
{
boxWeather.setEnabled(false);
boxWeather.setSelectedItem("");
boxMood.setEnabled(false);
boxMood.setSelectedItem("");
jtitle.setText("");
lastpaper.setEnabled(false);
jtext.setText("对不起,你还没有写日记!无法查看你的日记!");
jtext.setEditable(false);
tDate.setText("");
// paperlist.setEnabled(false);
// paperlist.setSelectedItem("");
}
st.close();
con.close();
}//end of try2
catch(Exception exc1){JOptionPane.showMessageDialog(null,"程序出错:/n"+exc1.getMessage());
}
boxWeather.addItem("晴朗");
boxWeather.addItem("阴天");
boxWeather.addItem("雨天");
//给心情提供选项
boxMood.addItem("爽呆了");
boxMood.addItem("正常");
boxMood.addItem("郁闷");
boxMood.addItem("伤心");
//写上日期
/* java.util.Date myDate=new java.util.Date();
SimpleDateFormat myformat=new SimpleDateFormat("yyyy年M月dd日 EEEE");
dDate.setText(myformat.format(myDate));*/
}
void jButton1_actionPerformed(ActionEvent e) {
String weatherstr="";
String contentstr="";
String titlestr="";
String datestr="";
String moodstr="";
int r=-1;//标志值
r=JOptionPane.showConfirmDialog(this,"你确信要删除你的心血之作吗?","再考虑一下?",JOptionPane.YES_NO_OPTION);
if(r==0)
{ try{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");}
catch(Exception exc7){};
try{
Connection con=DriverManager.getConnection(url,uname,pass);
Statement st=con.createStatement();
String str="DELETE FROM diary WHERE dAuthor='"+username+"' AND dID="+idstr;
int n=st.executeUpdate(str);
try{ if(n!=0){JOptionPane.showMessageDialog(null,"删除成功了!");clickForward=(--count);}}
catch(Exception exc){JOptionPane.showMessageDialog(null,"出错了哦:"+exc.getMessage());}
str="SELECT * FROM diary WHERE dAuthor='"+username+"'";
ResultSet rs=st.executeQuery(str);
if(count>=1)
{
for(int i=0;i<clickForward-1;i++){rs.next();}
//重新进行初始化
if(rs.next()) {
weatherstr=rs.getString("dWeather").trim();
moodstr=rs.getString("dMood").trim();
titlestr=rs.getString("dTitle").trim();
contentstr=rs.getString("dContent");
datestr=rs.getString("dDate").trim();
idstr=rs.getInt("dID");
//开始布景;
boxWeather.addItem(weatherstr);
boxWeather.setSelectedItem(weatherstr);
boxMood.addItem(moodstr);
boxMood.setSelectedItem(moodstr);
jtitle.setText(titlestr);
jtext.setText(contentstr);
tDate.setText(datestr);
//paperlist.setSelectedItem(titlestr);
}//end of if
}//end of big if
else
{boxWeather.setEnabled(false);
boxWeather.setSelectedItem("");
boxMood.setEnabled(false);
boxMood.setSelectedItem("");
jtitle.setText("");
jtext.setText("");
tDate.setText("");
// paperlist.setEnabled(false);
// paperlist.setSelectedItem("");
lastpaper.setEnabled(false);
jtext.setText("对不起,你还没有写日记!无法查看你的日记!");
JOptionPane.showMessageDialog(null,"留言坊中没有日记了~~!");}
}
catch(Exception exc7){JOptionPane.showMessageDialog(null,"出错了哦:"+exc7.getMessage());}
} //在这里写入删除的代码,成功的话记得弹出一个提示框//;
}
void midify_actionPerformed(ActionEvent e) {
//连接数据库
try{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");}
catch(Exception exc){};
try{
Connection con=DriverManager.getConnection(url,uname,pass);
Statement st=con.createStatement();
//准备执行任务
String str="UPDATE diary SET dTitle='"+jtitle.getText()+"',dDate='"+tDate.getText()+"',dMood='"+boxMood.getSelectedItem()+"',dWeather='"+boxWeather.getSelectedItem()+"',dContent='"+jtext.getText()+"' WHERE dAuthor='"+username+"' AND dID="+idstr;
int n=st.executeUpdate(str);
if(n!=0){JOptionPane.showMessageDialog(null,"恭喜你,修改成功了!");}
else
JOptionPane.showMessageDialog(null,"出错了哦!");
st.close();
con.close();
}//end of try2
catch(Exception exc1){JOptionPane.showMessageDialog(null,"出错了哦:"+exc1.getMessage());}//end of catch
}
void nextpaper_actionPerformed(ActionEvent e) {
//连接数据库
String weatherstr="";
String contentstr="",titlestr="";
String datestr="",moodstr="";
jtext.setText("");
try{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");}
catch(Exception exc2){};
try{
Connection con=DriverManager.getConnection(url,uname,pass);
Statement st=con.createStatement();
//执行任务
String str="SELECT * FROM diary WHERE dAuthor='"+username+"'";
ResultSet rs=st.executeQuery(str);
for(int i=0;i<clickForward;i++) rs.next();
if(clickForward!=count&&rs.next())
{
if(!lastpaper.isEnabled())lastpaper.setEnabled(true);
idstr=rs.getInt("dID");
weatherstr=rs.getString("dWeather").trim();
moodstr=rs.getString("dMood").trim();
titlestr=rs.getString("dTitle").trim();
contentstr=rs.getString("dContent");
datestr=rs.getString("dDate").trim();
//开始布景;
boxWeather.addItem(weatherstr);
boxWeather.setSelectedItem(weatherstr);
boxMood.addItem(moodstr);
boxMood.setSelectedItem(moodstr);
jtitle.setText(titlestr);
jtext.setText(contentstr);
tDate.setText(datestr);
// paperlist.setSelectedItem(titlestr);
clickForward++;
} //end of big if
else
{nextpaper.setEnabled(false);
weatherstr=rs.getString("dWeather").trim();
moodstr=rs.getString("dMood").trim();
titlestr=rs.getString("dTitle").trim();
contentstr=rs.getString("dContent");
datestr=rs.getString("dDate").trim();
//开始布景;
boxWeather.addItem(weatherstr);
boxWeather.setSelectedItem(weatherstr);
boxMood.addItem(moodstr);
boxMood.setSelectedItem(moodstr);
jtitle.setText(titlestr);
jtext.setText(contentstr);
tDate.setText(datestr);
// paperlist.setSelectedItem(titlestr);
}
st.close();
con.close();
}//end of try2
catch(Exception exc3){JOptionPane.showMessageDialog(null,"出错了哦:"+exc3.getMessage());}//end of catch
}
void lastpaper_actionPerformed(ActionEvent e) {
//连接数据库
String weatherstr="";
String contentstr="",titlestr="";
String datestr="",moodstr="";
try{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");}
catch(Exception exc4){};
try{
Connection con=DriverManager.getConnection(url,uname,pass);
//执行任务
String str="SELECT * FROM diary WHERE dAuthor='"+username+"'";
Statement st=con.createStatement();
ResultSet rs=st.executeQuery(str);
///移动指针到当前记录的前一条记录;
for(int i=0;i<clickForward-2;i++){rs.next();}
if(rs.next())
{
if(!nextpaper.isEnabled())nextpaper.setEnabled(true);
if(clickForward<=1){lastpaper.setEnabled(false);}
else
clickForward--;
weatherstr=rs.getString("dWeather").trim();
moodstr=rs.getString("dMood").trim();
titlestr=rs.getString("dTitle").trim();
contentstr=rs.getString("dContent");
datestr=rs.getString("dDate").trim();
//开始布景;
boxWeather.addItem(weatherstr);
boxWeather.setSelectedItem(weatherstr);
boxMood.addItem(moodstr);
boxMood.setSelectedItem(moodstr);
jtitle.setText(titlestr);
jtext.setText(contentstr);
tDate.setText(datestr);
//paperlist.setSelectedItem(titlestr);
}//end of if
st.close();
con.close();
}//end of try2
catch(Exception exc5){JOptionPane.showMessageDialog(null,"出错了哦:"+exc5.getMessage());}//end of catch
}
void oldpaper_actionPerformed(ActionEvent e) {
//连接数据库
String weatherstr="";
String contentstr="",titlestr="";
String datestr="",moodstr="";
try{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");}
catch(Exception exc7){};
try{
Connection con=DriverManager.getConnection(url,uname,pass);
Statement st=con.createStatement();
//执行任务
String str="SELECT * FROM diary WHERE dAuthor='"+username+"'";
ResultSet rs=st.executeQuery(str);
//定位指针;
if(rs.next()){
if(nextpaper.isEnabled())nextpaper.setEnabled(false);
if(!lastpaper.isEnabled())lastpaper.setEnabled(true);
weatherstr=rs.getString("dWeather").trim();
moodstr=rs.getString("dMood").trim();
titlestr=rs.getString("dTitle").trim();
contentstr=rs.getString("dContent");
datestr=rs.getString("dDate").trim();
//开始布景;
boxWeather.addItem(weatherstr);
boxWeather.setSelectedItem(weatherstr);
boxMood.addItem(moodstr);
boxMood.setSelectedItem(moodstr);
jtitle.setText(titlestr);
jtext.setText(contentstr);
tDate.setText(datestr);
// paperlist.setSelectedItem(titlestr);
clickForward=count;
}//end of big if
st.close();
con.close();
}//end of try2
catch(Exception exc6){JOptionPane.showMessageDialog(null,"出错了哦:"+exc6.getMessage());}//end of catch
}
JButton jButton3 = new JButton();
TitledBorder titledBorder3;
void jButton3_actionPerformed(ActionEvent e) {
int x,y;
write mewrite=new write(username,userpwd);
x=this.getX();
y=this.getY();
if(y<=0)
{y+=30;mewrite.setLocation(x,y);}//y
else
mewrite.setLocation(x,y);
mewrite.show();
this.dispose();
}
void paperlist_mouseReleased(MouseEvent e) {
String titlestr="";
//准备连接数据库
String weatherstr="";
String contentstr="";
String datestr="",moodstr="";
try{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");}
catch(Exception exc7){};
try{
Connection con=DriverManager.getConnection(url,uname,pass);
Statement st=con.createStatement();
//执行任务
String str="SELECT * FROM diary WHERE dAuthor='"+username+"' AND dID='"+titlestr+"'";
ResultSet rs=st.executeQuery(str);
//定位指针;
if(rs.next()){
if(nextpaper.isEnabled())nextpaper.setEnabled(false);
if(!lastpaper.isEnabled())lastpaper.setEnabled(true);
weatherstr=rs.getString("dWeather").trim();
moodstr=rs.getString("dMood").trim();
titlestr=rs.getString("dTitle").trim();
contentstr=rs.getString("dContent");
datestr=rs.getString("dDate").trim();
//开始布景;
boxWeather.addItem(weatherstr);
boxWeather.setSelectedItem(weatherstr);
boxMood.addItem(moodstr);
boxMood.setSelectedItem(moodstr);
jtitle.setText(titlestr);
jtext.setText(contentstr);
tDate.setText(datestr);
// paperlist.setSelectedItem(titlestr);
clickForward=count;
}//end of big if
st.close();
con.close();
}//end of try2
catch(Exception exc6){JOptionPane.showMessageDialog(null,"出错了哦:"+exc6.getMessage());}//end of catch
}
void this_componentShown(ComponentEvent e) {
//设置我自己的LOGO!
Image img=Toolkit.getDefaultToolkit().getImage("C://Documents and Settings//stone//jbproject//customer//myheart.jpg");
this.setIconImage(img);
}
}
//窗体类
package customer;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.sql.*;
import java.sql.DriverManager;
import javax.swing.border.*;
import java.applet.*;
import java.awt.Graphics;
public class Frame1 extends JFrame {
JPanel contentPane;
JMenuBar jMenuBar1 = new JMenuBar();
JTextField namef = new JTextField();
JLabel jLabel1 = new JLabel();
JLabel jLabel3 = new JLabel();
JPasswordField passf = new JPasswordField();
JButton addbutton = new JButton();
JButton look = new JButton();
String url1="jdbc:odbc:testDSN";
JButton jButton1 = new JButton();
JCheckBox remenber = new JCheckBox();
JButton jButton2 = new JButton();
TitledBorder titledBorder1;
JLabel jLabel4 = new JLabel();
JLabel jLabel5 = new JLabel();
//声明参数
String str1=null,str2=null,str3=null;
/**Construct the frame*/
public Frame1() {
enableEvents(AWTEvent.WINDOW_EVENT_MASK);
try {
jbInit();
}
catch(Exception e) {
e.printStackTrace();
}
}
/**Component initialization*/
private void jbInit() throws Exception {
//setIconImage(Toolkit.getDefaultToolkit().createImage(Frame1.class.getResource("[Your Icon]")));
contentPane = (JPanel) this.getContentPane();
titledBorder1 = new TitledBorder("");
contentPane.setLayout(null);
this.setJMenuBar(jMenuBar1);
this.setResizable(false);
this.setSize(new Dimension(400, 373));
this.setTitle("心情留言坊");
this.addComponentListener(new java.awt.event.ComponentAdapter() {
public void componentShown(ComponentEvent e) {
this_componentShown(e);
}
});
this.addWindowListener(new java.awt.event.WindowAdapter() {
public void windowOpened(WindowEvent e) {
this_windowOpened(e);
}
});
contentPane.setBackground(SystemColor.inactiveCaptionText);
contentPane.setToolTipText("");
jMenuBar1.setBackground(UIManager.getColor("text"));
namef.setToolTipText("输入你的姓名");
namef.setBounds(new Rectangle(161, 104, 87, 20));
jLabel1.setText("姓名:");
jLabel1.setBounds(new Rectangle(126, 104, 32, 20));
jLabel3.setText("密码:");
jLabel3.setBounds(new Rectangle(123, 136, 29, 19));
passf.setToolTipText("输入密码");
passf.setBounds(new Rectangle(161, 138, 89, 17));
addbutton.setToolTipText("");
addbutton.setText("注册");
addbutton.setBounds(new Rectangle(154, 244, 78, 21));
addbutton.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(ActionEvent e) {
addbutton_actionPerformed(e);
}
});
look.setText("忘记密码了?");
look.setBounds(new Rectangle(235, 244, 104, 21));
look.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(ActionEvent e) {
look_actionPerformed(e);
}
});
jButton1.setText("登陆");
jButton1.setBounds(new Rectangle(70, 244, 81, 22));
jButton1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(ActionEvent e) {
jButton1_actionPerformed(e);
}
});
remenber.setText(" 记 住 密 码");
remenber.setBackground(SystemColor.inactiveCaptionText);
remenber.setBounds(new Rectangle(149, 176, 101, 26));
remenber.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(ActionEvent e) {
remenber_actionPerformed(e);
}
});
jButton2.setBorder(BorderFactory.createEtchedBorder());
jButton2.setIcon(new ImageIcon(new java.net.URL("file:///D:/customer/moodrest.JPG")));
jButton2.setBounds(new Rectangle(66, 24, 249, 59));
jLabel4.setFont(new java.awt.Font("Serif", 0, 13));
jLabel4.setText("2005 石头工作室 出品");
jLabel4.setBounds(new Rectangle(124, 274, 147, 18));
jLabel5.setFont(new java.awt.Font("Dialog", 0, 25));
jLabel5.setText("/u00AE");
jLabel5.setBounds(new Rectangle(152, 273, 15, 22));
contentPane.add(jLabel1, null);
contentPane.add(namef, null);
contentPane.add(jLabel3, null);
contentPane.add(passf, null);
contentPane.add(addbutton, null);
contentPane.add(look, null);
contentPane.add(jButton1, null);
contentPane.add(jButton2, null);
contentPane.add(jLabel4, null);
contentPane.add(jLabel5, null);
contentPane.add(remenber, null);
}
//创建消息窗口
class queryDialog extends Dialog{
queryDialog(Frame f,Point point){
super(f,true);
reshape(20+point.x,20+point.y,100+point.x,30+point.y);
setBackground(new Color(215,215,215));
}//end of func
public boolean handleEvent(Event e){
if(e.id==Event.WINDOW_DESTROY)
{dispose();
return true;}
return false;
}//允许用户单击关闭按钮关闭窗口;
}//end of class queryDialog
/**Overridden so we can exit when window is closed*/
protected void processWindowEvent(WindowEvent e) {
super.processWindowEvent(e);
if (e.getID() == WindowEvent.WINDOW_CLOSING) {
System.exit(0);
}
}
void addbutton_actionPerformed(ActionEvent e) {
register myreg=new register();
int x,y;
x=this.getX();y=this.getY();
if(y<0)y+=20;
myreg.setLocation(x,y);
myreg.show();
this.dispose();
}
void look_actionPerformed(ActionEvent e) {
//change target code
String user="",ask="",answer="";
user=JOptionPane.showInputDialog(this,"请输入用户名!");
if(user.length()<0)
{JOptionPane.showMessageDialog(this,"你可什么都没有输入哦!");}
else{
//准备连接数据库;
try{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
}//end of try
catch(Exception ex3){}
try{
Connection con=java.sql.DriverManager.getConnection(url1,"","");
String str="SELECT * FROM friend WHERE fName=?";
PreparedStatement ps=con.prepareStatement(str);
ps.setString(1,user.trim());
java.sql.ResultSet rs=ps.executeQuery();
String cName,cPWD;
///
//有这个人,深入;
if(rs.next())
{
JOptionPane.showMessageDialog(null,"您的密码保护问题是:"+rs.getString("fQuestion"));
answer=JOptionPane.showInputDialog(this,"请输入密码保护答案:").trim();
if(answer.equals(rs.getString("fAnswer").trim()))
{ //显示结果;
cName=rs.getString("fName");
cPWD=rs.getString("fPass");
//test class dialog
queryDialog qd=new queryDialog(this,this.location());
qd.setLayout(new GridLayout(2,2,20,3));//(0,2,20,3));
qd.add(new Label("用户名:",Label.RIGHT));
qd.add(new Label(cName));
qd.add(new Label("密 码:",Label.RIGHT));
qd.add(new Label(cPWD));
qd.show(); }// end of internal if
else
{ JOptionPane.showMessageDialog(null,"您的答案不正确!");
rs.close();
ps.close();
con.close(); }//end of internal else
}//end of if
else// 没这个人;
{JOptionPane.showMessageDialog(null,"不好意思,查无此人!");
rs.close();
ps.close();
con.close();}//end of big else
con.close();
}//end of try 2
catch(Exception ex4){JOptionPane.showMessageDialog(null,"程序出错了~!"+ex4.getMessage());}
}
}
void jButton1_actionPerformed(ActionEvent e) {
//验证输入的合法性
int r=-1;
str1=null;
str2=null;
str3=null;
str1=namef.getText();
str2=passf.getText();
if(str1.length()<1||str2.length()<1){
JOptionPane.showMessageDialog(null,"出错了!请确保您输入了名字,和正确的密码!");
r=1;}
//准备连接数据库
try{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
}//end of try
catch(Exception ex5){}
try{
String cName=null,cPWD=null;
Connection con=java.sql.DriverManager.getConnection(url1,"","");
String str="SELECT * FROM friend WHERE fName=? AND fPass=?";
PreparedStatement ps=con.prepareStatement(str);
ps.setString(1,str1);
ps.setString(2,str2);
ResultSet rs=ps.executeQuery();
//开始验证
if(rs.next()){
cName=rs.getString("fName").trim();
cPWD=rs.getString("fPass").trim();
//存在此编号,release connection
ps.close();
con.close();
///
// 保存密码
try{
if(remenber.isSelected())
{
con=java.sql.DriverManager.getConnection(url1,"","");
str="UPDATE savedata SET sName='"+cName+"',sPass='"+cPWD+"'";
Statement st=con.createStatement();
st.executeUpdate(str);
con.close(); }//end of if
else
{con=java.sql.DriverManager.getConnection(url1,"","");
String str2="";
str="UPDATE savedata SET sName='"+cName+"',sPass='"+str2+"'";
Statement st=con.createStatement();
st.executeUpdate(str);
st.close();
con.close();
}//end of else
}//end of try
catch(Exception cc){JOptionPane.showMessageDialog(null,"出错了哦!");}
///
int x,y;
writefunc mywrite=new writefunc(cName,cPWD);
x=this.getX();
y=this.getY();
mywrite.setLocation(x,y);
mywrite.show();
this.dispose();
//成功登陆!!
r=0;
}//end of if||
else
{
ps.close();
con.close();
check mycheck=new check();
if(mycheck.checkup(namef.getText()))
JOptionPane.showMessageDialog(null,"不好意思,您输入的密码不对啊~!");
else
{int mysure=JOptionPane.showConfirmDialog(null,"这个用户名还没有被注册,你想注册吗?","你真走运",JOptionPane.YES_NO_OPTION);
switch(mysure){
case 0: register myreg=new register();
int x,y;
x=this.getX();y=this.getY();
if(y<0)y+=20;
myreg.setLocation(x,y);
myreg.show();
this.dispose();
}//end of switch
} }
ps.close();
con.close();
///
}//end of try2
catch(Exception ex6){JOptionPane.showMessageDialog(null,"程序出错了!");}
}
void remenber_actionPerformed(ActionEvent e) {
}
void this_windowOpened(WindowEvent e) {
try{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
}//end of try1
catch(Exception ee8){}
try{
//creat connection
Connection con=DriverManager.getConnection(url1,"","");
//creat statement
Statement st=con.createStatement();
ResultSet rs=st.executeQuery("SELECT * FROM savedata");
// 填上名字和密码,如果有的话;
if(rs.next())
{ namef.setText(rs.getString("sName"));
passf.setText(rs.getString("sPass").trim());
}//end of if
}//end of try2
catch(Exception ee9){JOptionPane.showMessageDialog(null,"出错了:"+ee9.getMessage());}
}
void this_componentShown(ComponentEvent e) {
//设置我自己的LOGO!
Image img=Toolkit.getDefaultToolkit().getImage("C://Documents and Settings//stone//jbproject//customer//myheart.jpg");
this.setIconImage(img);
}
void jButton3_actionPerformed(ActionEvent e) {
//
//准备连接数据库;
try{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
}//end of try1
catch(Exception ee3){}
try{
//creat connection
Connection con=DriverManager.getConnection(url1,"","");
//creat statement
Statement st=con.createStatement();
// PreparedStatement ps=con.prepareStatement();
//
// 修改模块;
//建立DIARY表
/* String str="CREATE TABLE diary (dAuthor VARCHAR(20),dTitle VARCHAR(30),dDate VARCHAR(25) NULL,dMood VARCHAR(20) NULL,dWeather VARCHAR(20) NULL,dContent VARCHAR(5000) NOT NULL)";
int n= st.executeUpdate(str);
if(n!=0){JOptionPane.showMessageDialog(null,"建表success!");}
else JOptionPane.showMessageDialog(null,"fail to update it!");
*/
/
//增加列
/*String str="ALTER TABLE diary ADD dDate VARCHAR(20)";
int n=st.executeUpdate(str);
if(n!=0){JOptionPane.showMessageDialog(null,"增加列success!");}
else JOptionPane.showMessageDialog(null,"fail to update it!");
*/
///
//插入数据
//int n=st.executeUpdate("INSERT INTO diary(dAuthor,dTitle,dDate,dMood,dWeather,dContent) values('"+namef.getText()+"','"+passf.getText()+"','2005-8-19','还不错哦','晴朗得很','"+codef.getText()+"')");
/*int n=st.executeUpdate("INSERT INTO diary(dContent,dTitle,dDate,dMood) values('我爱你老婆,一生一世,永远不相忘.你也要记得我哦','人生之路','雨','凉')");
if(n!=0){JOptionPane.showMessageDialog(null,"插入数据success!");}
else JOptionPane.showMessageDialog(null,"fail to update it!");*/
//修改数据
/*String str="ALTER TABLE diary MODIFY COLUMN dDate VARCHAR(20)";
int n=st.executeUpdate(str);
if(n!=0){JOptionPane.showMessageDialog(null,"修改表success!");}
else JOptionPane.showMessageDialog(null,"fail to update it!");
*/
///
// 删除列
/*String str="ALTER TABLE diary DROP COLUMN dDate";
int n=st.executeUpdate(str);
if(n!=0){JOptionPane.showMessageDialog(null,"修改表success!");}
else JOptionPane.showMessageDialog(null,"fail to update it!");
*/
///
//删除表
/* int n=st.executeUpdate("DROP TABLE friend");
if(n!=0){JOptionPane.showMessageDialog(null,"删除表success!");}
else JOptionPane.showMessageDialog(null,"fail to update it!");
*/
// 删除数据
/* int n=st.executeUpdate("DELETE FROM diary");
if(n!=0){JOptionPane.showMessageDialog(null,"删除数据success!");}
else JOptionPane.showMessageDialog(null,"fail to update it!");
*/
/
//st.executeUpdate("CREATE TABLE friend(fName VARCHAR(20) NOT NULL,fPass VARCHAR(16) NOT NULL, fSign VARCHAR(255) NULL)");
/* */
//
// st.executeUpdate("UPDATE friend SET fSign='我改密码了,看变了没有,原来是1哦!',fPass='2' WHERE fName='stone'");
/* String strr="111111111111111我决定又改回来,你看着吧!现在是1了吗? ";
String str="UPDATE friend SET fSign="+strr+" WHERE fName=?";
PreparedStatement ps1=con.prepareStatement(str);
String strr1="stone";
ps1.setString(1,strr1);
// ps1.setString(2,strr1);
int n=ps1.executeUpdate();if(n!=0){JOptionPane.showMessageDialog(null,"success!");}
else JOptionPane.showMessageDialog(null,"fail to update it!");*/
//String st1="CREATE TABLE diary (dAuthor VARCHAR(10) NOT NULL,dTitle VARCHAR(10) NOT NULL,dDate VARCHAR(18) NULL,dMood VARCHAR(10) NULL, dWeather VARCHAR(10),dContent VARCHAR(8000) NOT NULL )";
///
//String strr=namef.getText(),strr1=codef.getText();
//String st1="UPDATE friend SET fSign='"+strr+"',fPass='"+strr1+"' WHERE fName='stone'";
// st.executeUpdate(st1);
ResultSet rs=st.executeQuery("SELECT * FROM diary");
ResultSetMetaData rsmd=rs.getMetaData();
int cc=rsmd.getColumnCount();
int count=0;
//result.setText("列名 列类型/n");
while(rs.next()){
// for(int i=1;i<=cc;i++)
// {
//result.append(rsmd.getColumnName(i)+" "+rsmd.getColumnTypeName(i)+"/n");}
//String cName=rs.getString(1),cPass=rs.getString(3),cSign=rs.getString(6);
//result.append(cName+" "+cPass+" "+cSign);
//result.append(rs.getString("dContent"));
/mdb
// String cName=rs.getString(1),cPass=rs.getString(2),cSign=rs.getString(3);
//result.append(cName+" "+cPass+" "+cSign);
count++;
} //end internal for
// result.append("记录总数为:"+count+"条!");
st.close();
// ps1.close();
con.close();
}//end of try2
catch(Exception ee4){JOptionPane.showMessageDialog(null,"出错了:"+ee4.getMessage());}
}
}
//修改日记模块
package customer;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.sql.*;
import java.sql.DriverManager;
import javax.swing.border.*;
/**
* Title: customerForMysql
* Description: my java project on jdbc.
* Copyright: Copyright (c) 2005
* Company: stonehouse
* @author stone
* @version 1.0
*/
class mymodify extends JFrame {
JPanel contentPane;
JMenuBar jMenuBar1 = new JMenuBar();
String url="jdbc:odbc:testDSN",uname="",pass="";
JButton jButton2 = new JButton();
TitledBorder titledBorder1;
JLabel jLabel4 = new JLabel();
JLabel jLabel5 = new JLabel();
JLabel inputName = new JLabel();
JLabel jLabel3 = new JLabel();
JLabel jLabel6 = new JLabel();
JLabel jLabel7 = new JLabel();
JLabel jLabel8 = new JLabel();
JButton jButton3 = new JButton();
JButton jButton4 = new JButton();
JButton jButton5 = new JButton();
TitledBorder titledBorder2;
TitledBorder titledBorder3;
JLabel jLabel2 = new JLabel();
JLabel jLabel10 = new JLabel();
JTextField namef = new JTextField();
JScrollPane jScrollPane1 = new JScrollPane();
JTextArea sign = new JTextArea();
JPasswordField passf = new JPasswordField();
JPasswordField passf2 = new JPasswordField();
String username,userpwd;
/**Construct the frame*/
mymodify(String inputname,String inputpwd) {
enableEvents(AWTEvent.WINDOW_EVENT_MASK);
try {
username=inputname;
userpwd=inputpwd;
jbInit();
}
catch(Exception e) {
e.printStackTrace();
}
}
/**Component initialization*/
private void jbInit() throws Exception {
//setIconImage(Toolkit.getDefaultToolkit().createImage(Frame1.class.getResource("[Your Icon]")));
contentPane = (JPanel) this.getContentPane();
titledBorder1 = new TitledBorder("");
titledBorder2 = new TitledBorder("");
titledBorder3 = new TitledBorder("");
contentPane.setLayout(null);
this.setJMenuBar(jMenuBar1);
this.setResizable(false);
this.setSize(new Dimension(400, 542));
this.setTitle("心情留言坊--修改资料");
this.addComponentListener(new java.awt.event.ComponentAdapter() {
public void componentShown(ComponentEvent e) {
this_componentShown(e);
}
});
this.addWindowListener(new java.awt.event.WindowAdapter() {
public void windowOpened(WindowEvent e) {
this_windowOpened(e);
}
});
contentPane.setBackground(SystemColor.inactiveCaptionText);
contentPane.setForeground(Color.red);
contentPane.setToolTipText("");
jMenuBar1.setBackground(UIManager.getColor("text"));
jButton2.setBorder(BorderFactory.createEtchedBorder());
jButton2.setIcon(new ImageIcon(new java.net.URL("file:///D:/customer/moodrest.JPG")));
jButton2.setBounds(new Rectangle(57, 22, 249, 59));
jLabel4.setFont(new java.awt.Font("Serif", 0, 13));
jLabel4.setBorder(BorderFactory.createLineBorder(Color.black));
jLabel4.setText("2005 石头工作室 出品");
jLabel4.setBounds(new Rectangle(114, 459, 147, 18));
jLabel5.setFont(new java.awt.Font("Dialog", 0, 25));
jLabel5.setText("/u00AE");
jLabel5.setBounds(new Rectangle(142, 460, 15, 22));
inputName.setFont(new java.awt.Font("Dialog", 0, 20));
inputName.setHorizontalAlignment(SwingConstants.CENTER);
inputName.setText("密 码:");
inputName.setBounds(new Rectangle(36, 117, 98, 26));
jLabel3.setFont(new java.awt.Font("Dialog", 0, 20));
jLabel3.setHorizontalAlignment(SwingConstants.CENTER);
jLabel3.setText("用 户 名:");
jLabel3.setBounds(new Rectangle(27, 93, 119, 26));
jLabel6.setText("1.");
jLabel6.setBounds(new Rectangle(25, 375, 19, 28));
jLabel7.setText("2.");
jLabel7.setBounds(new Rectangle(25, 413, 22, 22));
jLabel8.setText("3.");
jLabel8.setBounds(new Rectangle(189, 380, 12, 16));
jButton3.setBackground(SystemColor.inactiveCaption);
jButton3.setForeground(SystemColor.window);
jButton3.setToolTipText("我手写我心!");
jButton3.setText("写日记");
jButton3.setBounds(new Rectangle(44, 375, 116, 28));
jButton3.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(ActionEvent e) {
jButton3_actionPerformed(e);
}
});
jButton4.setToolTipText("想品尝回忆的味道吗?");
jButton4.setText("看日记");
jButton4.setBounds(new Rectangle(44, 410, 117, 28));
jButton4.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(ActionEvent e) {
jButton4_actionPerformed(e);
}
});
jButton5.setBackground(Color.pink);
jButton5.setToolTipText("改改再说~");
jButton5.setText("修改个人资料");
jButton5.setBounds(new Rectangle(207, 378, 123, 60));
jButton5.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(ActionEvent e) {
jButton5_actionPerformed(e);
}
});
jLabel2.setFont(new java.awt.Font("Dialog", 0, 20));
jLabel2.setText("确认密码:");
jLabel2.setBounds(new Rectangle(43, 143, 86, 25));
jLabel10.setFont(new java.awt.Font("Dialog", 0, 20));
jLabel10.setText("个性签名:");
jLabel10.setBounds(new Rectangle(43, 226, 90, 23));
namef.setFont(new java.awt.Font("SansSerif", 0, 15));
namef.setToolTipText("用户名不能修改!");
namef.setEditable(false);
namef.setHorizontalAlignment(SwingConstants.CENTER);
namef.setBounds(new Rectangle(136, 95, 173, 22));
jScrollPane1.setBounds(new Rectangle(55, 257, 262, 99));
sign.setLineWrap(true);
sign.setText(" 这人好懒,什么都没写哦!");
passf.setHorizontalAlignment(SwingConstants.CENTER);
passf.setBounds(new Rectangle(136, 123, 173, 22));
passf2.setHorizontalAlignment(SwingConstants.CENTER);
passf2.setBounds(new Rectangle(135, 149, 173, 22));
jLabel11.setBounds(new Rectangle(294, 28, 24, 24));
jLabel11.setText("*");
jLabel11.setForeground(Color.red);
jLabel11.setFont(new java.awt.Font("Dialog", 0, 15));
jLabel9.setBounds(new Rectangle(317, 199, 24, 24));
jLabel9.setText("*");
jLabel9.setForeground(Color.red);
jLabel9.setFont(new java.awt.Font("Dialog", 0, 15));
jLabel12.setBounds(new Rectangle(43, 198, 86, 20));
jLabel12.setText("答案:");
jLabel12.setHorizontalAlignment(SwingConstants.CENTER);
jLabel12.setForeground(Color.red);
jLabel12.setFont(new java.awt.Font("Dialog", 0, 15));
jLabel13.setBounds(new Rectangle(34, 172, 101, 27));
jLabel13.setText("取回密码问题:");
jLabel13.setForeground(Color.red);
jLabel13.setFont(new java.awt.Font("Dialog", 0, 15));
getquestion.setBounds(new Rectangle(134, 175, 177, 21));
getquestion.setText("我最喜欢的食物?");
getquestion.setToolTipText("可以帮助你取回密码!");
jLabel14.setBounds(new Rectangle(317, 176, 24, 24));
jLabel14.setText("*");
jLabel14.setForeground(Color.red);
jLabel14.setFont(new java.awt.Font("Dialog", 0, 15));
getanswer.setBounds(new Rectangle(133, 198, 178, 22));
getanswer.setToolTipText("牢记你的答案!");
jLabel15.setFont(new java.awt.Font("Dialog", 0, 15));
jLabel15.setForeground(Color.red);
jLabel15.setText("*");
jLabel15.setBounds(new Rectangle(316, 153, 26, 24));
jLabel16.setFont(new java.awt.Font("Dialog", 0, 15));
jLabel16.setForeground(Color.red);
jLabel16.setText("*");
jLabel16.setBounds(new Rectangle(315, 122, 24, 24));
jLabel17.setFont(new java.awt.Font("Dialog", 0, 15));
jLabel17.setForeground(Color.red);
jLabel17.setText("*");
jLabel17.setBounds(new Rectangle(314, 93, 24, 24));
contentPane.add(passf2, null);
contentPane.add(jLabel3, null);
contentPane.add(namef, null);
contentPane.add(inputName, null);
contentPane.add(passf, null);
contentPane.add(jButton2, null);
contentPane.add(jLabel2, null);
contentPane.add(jScrollPane1, null);
jScrollPane1.getViewport().add(sign, null);
contentPane.add(jButton3, null);
contentPane.add(jLabel6, null);
contentPane.add(jLabel7, null);
contentPane.add(jButton4, null);
contentPane.add(jLabel8, null);
contentPane.add(jButton5, null);
contentPane.add(jLabel4, null);
contentPane.add(jLabel5, null);
contentPane.add(jLabel10, null);
contentPane.add(jLabel11, null);
contentPane.add(getquestion, null);
contentPane.add(jLabel12, null);
contentPane.add(jLabel13, null);
contentPane.add(jLabel14, null);
contentPane.add(getanswer, null);
contentPane.add(jLabel9, null);
contentPane.add(jLabel15, null);
contentPane.add(jLabel16, null);
contentPane.add(jLabel17, null);
}
/**Overridden so we can exit when window is closed*/
protected void processWindowEvent(WindowEvent e) {
super.processWindowEvent(e);
if (e.getID() == WindowEvent.WINDOW_CLOSING) {
System.exit(0);
}
}
void jButton3_actionPerformed(ActionEvent e) {
int x,y;
write mewrite=new write(username,userpwd);
x=this.getX();
y=this.getY();
if(y<=0)
{y+=30;mewrite.setLocation(x,y);}//y
else
mewrite.setLocation(x,y);
mewrite.show();
this.dispose();
}
void jButton4_actionPerformed(ActionEvent e) {
int x,y;
readfunc myread=new readfunc(username,userpwd);
x=this.getX();
y=this.getY();
if(y<=0)
{y+=30;myread.setLocation(x,y);}//y
else
myread.setLocation(x,y);
myread.show();
this.dispose();
}
void jButton5_actionPerformed(ActionEvent e) {
String str1,str2,str3;
int r=-1;//标志位
str1=passf.getText();
str2=passf2.getText();
str3=sign.getText();
if(str1.length()<1||str2.length()<1){
JOptionPane.showMessageDialog(null,"对不起,您的密码不能为空!");
r=1;}
else if(str3.length()<1){
JOptionPane.showMessageDialog(null,"天啊,您的签名是一片空白!");
}
else if(!str1.equals(str2))
{JOptionPane.showMessageDialog(null,"对不起,您两次输入的密码不一致,请重新输入!!");}
//准备连接数据库进行修改;
else
{//在这里写入操作数据库的代码;
try{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
}//end of try
catch(Exception ex5){}
try{
String cName=namef.getText(),cPWD="";
Connection con=java.sql.DriverManager.getConnection(url,"sa","123");
String str="UPDATE friend SET fPass='"+str1+"',fSign='"+str3+"',fQuestion='"+getquestion.getText()+"',fAnswer='"+getanswer.getText()+"' WHERE fName='"+cName+"'";
Statement st=con.createStatement();
int n=st.executeUpdate(str);
String selectstr="SELECT * FROM friend WHERE fName=? AND fPass=?";
PreparedStatement ps=con.prepareStatement(selectstr);
if(n!=0){
JOptionPane.showMessageDialog(null,"恭喜你,修改资料成功了!按确定返回主面板");
ps.setString(1,namef.getText());
ps.setString(2,passf.getText());
ResultSet rs=ps.executeQuery();
//开始验证
if(rs.next()){
cName=rs.getString("fName");
cPWD=rs.getString("fPass");
//存在此编号;
int x,y;
writefunc mywrite=new writefunc(cName,cPWD);
x=this.getX();
y=this.getY();
mywrite.setLocation(x,y);
mywrite.show();
ps.close();
st.close();
con.close();
this.dispose();
//成功登陆!!
r=0;
}//end of if||
}//end of if success
else
{
check mycheck=new check();
if(mycheck.checkup(str1))JOptionPane.showMessageDialog(null,"不好意思,你申请的用户名已被注册,请重新申请!");
else
{JOptionPane.showMessageDialog(null,"这个用户名还没有被注册,但是程序出错了!");
}
ps.close();
st.close();
con.close(); }
///
}//end of try2
catch(SQLException ex6){
JOptionPane.showMessageDialog(null,"程序出错了:你的签名字数太长了!"+ex6.getMessage());}
}//end of database operation
}
void this_windowOpened(WindowEvent e) {
namef.setText(username);
/连接数据库;
try{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
}//end of try1
catch(Exception ee3){}
try{
//creat connection
Connection con=DriverManager.getConnection(url,uname,pass);
//creat statement
Statement st=con.createStatement();
String selectstr="SELECT * FROM friend WHERE fName='"+username+"'";
ResultSet rs=st.executeQuery(selectstr);
if(rs.next()){
String passstr=rs.getString("fPass");
sign.setText(rs.getString("fSign"));
passf.setText(passstr);
passf2.setText(passstr);}
getquestion.setText(rs.getString("fQuestion"));
getanswer.setText(rs.getString("fAnswer"));
rs.close();
st.close();
con.close();
}//end of try2
catch(Exception ce){JOptionPane.showMessageDialog(null,"无法显示个人签名,请重新登陆!"+ce.getMessage());}
}
JLabel jLabel11 = new JLabel();
JLabel jLabel9 = new JLabel();
JLabel jLabel12 = new JLabel();
JLabel jLabel13 = new JLabel();
JTextField getquestion = new JTextField();
JLabel jLabel14 = new JLabel();
JTextField getanswer = new JTextField();
JLabel jLabel15 = new JLabel();
JLabel jLabel16 = new JLabel();
JLabel jLabel17 = new JLabel();
void this_componentShown(ComponentEvent e) {
//设置我自己的LOGO!
Image img=Toolkit.getDefaultToolkit().getImage("E://javatest//customer//myheart.jpg");
this.setIconImage(img);
}
}
//写日记模块
package customer;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.sql.*;
import java.sql.DriverManager;
import javax.swing.border.*;
import java.util.*;
import java.text.SimpleDateFormat;
import java.io.*;
/**
* Title: customerForMysql
* Description: my java project on jdbc.
* Copyright: Copyright (c) 2005
* Company: stonehouse
* @author stone
* @version 1.0
*/
class write extends JFrame {
JPanel contentPane;
JMenuBar jMenuBar1 = new JMenuBar();
String url="jdbc:odbc:testDSN";
JButton jButton2 = new JButton();
TitledBorder titledBorder1;
JLabel jLabel4 = new JLabel();
JLabel jLabel5 = new JLabel();
JLabel inputName = new JLabel();
JLabel jLabel6 = new JLabel();
JLabel jLabel7 = new JLabel();
JButton jButton4 = new JButton();
JButton jButton5 = new JButton();
JLabel jLabel1 = new JLabel();
JLabel jLabel3 = new JLabel();
JComboBox boxMood = new JComboBox();
JLabel jLabel8 = new JLabel();
JTextField jTitle = new JTextField();
JScrollPane jScrollPane1 = new JScrollPane();
JTextPane dText = new JTextPane();
JLabel jLabel9 = new JLabel();
JButton jButton1 = new JButton();
JComboBox boxWeather = new JComboBox();
String username,usercode,userpwd;
//declare myvector for boxchoic;
Vector myvector=new Vector();
JTextField dDate = new JTextField();
JLabel jLabel2 = new JLabel();
/**Construct the frame*/
write(String inputname,String inputpwd) {
enableEvents(AWTEvent.WINDOW_EVENT_MASK);
try {
username=inputname;
userpwd=inputpwd;
jbInit();
}
catch(Exception e) {
e.printStackTrace();
}
}
/**Component initialization*/
private void jbInit() throws Exception {
//setIconImage(Toolkit.getDefaultToolkit().createImage(Frame1.class.getResource("[Your Icon]")));
contentPane = (JPanel) this.getContentPane();
titledBorder1 = new TitledBorder("");
contentPane.setLayout(null);
this.setJMenuBar(jMenuBar1);
this.setResizable(false);
this.setSize(new Dimension(400, 503));
this.setTitle("心情留言坊--写日记");
this.addComponentListener(new java.awt.event.ComponentAdapter() {
public void componentShown(ComponentEvent e) {
this_componentShown(e);
}
});
this.addWindowListener(new java.awt.event.WindowAdapter() {
public void windowOpened(WindowEvent e) {
this_windowOpened(e);
}
});
contentPane.setBackground(SystemColor.inactiveCaptionText);
contentPane.setForeground(Color.red);
contentPane.setToolTipText("");
jMenuBar1.setBackground(UIManager.getColor("text"));
jButton2.setBorder(BorderFactory.createEtchedBorder());
jButton2.setIcon(new ImageIcon(new java.net.URL("file:///D:/customer/moodrest.JPG")));
jButton2.setBounds(new Rectangle(66, 18, 249, 56));
jLabel4.setFont(new java.awt.Font("Serif", 0, 13));
jLabel4.setText("2005 石头工作室 出品");
jLabel4.setBounds(new Rectangle(119, 413, 147, 18));
jLabel5.setFont(new java.awt.Font("Dialog", 0, 25));
jLabel5.setText("/u00AE");
jLabel5.setBounds(new Rectangle(148, 415, 15, 17));
inputName.setFont(new java.awt.Font("Dialog", 0, 23));
inputName.setForeground(Color.red);
inputName.setBounds(new Rectangle(7, 157, 107, 22));
jLabel6.setText("1.");
jLabel6.setBounds(new Rectangle(8, 359, 19, 20));
jLabel7.setText("2.");
jLabel7.setBounds(new Rectangle(116, 356, 22, 22));
jButton4.setText("看日记");
jButton4.setBounds(new Rectangle(24, 356, 87, 22));
jButton4.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(ActionEvent e) {
jButton4_actionPerformed(e);
}
});
jButton5.setText("修改个人资料");
jButton5.setBounds(new Rectangle(146, 357, 108, 23));
jButton5.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(ActionEvent e) {
jButton5_actionPerformed(e);
}
});
jLabel1.setFont(new java.awt.Font("Serif", 0, 15));
jLabel1.setText("天气:");
jLabel1.setBounds(new Rectangle(22, 84, 38, 26));
jLabel3.setFont(new java.awt.Font("Serif", 0, 15));
jLabel3.setText("心情:");
jLabel3.setBounds(new Rectangle(233, 85, 40, 19));
boxMood.setEditable(true);
boxMood.setBounds(new Rectangle(276, 80, 85, 25));
jLabel8.setFont(new java.awt.Font("Monospaced", 1, 15));
jLabel8.setText("标题:");
jLabel8.setBounds(new Rectangle(105, 114, 46, 20));
jTitle.setText("无题");
jTitle.setHorizontalAlignment(SwingConstants.CENTER);
jTitle.setBounds(new Rectangle(157, 111, 91, 24));
jScrollPane1.setBounds(new Rectangle(15, 140, 353, 211));
dText.setSelectedTextColor(Color.blue);
dText.setSelectionColor(SystemColor.menu);
dText.setText(" ");
jLabel9.setText("3.");
jLabel9.setBounds(new Rectangle(262, 360, 19, 17));
jButton1.setBackground(SystemColor.text);
jButton1.setFont(new java.awt.Font("DialogInput", 0, 25));
jButton1.setForeground(SystemColor.windowBorder);
jButton1.setText("保存");
jButton1.setBounds(new Rectangle(280, 359, 92, 55));
jButton1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(ActionEvent e) {
jButton1_actionPerformed(e);
}
});
boxWeather.setEditable(true);
boxWeather.setBounds(new Rectangle(61, 84, 106, 23));
dDate.setBounds(new Rectangle(109, 387, 146, 23));
jLabel2.setFont(new java.awt.Font("SansSerif", 0, 15));
jLabel2.setForeground(Color.red);
jLabel2.setBorder(BorderFactory.createEtchedBorder());
jLabel2.setHorizontalAlignment(SwingConstants.CENTER);
jLabel2.setText("现在时间:");
jLabel2.setBounds(new Rectangle(25, 388, 80, 23));
contentPane.add(jButton2, null);
contentPane.add(inputName, null);
contentPane.add(jTitle, null);
contentPane.add(jLabel8, null);
contentPane.add(jScrollPane1, null);
contentPane.add(jLabel6, null);
contentPane.add(jButton4, null);
contentPane.add(jLabel4, null);
contentPane.add(jLabel5, null);
contentPane.add(jButton5, null);
contentPane.add(jLabel7, null);
contentPane.add(jLabel9, null);
contentPane.add(jButton1, null);
contentPane.add(boxWeather, null);
contentPane.add(jLabel1, null);
contentPane.add(boxMood, null);
contentPane.add(jLabel3, null);
contentPane.add(dDate, null);
contentPane.add(jLabel2, null);
jScrollPane1.getViewport().add(dText, null);
}
/**Overridden so we can exit when window is closed*/
protected void processWindowEvent(WindowEvent e) {
super.processWindowEvent(e);
if (e.getID() == WindowEvent.WINDOW_CLOSING) {
System.exit(0);
}
}
void jButton5_actionPerformed(ActionEvent e) {
int x,y;
mymodify mymod=new mymodify(username,userpwd);
x=this.getX();
y=this.getY();
while(y<=0)
{y+=30;}//y
mymod.setLocation(x,y);
mymod.show();
this.dispose();
}
void this_windowOpened(WindowEvent e) {
boxWeather.addItem("晴朗");
boxWeather.addItem("阴天");
boxWeather.addItem("雨天");
//给心情提供选项
boxMood.addItem("爽呆了");
boxMood.addItem("正常");
boxMood.addItem("郁闷");
boxMood.addItem("伤心");
//写上日期
java.util.Date myDate=new java.util.Date();
SimpleDateFormat myformat=new SimpleDateFormat("yyyy年M月dd日 EEEE");
dDate.setText(myformat.format(myDate));
}
void jButton4_actionPerformed(ActionEvent e) {
int x,y;
readfunc myread=new readfunc(username,userpwd);
x=this.getX();
y=this.getY();
if(y<=0)
{y+=30;myread.setLocation(x,y);}//y
else
myread.setLocation(x,y);
myread.show();
this.dispose();
}
void jButton1_actionPerformed(ActionEvent e) {
//准备连接数据库;
try{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
}//end of try
catch(Exception ex3){}
try{
//取出所填入的日记相关信息
String str2="",str3="",str4="",str5="",strname=username,surestr="";
String str1="";
//
///
str1=dText.getText();
str2=jTitle.getText();
try{str3=dDate.getText();
}//end of try
catch(Exception ex7){JOptionPane.showMessageDialog(null,"出错了: "+ex7+str3);}
str4=String.valueOf(boxMood.getSelectedItem());
str5=String.valueOf(boxWeather.getSelectedItem());
///
Connection con=java.sql.DriverManager.getConnection(url,"","");
//String str="INSERT INTO diary( dTitle,dDate,dMood,dContent) values('"+str2+"','"+str3+"','"+str4+"','"+str1+"')";
Statement st=con.createStatement();
int n=st.executeUpdate("INSERT INTO diary(dAuthor,dTitle,dDate,dMood,dWeather,dContent) values('"+username+"','"+str2+"','"+str3+"','"+str4+"','"+str5+"','"+str1+"')");
//显示结果;
st.close();
con.close();
if(n!=0) {JOptionPane.showMessageDialog(null,"添加日记成功,喝一杯吧!");
dText.setText(""); }
else
JOptionPane.showMessageDialog(null,"发生错误了!你最好保存过了.");
}//end of try2
catch(Exception ex7){
JOptionPane.showMessageDialog(this,"出错了:/n"+username+ex7.getMessage());}//end of catch
}
void this_componentShown(ComponentEvent e) {
//设置我自己的LOGO!
Image img=Toolkit.getDefaultToolkit().getImage("C://Documents and Settings//stone//jbproject//customer//myheart.jpg");
this.setIconImage(img);
}}
//注册模块
package customer;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.sql.*;
import java.sql.DriverManager;
import javax.swing.border.*;
/**
* Title: customerForMysql
* Description: my java project on jdbc.
* Copyright: Copyright (c) 2005
* Company: stonehouse
* @author stone
* @version 1.0
*/
class register extends JFrame {
JPanel contentPane;
JMenuBar jMenuBar1 = new JMenuBar();
String url="jdbc:odbc:testDSN";
JButton jButton2 = new JButton();
TitledBorder titledBorder1;
JLabel jLabel4 = new JLabel();
JLabel jLabel5 = new JLabel();
JLabel inputName = new JLabel();
JLabel jLabel3 = new JLabel();
JButton jButton5 = new JButton();
TitledBorder titledBorder2;
TitledBorder titledBorder3;
JLabel jLabel2 = new JLabel();
JLabel jLabel10 = new JLabel();
JTextField namef = new JTextField();
JScrollPane jScrollPane1 = new JScrollPane();
JTextArea sign = new JTextArea();
JPasswordField passf = new JPasswordField();
JPasswordField passf2 = new JPasswordField();
String username,usercode,userpwd;
/**Construct the frame*/
register() {
enableEvents(AWTEvent.WINDOW_EVENT_MASK);
try {
jbInit();
}
catch(Exception e) {
e.printStackTrace();
}
}
/**Component initialization*/
private void jbInit() throws Exception {
//setIconImage(Toolkit.getDefaultToolkit().createImage(Frame1.class.getResource("[Your Icon]")));
contentPane = (JPanel) this.getContentPane();
titledBorder1 = new TitledBorder("");
titledBorder2 = new TitledBorder("");
titledBorder3 = new TitledBorder("");
contentPane.setLayout(null);
this.setJMenuBar(jMenuBar1);
this.setResizable(false);
this.setSize(new Dimension(400, 556));
this.setTitle("心情留言坊--修改资料");
this.addComponentListener(new java.awt.event.ComponentAdapter() {
public void componentShown(ComponentEvent e) {
this_componentShown(e);
}
});
this.addWindowListener(new java.awt.event.WindowAdapter() {
public void windowOpened(WindowEvent e) {
this_windowOpened(e);
}
});
contentPane.setBackground(SystemColor.inactiveCaptionText);
contentPane.setForeground(Color.red);
contentPane.setToolTipText("");
jMenuBar1.setBackground(UIManager.getColor("text"));
jButton2.setBorder(BorderFactory.createEtchedBorder());
jButton2.setIcon(new ImageIcon(new java.net.URL("file:///D:/customer/moodrest.JPG")));
jButton2.setBounds(new Rectangle(57, 22, 249, 59));
jLabel4.setFont(new java.awt.Font("Serif", 0, 13));
jLabel4.setBorder(BorderFactory.createLineBorder(Color.black));
jLabel4.setText("2005 石头工作室 出品");
jLabel4.setBounds(new Rectangle(115, 461, 147, 18));
jLabel5.setFont(new java.awt.Font("Dialog", 0, 25));
jLabel5.setText("/u00AE");
jLabel5.setBounds(new Rectangle(143, 461, 15, 22));
inputName.setFont(new java.awt.Font("SansSerif", 0, 20));
inputName.setHorizontalAlignment(SwingConstants.CENTER);
inputName.setText("密 码:");
inputName.setBounds(new Rectangle(36, 117, 98, 26));
jLabel3.setFont(new java.awt.Font("SansSerif", 0, 20));
jLabel3.setHorizontalAlignment(SwingConstants.CENTER);
jLabel3.setText("用 户 名:");
jLabel3.setBounds(new Rectangle(27, 93, 119, 26));
jButton5.setBackground(Color.lightGray);
jButton5.setFont(new java.awt.Font("Dialog", 0, 20));
jButton5.setToolTipText("改改再说~");
jButton5.setText("确认注册");
jButton5.setBounds(new Rectangle(122, 377, 123, 60));
jButton5.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(ActionEvent e) {
jButton5_actionPerformed(e);
}
});
jLabel2.setFont(new java.awt.Font("SansSerif", 0, 20));
jLabel2.setText("确认密码:");
jLabel2.setBounds(new Rectangle(43, 143, 86, 25));
jLabel10.setFont(new java.awt.Font("Dialog", 0, 20));
jLabel10.setBorder(BorderFactory.createEtchedBorder());
jLabel10.setText("个性签名:");
jLabel10.setBounds(new Rectangle(29, 229, 90, 23));
namef.setFont(new java.awt.Font("SansSerif", 0, 15));
namef.setToolTipText("用户名不能修改!");
namef.setHorizontalAlignment(SwingConstants.CENTER);
namef.setBounds(new Rectangle(136, 94, 89, 22));
jScrollPane1.setBounds(new Rectangle(56, 260, 262, 98));
sign.setLineWrap(true);
sign.setText(" 这人好懒,什么都没写哦!");
passf.setHorizontalAlignment(SwingConstants.CENTER);
passf.setBounds(new Rectangle(136, 123, 187, 22));
passf2.setHorizontalAlignment(SwingConstants.CENTER);
passf2.setBounds(new Rectangle(135, 149, 189, 22));
jButton1.setBackground(Color.orange);
jButton1.setFont(new java.awt.Font("Dialog", 0, 13));
jButton1.setText("有重名吗?");
jButton1.setBounds(new Rectangle(229, 91, 94, 26));
jButton1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(ActionEvent e) {
jButton1_actionPerformed(e);
}
});
jLabel1.setFont(new java.awt.Font("Dialog", 0, 15));
jLabel1.setForeground(Color.red);
jLabel1.setText("*");
jLabel1.setBounds(new Rectangle(333, 122, 24, 24));
jLabel6.setBounds(new Rectangle(334, 146, 24, 24));
jLabel6.setText("*");
jLabel6.setForeground(Color.red);
jLabel6.setFont(new java.awt.Font("Dialog", 0, 15));
jLabel7.setFont(new java.awt.Font("Dialog", 0, 15));
jLabel7.setForeground(Color.red);
jLabel7.setText("取回密码问题:");
jLabel7.setBounds(new Rectangle(39, 172, 101, 27));
getquestion.setToolTipText("可以帮助你取回密码!");
getquestion.setText("我最喜欢的食物?");
getquestion.setBounds(new Rectangle(137, 176, 188, 21));
jLabel8.setFont(new java.awt.Font("Dialog", 0, 15));
jLabel8.setForeground(Color.red);
jLabel8.setHorizontalAlignment(SwingConstants.CENTER);
jLabel8.setText("答案:");
jLabel8.setBounds(new Rectangle(41, 199, 86, 20));
getanswer.setToolTipText("牢记你的答案!");
getanswer.setBounds(new Rectangle(136, 200, 190, 22));
jLabel9.setFont(new java.awt.Font("Dialog", 0, 15));
jLabel9.setForeground(Color.red);
jLabel9.setText("*");
jLabel9.setBounds(new Rectangle(333, 174, 24, 24));
jLabel11.setFont(new java.awt.Font("Dialog", 0, 15));
jLabel11.setForeground(Color.red);
jLabel11.setText("*");
jLabel11.setBounds(new Rectangle(333, 200, 24, 24));
jLabel12.setFont(new java.awt.Font("Dialog", 0, 15));
jLabel12.setForeground(Color.red);
jLabel12.setText("*");
jLabel12.setBounds(new Rectangle(333, 94, 24, 24));
contentPane.add(passf2, null);
contentPane.add(jLabel3, null);
contentPane.add(inputName, null);
contentPane.add(passf, null);
contentPane.add(jButton2, null);
contentPane.add(jLabel2, null);
contentPane.add(jButton1, null);
contentPane.add(namef, null);
contentPane.add(jScrollPane1, null);
contentPane.add(jButton5, null);
contentPane.add(jLabel4, null);
contentPane.add(jLabel5, null);
contentPane.add(jLabel1, null);
contentPane.add(jLabel6, null);
contentPane.add(getquestion, null);
contentPane.add(jLabel7, null);
contentPane.add(jLabel10, null);
contentPane.add(jLabel8, null);
contentPane.add(getanswer, null);
contentPane.add(jLabel9, null);
contentPane.add(jLabel11, null);
contentPane.add(jLabel12, null);
jScrollPane1.getViewport().add(sign, null);
}
/**Overridden so we can exit when window is closed*/
protected void processWindowEvent(WindowEvent e) {
super.processWindowEvent(e);
if (e.getID() == WindowEvent.WINDOW_CLOSING) {
System.exit(0);
}
}
void jButton3_actionPerformed(ActionEvent e) {
int x,y;
write mewrite=new write(username,userpwd);
x=this.getX();
y=this.getY();
if(y<=0)
{y+=30;mewrite.setLocation(x,y);}//y
else
mewrite.setLocation(x,y);
mewrite.show();
this.dispose();
}
void jButton4_actionPerformed(ActionEvent e) {
int x,y;
readfunc myread=new readfunc(username,userpwd);
x=this.getX();
y=this.getY();
if(y<=0)
{y+=30;myread.setLocation(x,y);}//y
else
myread.setLocation(x,y);
myread.show();
this.dispose();
}
void jButton5_actionPerformed(ActionEvent e) {
String str1,str2,str3;
int r=-1;//标志位
str1=passf.getText();
str2=passf2.getText();
str3=sign.getText();
if(str1.length()<1||str2.length()<1){
JOptionPane.showMessageDialog(null,"对不起,您的密码不能为空!");
r=1;}
else if(str3.length()<1){
JOptionPane.showMessageDialog(null,"天啊,您的签名是一片空白!");
}
else if(!str1.equals(str2))
{JOptionPane.showMessageDialog(null,"对不起,您两次输入的密码不一致,请重新输入!!");}
//准备连接数据库进行修改;
else
{//在这里写入操作数据库的代码;
try{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
}//end of try
catch(Exception ex){}
try{
Connection con=java.sql.DriverManager.getConnection(url,"sa","");
String str="INSERT INTO friend(fName,fPass,fSign,fQuestion,fAnswer) Values(?,?,?,?,?)";
PreparedStatement ps=con.prepareStatement(str);
ps.setString(1,namef.getText());
ps.setString(3,sign.getText());
ps.setString(2,passf.getText());
ps.setString(4,getquestion.getText());
ps.setString(5,getanswer.getText());
//执行语句
int n=ps.executeUpdate();
if(n!=0){
JOptionPane.showMessageDialog(null,"添加成员成功,喝一杯吧!");
ps.close();
con.close();
int x,y;
writefunc mywrite=new writefunc(namef.getText(),passf.getText());
x=this.getX();
y=this.getY();
if(y<0)y+=20;
mywrite.setLocation(x,y);
mywrite.show();
this.dispose();
}
else{JOptionPane.showMessageDialog(null,"发生错误了!");
ps.close();
con.close();}
}//end of try2
catch(Exception ex1){JOptionPane.showMessageDialog(null,"发生错误了:"+ex1.getMessage());}
}//end of database operation
}
void this_windowOpened(WindowEvent e) {
}
JButton jButton1 = new JButton();
JLabel jLabel1 = new JLabel();
JLabel jLabel6 = new JLabel();
JLabel jLabel7 = new JLabel();
JTextField getquestion = new JTextField();
JLabel jLabel8 = new JLabel();
JTextField getanswer = new JTextField();
JLabel jLabel9 = new JLabel();
JLabel jLabel11 = new JLabel();
JLabel jLabel12 = new JLabel();
void jButton1_actionPerformed(ActionEvent e) {
check mycheck1=new check();
if(mycheck1.checkup(namef.getText()))
JOptionPane.showMessageDialog(null,"不好意思,用户名已被注册!");
else
JOptionPane.showMessageDialog(null,"恭喜你,该用户名还没有被注册!");
}
void this_componentShown(ComponentEvent e) {
//设置我自己的LOGO!
Image img=Toolkit.getDefaultToolkit().getImage("C://Documents and Settings//stone//jbproject//customer//myheart.jpg");
this.setIconImage(img);
}
}
//验证登陆模块
package customer;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.sql.*;
import java.sql.DriverManager;
import javax.swing.border.*;
import java.util.*;
/**
* Title: customerForMysql
* Description: my java project on jdbc.
* Copyright: Copyright (c) 2005
* Company: stonehouse
* @author stone
* @version 1.0
*/
public class check{
String url="jdbc:odbc:testDSN";
public boolean checkup(String aname){
try{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver"); }//end of try
catch(Exception ee){}
try{
Connection con=java.sql.DriverManager.getConnection(url,"","");
String str="SELECT * FROM friend WHERE fName=?";
PreparedStatement ps=con.prepareStatement(str);
ps.setString(1,aname);
ResultSet rs=ps.executeQuery();
if(rs.next())
{
ps.close();
con.close();
return true;
}//end of if not exist
else
{
ps.close();
con.close();
return false;
}
}//end of try2
catch(Exception ee1){JOptionPane.showMessageDialog(null,ee1.getMessage()+" 似乎什么地方出了差错哦!");return false;}
} //end of checkup if the name exists
}//end of class
/
//结束.
很冗长,还要构造一个mydb.mdb的数据库,我用的是ACCESS,以上代码仅供同学习和参考.