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("数据库数据成功获取!!");
        }
    }
}



猜你喜欢

转载自blog.csdn.net/aA518189/article/details/78856570