1. 程式人生 > 其它 >java連線資料庫(4)--teacher

java連線資料庫(4)--teacher

package teacher;
 
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Scanner;
 
 
public class add  {
    Scanner scan=new Scanner(System.in);
    private static final long serialVersionUID = -1928970409928880648L;  //
用於版本控制 public void add() { System.out.println("輸入宿舍號"); String did =scan.next(); System.out.println("輸入用電量"); int used=scan.nextInt(); System.out.println("輸入總電量"); int total =scan.nextInt(); System.out.println("輸入學生要交的電費"); int count=scan.nextInt(); System.out.println(
"學生是否交錢"); String money =scan.next(); //Add Connection conn = null; Statement stat = null; PreparedStatement ps=null; String sql = "INSERT INTO teacher(did,used,total,count,money) " + "values(?,?,?,?,?)";
try{ Class.forName("com.mysql.cj.jdbc.Driver"); System.out.println("成功"); }catch(Exception a){ System.out.println("失敗"); a.printStackTrace(); } try{ conn=DriverManager.getConnection("jdbc:mysql://localhost:3306/usefull","root","root"); ps=conn.prepareStatement(sql); ps.setString(1,did); ps.setInt(2, used); ps.setInt(3, total); ps.setInt(4, count); ps.setString(5, money); ps.executeUpdate(); //System.out.println("MySQL 連線成功!"); //stat = conn.createStatement(); //stat.executeUpdate(sql); //System.out.println("插入資料成功!");
package teacher;

import java.awt.event.ActionEvent;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Scanner;

public class change {

    
    
    public void change(){
        
        Scanner scan=new Scanner(System.in);
        Connection conn = null;
        ResultSet res = null;
        Statement stat = null;
        System.out.println("輸入要修改的宿舍id");
            String did =scan.next();
            System.out.println("輸入要更改成的用電量");
            int used=scan.nextInt();
                System.out.println("輸入要改成的總電量");
            int total=scan.nextInt();
                System.out.println("輸入要改成的電費");
                int count=scan.nextInt();
                System.out.println("學生是否交錢");
                String money =scan.next();
        String sql = "SELECT did,used,total,count,money FROM teacher;";
        try{
            Class.forName("com.mysql.cj.jdbc.Driver");
            
        }catch(Exception d){
            System.out.println("jdbc fall");
            d.printStackTrace();
        }
        try{
            conn=DriverManager.getConnection("jdbc:mysql://localhost:3306/usefull","root","root");
            stat=conn.createStatement();
            res=stat.executeQuery(sql);
            while (res.next())
            {
                //change
                if (res.getString(1).equals(did))
                {
                try{
                    Class.forName("com.mysql.jdbc.Driver");
                }catch(Exception d){
                    System.out.println("jdbc fall");
                    d.printStackTrace();
                }
                    
                String sql2="UPDATE teacher SET used='"+used+"'  WHERE did='"+did+"'";
                String sql3="UPDATE teacher SET total='"+total+"'  WHERE did='"+did+"'";
                String sql4="UPDATE teacher SET count='"+count+"'  WHERE did='"+did+"'";
                String sql5="UPDATE teacher SET monty='"+money+"'  WHERE did='"+did+"'";
                    try {
                        conn=DriverManager.getConnection("jdbc:mysql://localhost:3306/usefull","root","root");
                        stat=conn.createStatement();
                        stat.executeUpdate(sql2);
                        stat.executeUpdate(sql3);
                        stat.executeUpdate(sql4);
                        stat.executeUpdate(sql5);
                        
                    } catch (SQLException g) {
                        // TODO Auto-generated catch block
                        g.printStackTrace();
                    }try{
                        stat.close();
                        conn.close();
                    }catch(SQLException ar){
                        ar.printStackTrace();
                }

                    break;
                }
                
            //change end
            }
        }catch (SQLException e1) {
            // TODO Auto-generated catch block
            e1.printStackTrace();
        
        
    }
        finally{
            try{
                conn.close();
            }catch(SQLException ar){
                ar.printStackTrace();
            }
    
        }
        
    }
    
    
}


package teacher;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Scanner;

public class delete {

    public void delete() {Scanner scan=new Scanner(System.in);
    System.out.println("輸入宿舍號");
    String did =scan.next();
    Connection conn = null;
    ResultSet res = null;
    Statement stat = null;
    String sql = "DELETE FROM teacher WHERE did='"+did+"'";
    
    try{
        Class.forName("com.mysql.cj.jdbc.Driver"); //載入資料庫驅動
    }catch(Exception a){
        a.printStackTrace();
    }
    try{
        conn=DriverManager.getConnection("jdbc:mysql://localhost:3306/usefull","root","root");
        stat = conn.createStatement();
        stat.executeUpdate(sql);
    }catch(SQLException h){
        h.printStackTrace();
        
    }finally{
        try{
            conn.close();
            System.out.println("關閉成功");
        }catch(SQLException j){
            System.out.println("關閉失敗");
            j.printStackTrace();
        }
        
    }
    
}
}
package teacher;

import java.awt.GridLayout;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Vector;

import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;

public class look {

    
    private static final long serialVersionUID = -1928970409928880648L;
    
    Connection conn = null;
    PreparedStatement ps = null;
    ResultSet res = null;
    
    
    public void Look() {
        
        
        try {
            Class.forName("com.mysql.cj.jdbc.Driver");
            conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/usefull","root","root");
            ps = conn.prepareStatement("SELECT * FROM teacher");
            res = ps.executeQuery();
            while (res.next())
            {
                        String did =res.getString("did");
                        int used=res.getInt("used");
                        int total=res.getInt("total");
                        int count=res.getInt("count");
                        String money=res.getString("money");
                        System.out.println("  "+did+"  "+used+"  "+total+"  "+count+ "  "+money);
                
            }
            System.out.println("成功");
        }catch (Exception q){
            q.printStackTrace();
            System.out.println("失敗");
        }finally{
            try{
                res.close();
                ps.close();
                conn.close();
                System.out.println("成功");
            }catch (SQLException o){
                o.printStackTrace();
                System.out.println("失敗");
            }
        }
}
}
package teacher;

import java.awt.GridLayout;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Vector;

import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;

public class show {

    
    private static final long serialVersionUID = -1928970409928880648L;
    
    Connection conn = null;
    PreparedStatement ps = null;
    ResultSet res = null;
    
    
    public void Look() {
        
        
        try {
            Class.forName("com.mysql.cj.jdbc.Driver");
            conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/usefull","root","root");
            ps = conn.prepareStatement("SELECT did FROM teacher where did not in(select did from student)");
            res = ps.executeQuery();
            while (res.next())
            {
                        String did =res.getString("did");
                        System.out.println("  "+did);
                
            }
            System.out.println("成功");
        }catch (Exception q){
            q.printStackTrace();
            System.out.println("失敗");
        }finally{
            try{
                res.close();
                ps.close();
                conn.close();
                System.out.println("成功");
            }catch (SQLException o){
                o.printStackTrace();
                System.out.println("失敗");
            }
        }
}
}

                }catch (SQLException b){
                    b.printStackTrace();
                }finally{
                    try{
                        conn.close();
                        System.out.println("MySQL 關閉成功");
                    }catch (SQLException c){
                        System.out.println("MySQL 關閉失敗 ");
                        c.printStackTrace();
                    }
                    
                }
                    
                    
        }
    }