jdbc之mysql連接與操作

一  介紹:數據庫連接其實不算複雜,連接有固定的格式,以學生系統實現mysql數據庫連接

二  jdbc連接格式(mysql爲例)

注意:數據庫連接和操作需要拋出相應的異常類型


三 數據庫具體操作

import java.sql.*;
import java.util.Scanner;
import java.io.*;
public class  test{
    public static void main(String[] args)
    {
        //聲明Connection對象
        Connection con=null;
        //驅動程序名
        String driver = "com.mysql.jdbc.Driver";
        //URL指向要訪問的數據庫名login
        String url = "jdbc:mysql://localhost:3306/hello";
        //MySQL配置時的用戶名
        String user = "root";
        //MySQL配置時的密碼
        String password = "518189";
        //遍歷查詢結果集
        String sql=null;
        ResultSet rs=null;
        Scanner in = new Scanner(System.in);
        // BufferedReader buffer = new BufferedReader(new InputStreamReader(System.in));

        try {
            Class.forName(driver);
            con = DriverManager.getConnection(url, user, password);
            if (!con.isClosed())
                System.out.println("Succeeded connecting to the Database!");

            while (true) {
                System.out.println("****************歡迎進入學生系統*********************");
                System.out.println("**************** 請輸入你的選擇 *********************");
                System.out.println("**************** 1.查詢學生信息 *********************");
                System.out.println("**************** 2.添加學生信息 *********************");
                System.out.println("**************** 3.刪除學生信息 *********************");
                System.out.println("**************** 4.修改學生信息 *********************");
                int   choice =in.nextInt();
                if(choice==1){
                    sql = "select * from Student ";

                    Statement statement = con.createStatement();
                    rs = statement.executeQuery(sql);
                }
                else if (choice==2){
                    sql = "insert into Student(Ssno,Sname,Ssex,Ssdept) VALUES (?,?,?,?)";
                    PreparedStatement pstmt;
                    pstmt = con.prepareStatement(sql);
                    System.out.println("請輸出你要添加學生的學號");
                    String sno=in.next();
                    System.out.println("請輸出你要添加學生的姓名");
                    String name=in.next();
                    System.out.println("請輸出你要添加學生的性別");
                    String sex=in.next();
                    System.out.println("請輸出你要添加學生的學院");
                    String sdept=in.next();
                    pstmt.setObject(1,sno);
                    pstmt.setObject(2,name);
                    pstmt.setObject(3,sex);
                    pstmt.setObject(4,sdept);
                    pstmt.executeUpdate();
                    sql = "select * from Student ";
                    rs = pstmt.executeQuery(sql);
                }else  if (choice==3){
                    System.out.println("請輸出你要刪除學生的學號");
                    String sno=in.next();
                    sql="delete  from Student where Ssno =?";
                    PreparedStatement pstmt;
                    pstmt = con.prepareStatement(sql);
                    pstmt.setObject(1,sno);
                    pstmt.executeUpdate();
                    sql = "select * from Student ";
                    rs = pstmt.executeQuery(sql);
                }else  if (choice==4){
                    sql="UPDATE Student  set Ssno=? , Sname=? ,Ssex=?, Ssdept=? where Ssno=?";
                    PreparedStatement pstmt;
                    pstmt = con.prepareStatement(sql);
                    System.out.println("請輸出你要修改學生的學號");
                    String sno=in.next();
                    System.out.println("請輸出你要修改後學生的學號");
                    String sno1=in.next();
                    System.out.println("請輸出你要修改後學生的姓名");
                    String name=in.next();
                   System.out.println("請輸出你要修改和學生的性別");
                    String sex=in.next();
                    System.out.println("請輸出你要修改後學生的學院");
                    String sdept=in.next();
                    pstmt.setObject(1,sno1);
                    pstmt.setObject(2,name);
                    pstmt.setObject(3,sex);
                    pstmt.setObject(4,sdept);
                    pstmt.setObject(5,sno );
                    pstmt.executeUpdate();
                    sql = "select * from Student ";
                    rs = pstmt.executeQuery(sql);
                }

                else if (choice==0){
                    break ;
                }
                System.out.println("-----------------------------------------------");
                System.out.println("學號"  +  "\t" + "姓名" + "\t" + "\t"  + "\t" + "性別" + "\t" + "專業");
                System.out.println("------------------------------------------------");
                System.out.println("");

                while (rs.next()) {
                    String   Ssno = rs.getString("Ssno");
                    String  Sname = rs.getString("Sname");
                    String  Ssex = rs.getString("Ssex");
                    String  Ssdept = rs.getString("Ssdept");
                    System.out.println(Ssno + "\t" + Sname + "\t" + "\t" + "\t" +Ssex+ "\t" + "\t" + Ssdept);
                }
            }
            // rs.close();
            // con.close();
        }
        catch(ClassNotFoundException e)
        {
            //數據庫驅動類異常處理
            System.out.println("Sorry,can`t find the Driver!");
            e.printStackTrace();
        }
        catch(SQLException e)
        {
            //數據庫連接失敗異常處理
            e.printStackTrace();
        }
        catch(Exception e)
        {
            // TODO: handle exception
            e.printStackTrace();
        }
        finally
        {
            System.out.println("數據庫數據成功獲取!!");
        }
    }
}



發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章