//先创建登录界面
package com.txl.jm;
import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.ButtonBuilder;
import javafx.scene.control.Label;
import javafx.scene.layout.GridPane;
import javafx.stage.Stage;
public class denglu extends Application{
   
 public void  start( final Stage primaryStage) {
       GridPane pane= new GridPane();
      pane.setAlignment(Pos.CENTER);
      pane.setPadding(new Insets(50));
      pane.setHgap(5);
      pane.setVgap(5);
      
      final javafx.scene.control.TextField yhm=new javafx.scene.control.TextField();
      final javafx.scene.control.TextField mm=new javafx.scene.control.TextField();
      
      pane.add(new javafx.scene.control.Label("用户名"),0,0);
      pane.add(yhm,1,0);
 
      pane.add(new Label("密码"),0,1);
      pane.add(mm,1,1);
      Button    button1 = ButtonBuilder.create().text("登陆").onAction(new EventHandler()
      {         public void handle(ActionEvent e){ 
        String a=yhm.getText();
        String b=mm.getText();
       if("8".equals(a)&&"8".equals(b)){
        javafx.application.Platform.runLater(new Runnable() {//Platform方法实现界面跳转
         
         public void run() {
           new jiemian().start(new Stage());
         }
        });
       } else{
              javafx.application.Platform.runLater(new Runnable() {//Platform方法实现界面跳转
         public void run() {     
          new jieshu().start(new Stage());
         }
        });
              }
       primaryStage.close();
       System.out.println("button  clicked");
       }       }).build();  
      pane.add(button1,2,3);
   Scene scene=new Scene(pane);
   primaryStage.setTitle("通讯录");
   primaryStage.setScene(scene);
   primaryStage.show();
  }
public static void main(String[] args) {
  
  Application.launch(args);
 }
}
//创建用户
package com.txl.jm;
public class person {
 String id;
 String dh;
 String xm;
 public String getId() {
  return id;
 }
 public void setId(String id) {
  this.id = id;
 }
 public String getDh() {
  return dh;
 }
 public void setDh(String dh) {
  this.dh = dh;
 }
 public String getXm() {
  return xm;
 }
 public void setXm(String xm) {
  this.xm = xm;
 }
 public  void people(String xm,String dh, String id) {
  this.setDh(dh);
  this.setId(id);
  this.setXm(xm);  
 }
}

//创建主界面

package com.txl.jm;

import java.sql.SQLException;
import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.layout.GridPane;
import javafx.stage.Stage;

public class jiemian extends Application{
 
 javafx.scene.control.TextField cx=new javafx.scene.control.TextField();
 javafx.scene.control.TextField mz=new javafx.scene.control.TextField();
 javafx.scene.control.TextField xh=new javafx.scene.control.TextField();
 javafx.scene.control.TextField dh=new javafx.scene.control.TextField();

 
 public void start(Stage primaryStage) {
    //窗体
     GridPane pane= new GridPane();
     pane.setAlignment(Pos.CENTER);
     pane.setPadding(new Insets(11,12,13,14));
     pane.setHgap(5);
     pane.setVgap(5);
    
     pane.add(new Label("查询学号"),0,0);
     pane.add(cx,1,0);
     Button button2=new Button("查询");
     OKHandlerClass handler1=new OKHandlerClass();
     button2.setOnAction(handler1);
  pane.add(button2,2,0);
    
     pane.add(new javafx.scene.control.Label("名字:"),0,1);
     pane.add(mz,1,1);
        
     pane.add(new Label("学号:"),0,2);
     pane.add(xh,1,2);
    
     pane.add(new Label("电话:"),0,3);
     pane.add(dh,1,3);
    
     Button button1=new Button("添加");
     pane.add(button1,2,3);
    OKHandlerClass1 handler2=new OKHandlerClass1();
     button1.setOnAction(handler2);
    
     Button button3=new Button("删除");
     pane.add(button3,2,2);
     OKHandlerClass2 handler3=new OKHandlerClass2();
     button3.setOnAction(handler3);
    
     Button button4=new Button("修改");
     pane.add(button4,2,1);
     OKHandlerClass3 handler4=new OKHandlerClass3();
     button4.setOnAction(handler4);
    
     Scene scene1=new Scene(pane);
  primaryStage.setTitle("通讯录");
  primaryStage.setScene(scene1);
  primaryStage.show();
  
 }
 class OKHandlerClass implements EventHandler{
  public void handle(ActionEvent e) {
   search();
   System.out.println("查询button  clicked");
   
  }
  
 }
 class OKHandlerClass1 implements EventHandler{
  public void handle(ActionEvent e) {
   tianjia();
   System.out.println("添加 button clicked");
   
  }
  
 }
 
 class OKHandlerClass2 implements EventHandler{
  public void handle(ActionEvent e) {
   delete();
   System.out.println("删除 button clicked");
   }
 }
 
 class OKHandlerClass3 implements EventHandler{
  public void handle(ActionEvent e) {
    update();
   System.out.println("修改button clicked");
    }
  }
 private void search(){
  person ren=new person();
  ren.id=cx.getText();
  mysqljdbc a=new mysqljdbc();
  a.initializeDB();

  try {
   String queryString="select * from stu where id='"+ren.id+"'";
  a.preStmt = a.connect.prepareStatement(queryString);
           a.rSet=a.preStmt.executeQuery();
        if(a.rSet.next()){
             String name1=a.rSet.getString(1);
                String xh1=a.rSet.getString(2);
             String txl1=a.rSet.getString(3);
             xh.setText(name1);
             dh.setText(txl1);
             mz.setText(xh1); 
            } 
        exit1();
  } catch (Exception e) {
  }
  }
 private void tianjia(){
  person ren1=new person();
  ren1.xm=mz.getText();
  ren1.id=xh.getText();
  ren1.dh=dh.getText();
 
  try {
   mysqljdbc b1=new mysqljdbc();
   b1.initializeDB();
   String queryString="insert into stu values(?,?,?)";
      b1.preStmt = b1.connect.prepareStatement(queryString);
      b1.preStmt.setString(2, ren1.xm);
      b1.preStmt.setString(1, ren1.id);
      b1.preStmt.setString(3, ren1.dh);
      b1.preStmt.executeUpdate();  
      exit1();
  } catch (Exception e) {
  }
  }
   public int delete()   { 
    person ren2=new person();
  ren2.id=xh.getText();
  
        String sql = "delete from stu where id='"+ren2.id+"'"; 
     
        int i=0;  
        try  {
           mysqljdbc cMysqljdbc=new mysqljdbc();
              cMysqljdbc.initializeDB();
            cMysqljdbc.preStmt =  cMysqljdbc.connect.prepareStatement(sql);
            i =  cMysqljdbc.preStmt.executeUpdate(); 
            exit1();
        } 
        catch (SQLException e) 
        { 
            e.printStackTrace(); 
        } 
        return i;//如果返回的是1,则执行成功; 
    } 
    public int update()
    {  person aPerson=new person();
        aPerson.xm=mz.getText();
     aPerson.id=xh.getText();
     aPerson.dh=dh.getText();
        int i=0; 
      
        String sql="update stu set name=?,txl=?,id=?where id='"+aPerson.id+"'";//注意要有where条件 
        try{ 
          mysqljdbc cMysqljdbc=new mysqljdbc();
             cMysqljdbc.initializeDB();
         cMysqljdbc.preStmt =cMysqljdbc.connect.prepareStatement(sql); 
         cMysqljdbc.preStmt.setString(1, aPerson.xm);
         cMysqljdbc. preStmt.setString(2, aPerson.dh);
         cMysqljdbc.preStmt.setString(3, aPerson.id);
            i=cMysqljdbc.preStmt.executeUpdate(); 
            exit1();
        } 
        catch (SQLException e) 
        { 
            e.printStackTrace(); 
        } 
        return i;//返回影响的行数,1为执行成功 
    } 
   
public void exit1()   {
 mysqljdbc a=new mysqljdbc();
 if(a.rSet != null){   // 关闭记录集   
        try{   
            a.rSet.close() ;   
        }catch(SQLException e){   
            e.printStackTrace() ;   
        }   
          }   
           if(a.preStmt != null){   // 关闭声明   
         try{   
          a.preStmt.close() ;   
         }catch(SQLException e){   
             e.printStackTrace() ;   
         }   
           }   
           if(a.connect != null){  // 关闭连接对象   
          try{   
             a.connect.close() ;   
          }catch(SQLException e){   
             e.printStackTrace() ;   
          }   
           } 
    } 
   
public static void main(String[] args) {
  
  Application.launch(args);
  
 }
}

//创建结束界面

package com.txl.jm;

import javafx.application.Application;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Label;
import javafx.scene.layout.GridPane;
import javafx.stage.Stage;

public class jieshu extends Application{

 public void start(Stage primaryStage) {
  //窗体
     GridPane pane= new GridPane();
     pane.setAlignment(Pos.CENTER);
     pane.setPadding(new Insets(11,12,13,14));
     pane.setHgap(5);
     pane.setVgap(5);
    
     Label button1=new Label("密码错误");
     pane.add(button1,1,1);
  Scene scene=new Scene(pane);
  primaryStage.setTitle("通讯录");
  primaryStage.setScene(scene);
  primaryStage.show();
 }
public static void main(String[] args) {
  
  Application.launch(args);
 }
}

//创建数据库连接

package com.txl.jm;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;

class mysqljdbc {
  Connection connect;
  ResultSet rSet;
  java.sql.PreparedStatement preStmt;
 public void initializeDB(){
  try {
        Class.forName("com.mysql.jdbc.Driver");     //加载MYSQL JDBC驱动程序  
        //Class.forName("org.gjt.mm.mysql.Driver");
       System.out.println("Success loading Mysql Driver!");
       connect= DriverManager.getConnection(
            "","","");
             //连接URL为   jdbc:mysql//服务器地址/数据库名  ,后面的2个参数分别是登陆用户名和密码

        System.out.println("Success connect Mysql server!");
      }
      catch (Exception e) {
       e.printStackTrace();
      }
  }
}

//新手写的,请指教,谢谢。