sx2019121622538_v1_JavaJDBC

Posted adeepbluesky

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了sx2019121622538_v1_JavaJDBC相关的知识,希望对你有一定的参考价值。

Test

package sx20191216;

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

public class Test {
    public static void main(String[] args) throws Exception {

    Connection con = null;
    PreparedStatement ps = null;
    ResultSet rs = null;

    String driver = "com.mysql.jdbc.Driver";
    String url = "jdbc:mysql://localhost:3306/hospital";
    String user = "root";
    String password = "root";

    try {
        // 加载驱动程序
        Class.forName(driver);
        // 获得数据库连接
        con = DriverManager.getConnection(url, user, password);
        // 操作数据库,实现增删改查
        String sql = "SELECT *  FROM Doctor";
        ps = con.prepareStatement(sql);
        rs = ps.executeQuery();

        // 如果有数据,rs.next()返回true
        System.out.print("编号	姓名	年龄	专业
");
        while (rs.next()) {
        System.out.print(rs.getInt(1) + "	");
        System.out.print(rs.getString(2) + "	");
        System.out.print(rs.getInt(3) + "	");
        System.out.print(rs.getString(4) + "
");
        }
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        if (rs != null) {
        rs.close();
        }
        if (ps != null) {
        ps.close();
        }
        if (con != null) {
        con.close();
        }
    }
    }

}

Test1

package sx20191216;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.Scanner;

public class Test1 {
    @SuppressWarnings("resource")
    public static void main(String[] args) throws Exception {

    Scanner scanner = new Scanner(System.in);

    System.out.println("请输入名字:");
    String name1 = scanner.next();
    System.out.println("请输入年龄");
    int age1 = scanner.nextInt();
    System.out.println("请输入专业");
    String major1 = scanner.next();

    Connection con = null;
    PreparedStatement ps = null;
    ResultSet rs = null;

    String driver = "com.mysql.jdbc.Driver";
    String url = "jdbc:mysql://localhost:3306/hospital";
    String user = "root";
    String password = "root";

    try {
        // 加载驱动程序
        Class.forName(driver);
        // 获得数据库连接
        con = DriverManager.getConnection(url, user, password);
        // 操作数据库,实现插入
        String sql = "insert into Doctor values(null,?,?,?)";
        ps = con.prepareStatement(sql);
        ps.setString(1, name1);
        ps.setInt(2, age1);
        ps.setString(3, major1);

        int result = ps.executeUpdate();
        if (result == 1) {
        System.out.println("插入成功");

        } else {
        System.out.println("插入失败");

        }

    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        if (rs != null) {
        rs.close();
        }
        if (ps != null) {
        ps.close();
        }
        if (con != null) {
        con.close();
        }
    }
    }

}

Test2

package sx20191216;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.Scanner;

public class Test2 {
    @SuppressWarnings("resource")
    public static void main(String[] args) throws Exception {
    Scanner scanner = new Scanner(System.in);

    System.out.println("请输入序号");
    int id1 = scanner.nextInt();

    System.out.println("请输入名字:");
    String name1 = scanner.next();

    System.out.println("请输入年龄");
    int age1 = scanner.nextInt();

    System.out.println("请输入专业");
    String major1 = scanner.next();

    Connection con = null;
    PreparedStatement ps = null;
    ResultSet rs = null;

    String driver = "com.mysql.jdbc.Driver";
    String url = "jdbc:mysql://localhost:3306/hospital";
    String user = "root";
    String password = "root";

    try {
        // 加载驱动程序
        Class.forName(driver);
        // 获得数据库连接
        con = DriverManager.getConnection(url, user, password);
        // 操作数据库,实现更新
        String sql = "UPDATE doctor SET name=?, age = ?,major = ? where id=?";
        ps = con.prepareStatement(sql);
        ps.setString(1, name1);
        ps.setInt(2, age1);
        ps.setString(3, major1);
        ps.setInt(4, id1);
        int result = ps.executeUpdate();
        if (result == 1) {
        System.out.println("修改成功1");

        } else {
        System.out.println("修改失败0");

        }

    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        if (rs != null) {
        rs.close();
        }
        if (ps != null) {
        ps.close();
        }
        if (con != null) {
        con.close();
        }
    }
    }

}

Test3

package sx20191216;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.Scanner;

public class Test3 {
    @SuppressWarnings("resource")
    public static void main(String[] args) throws Exception {
    Scanner scanner = new Scanner(System.in);

    System.out.println("请输入序号");
    int id1 = scanner.nextInt();

    Connection con = null;
    PreparedStatement ps = null;
    ResultSet rs = null;

    String driver = "com.mysql.jdbc.Driver";
    String url = "jdbc:mysql://localhost:3306/hospital";
    String user = "root";
    String password = "root";

    try {
        // 加载驱动程序
        Class.forName(driver);
        // 获得数据库连接
        con = DriverManager.getConnection(url, user, password);
        // 操作数据库,实现删除
        String sql = "DELETE FROM doctor where id = ? ";
        ps = con.prepareStatement(sql);
        ps.setInt(1, id1);
        int result = ps.executeUpdate();
        if (result == 1) {
        System.out.println("删除成功1");

        } else {
        System.out.println("删除失败0");

        }

    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        if (rs != null) {
        rs.close();
        }
        if (ps != null) {
        ps.close();
        }
        if (con != null) {
        con.close();
        }
    }
    }

}

以上是关于sx2019121622538_v1_JavaJDBC的主要内容,如果未能解决你的问题,请参考以下文章

SX1281驱动学习笔记二:驱动学习

SX1281驱动学习笔记二:驱动学习

SX知识学习——相关sources

PCA-3 通过奇异值分解来求主成分

LoRa模块SX1278/SX1262/SX1268/SX1212的参数对比

LoRa模块SX1278/SX1262/SX1268/SX1212的参数对比