JAVA--高級基礎開發JDBC

//封裝JDBC工具包
driverClass=com.mysql.cj.jdbc.Driver
url=jdbc:mysql://localhost:3306/ab_wzy?serverTimezone=UTC&character=utf8
user=root
password=105104
import java.io.IOException;
import java.io.InputStream;
import java.sql.*;
import java.util.Properties;
public class JdbcUtils {
    private  static String  url;
    private  static String  user;
    private  static String password;
    private  static Properties P;
    static {
        P=new Properties();
        //讀取屬性文件
        InputStream  input=Thread.currentThread().getContextClassLoader().getResourceAsStream("jdbc.properties");
        //加載P對象
        try{
            P.load(input);
        }catch(IOException  ce){
            ce.printStackTrace();
        }

        //根據鍵獲取值
       url=P.getProperty("url");
       user=P.getProperty("user");
       password=P.getProperty("password");
    }

    //獲取數據庫鏈接對象
    public  static Connection  getConnection()throws SQLException {
        Connection con= DriverManager.getConnection(url,user,password);
        return con;
    }
    //關閉數據庫鏈接對象之insert  delete update的操做
    public static  void  close(Connection  con, Statement state)throws SQLException{
        con.close();;
        state.close();
    }
    //關閉數據庫鏈接的對象之 select 查找查詢的操做
    public static  void close(Connection  con, Statement  state, ResultSet  set)throws SQLException{
         set.close();
         state.close();
         con.close();
    }
    //關閉獲取數據庫鏈接對象
    public  static  void  close(Connection  con)throws SQLException{
        con.close();
    }
    // 關閉執行Statement執行SQL 語句的對象
    public static  void close(Statement  state)throws SQLException{
        state.close();
    }
    //關閉結果集對象ResultSet對象
    public static  void close(ResultSet  set)throws SQLException{
        set.close();
    }
}
//案例:用戶登陸
public class jdbc_demo1 {
    public static void main(String[] args) {
        Scanner  input=new Scanner(System.in);
        System.out.println("請輸入用戶名");
        String  name=input.nextLine();
        System.out.println("請輸入密碼");
        String  password=input.nextLine();
        Login(name,password);
    }
    // 用戶登陸的方法
    public  static  void Login(String name,String  password){
        Connection  con=null;
        Statement  state=null;
        ResultSet  set=null;
        //經過工具類來獲取鏈接對象
        try{
            con= JdbcUtils.getConnection();
            //獲取Statement執行SQL語句的對象
            state=con.createStatement();
            //準備登陸的SQL語句。登陸就是從數據庫表中查詢數據
            String  sql="select  * from user  where name='"+name+"'and password='"+password+"'";
            // 執行SQL語句
            set=state.executeQuery(sql);
            if(set.next()){
                System.out.println("登陸成功,歡迎"+name);
            }else{
                System.out.println("登陸失敗");
            }
        }catch(SQLException ce){
            ce.printStackTrace();
        }finally {
            try{
                JdbcUtils.close(con,state,set);
            }catch(SQLException  ce){
                ce.printStackTrace();
            }

        }
    }
}
//使用reparedStatement接口
//防止SQL注入的問題
public class jdbc_demo2 {
    public static void main(String[] args) {
        Scanner input=new Scanner(System.in);
        System.out.println("請輸入用戶名");
        String  name=input.nextLine();
        System.out.println("請輸入密碼");
        String  password=input.nextLine();
        Login(name,password);
    }
    //定義用戶登陸的方法
    public static  void  Login(String  name,String password){
        Connection con=null;
        PreparedStatement  statement=null;
        ResultSet set=null;
        try{
            //根據工具包獲取鏈接對象
            con= JdbcUtils.getConnection();
            //準備登陸的SQL語句
            String  sql="select *  from user where name=? and password=?";
            //獲取PreparedStatement執行的SQL語句的對象
            statement=con.prepareStatement(sql);
            //給佔位符賦值
            statement.setString(1,name);
            statement.setString(2,password);
            //執行SQL語句
            set=statement.executeQuery();
            if(set.next()){
                System.out.println("登陸成功,歡迎"+name);
            }else{
                System.out.println("登陸失敗");
            }
        }catch(SQLException  ce){
            ce.printStackTrace();
        }finally {
            try{
                JdbcUtils.close(con,statement,set);
            }catch(SQLException  ce){
                ce.printStackTrace();
            }

        }
    }
}
//表與類之間的關係
public class jdbc_demo3 {
    public static void main(String[] args) {
        List<Student>list=finAll();
        for(Student  ss:list){
            System.out.println(ss);
        }
    }
    //查詢表中全部的信息
    public static List<Student> finAll(){
        Connection  con=null;
        PreparedStatement  state=null;
        ResultSet  set=null;
        List<Student>list=new ArrayList<>();
        try{
            //獲取鏈接對象
            con= JdbcUtils.getConnection();
            //準備SQL 語句
            String sql="select  *  from  student";
            //獲取PrepareStatement對象
            state=con.prepareStatement(sql);
            set=state.executeQuery();
            //先聲明的對象
            Student  student=null;
            while(set.next()){
        //每次建立的對象,都會把上次對象給覆蓋掉,始終保持只佔用一塊內存空間
               student=new Student();
               student.setId(set.getInt("id"));
               student.setName(set.getString("name"));
               student.setGender(set.getString("gender"));
               student.setBirthday(set.getDate("birthday"));
               list.add(student);
            }
        }catch(SQLException  ce){
            ce.printStackTrace();
        }
        return list;
    }
}

//JDBC 事物的處理案例
public class jdbc_demo5 {
    public static void main(String[] args) {
        Connection  con=null;
        PreparedStatement state=null;
        try{
            //獲取Connection對象
            con= JdbcUtils.getConnection();
            //開啓事物 默認爲fasle
            con.setAutoCommit(false);
            //給李文傑扣錢
            String  sql="update common set  money=money-? where id=?";
            state=con.prepareStatement(sql);
            //給佔位符賦值
            state.setString(1,"500");
            state.setInt(2,1);
            //執行SQL語句
            state.executeUpdate();
            //給郭朝旭加錢

            String  sql2="update common set money=money+? where id=?";
            state=con.prepareStatement(sql2);
            //給佔位符賦值
            state.setString(1,"500");
            state.setInt(2,2);
            //執行SQL語句
           int i=state.executeUpdate();
           if(i>0){
               System.out.println("轉帳成功");
           }
            //執行完畢,關閉事物
            con.commit();

        }catch(SQLException  ce){
            ce.printStackTrace();
            //若是發生異常,就回滾 rollback
            try{
                con.rollback();
            }catch(SQLException  ces){
                ces.printStackTrace();
            }
            System.out.println("轉帳失敗");
        }finally {
            try{
                JdbcUtils.close(con,state);
            }catch(SQLException  ce){
                ce.printStackTrace();
            }

        }
    }
}
//JAVABean Student
public class Student implements Serializable {
    private static final long serialVersionUID = 8394954963273410188L;
    private int  id;
    private String name;
    private String gender;
    private Date birthday;
    public Student(){

    }
    public Student(int id, String name, String gender, Date birthday) {
        this.id = id;
        this.name = name;
        this.gender = gender;
        this.birthday = birthday;
    }

    public static long getSerialVersionUID() {
        return serialVersionUID;
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getGender() {
        return gender;
    }

    public void setGender(String gender) {
        this.gender = gender;
    }

    public Date getBirthday() {
        return birthday;
    }

    public void setBirthday(Date birthday) {
        this.birthday = birthday;
    }

    @Override
    public String toString() {
        return "Student{" +
                "id=" + id +
                ", name='" + name + '\'' +
                ", gender='" + gender + '\'' +
                ", birthday='" + birthday + '\'' +
                '}';
    }
}
相關文章
相關標籤/搜索