zoukankan      html  css  js  c++  java
  • jdbc 数据库的连接 以及数据库的增删改查

    //加载驱动

    //连接数据库

    //sql语句

    //创建执行SQL语句的Statement对象

    //处理sql语句执行的结果集(ResultSet)

    package util;

    import java.sql.Connection;
    import java.sql.DriverManager;

    public class Jdbcutil {
    private static Connection conn=null;
    static{
    String url="jdbc:mysql://localhost:3306/test";
    String username="root";
    String password="1234";
    String driverClass="com.mysql.jdbc.Driver";


    try {Class.forName(driverClass);//
    conn = DriverManager.getConnection(url, username, password); //
    } catch (Exception e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    }
    }
    public static Connection getConnection(){

    if(conn!=null){
    return conn;
    }
    else {
    return null;
    }

    }

    }

    查询

    public List<Student> getStudentList(){

    PreparedStatement preparedStatement=null;
    ResultSet rs=null;
    List<Student> studentlist=new ArrayList<Student>();
    Connection connection=Jdbcutil.getConnection();
    String sql="select * from student";
    try {
    preparedStatement=connection.prepareStatement(sql);
    } catch (SQLException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    }
    try {
    rs = preparedStatement.executeQuery();
    while (rs.next()) {
    Student student=new Student();
    int id=rs.getInt("id");
    String name=rs.getString("name");
    String school=rs.getString("school");
    double score=rs.getDouble("score");
    student.setId(id);
    student.setName(name);
    student.setSchool(school);
    student.setScore(score);
    studentlist.add(student);

    }
    } catch (SQLException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    }


    return studentlist;}

    增删改

    public void tianjia(String userid,String username,String userschool,String userscore){
    PreparedStatement preparedStatement=null;
    Connection connection=Jdbcutil.getConnection();
    String sql="insert into student values (?,?,?,?)";
    try {
    preparedStatement=connection.prepareStatement(sql);
    preparedStatement.setObject(1, userid);
    preparedStatement.setObject(2, username);
    preparedStatement.setObject(3, userschool);
    preparedStatement.setObject(4, userscore);
    preparedStatement.executeUpdate();

    } catch (SQLException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    }

    }

    public void shanchu(String userid){
    PreparedStatement preparedStatement=null;
    Connection connection=Jdbcutil.getConnection();
    String sql="DELETE FROM student WHERE id='"+userid+"'";
    try {
    preparedStatement=connection.prepareStatement(sql);
    preparedStatement.executeUpdate();

    } catch (SQLException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    }

    }

    public void xiugai(String userid,String username,String userschool,String userscore){
    PreparedStatement preparedStatement=null;
    Connection connection=Jdbcutil.getConnection();
    String sql="update student set id=?,name=?,school=?,score=? where id='"+userid+"'";
    try {
    preparedStatement=connection.prepareStatement(sql);
    preparedStatement.setObject(1, userid);
    preparedStatement.setObject(2, username);
    preparedStatement.setObject(3, userschool);
    preparedStatement.setObject(4, userscore);
    preparedStatement.executeUpdate();

    } catch (SQLException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    }

    }

  • 相关阅读:
    搭建Hadoop2.6.0+Spark1.1.0集群环境
    分别用Eclipse和IDEA搭建Scala+Spark开发环境
    poj 2586 Y2K Accounting Bug【贪心】【刷题计划】
    poj2109 Power of Cryptography【坑~泪目】【水过】【刷题计划】
    例题6.1 铁轨【算法入门经典】
    第三章 队列【数据结构】【链队列】【循环队列】
    poj 1328 Radar Installation【贪心】【刷题计划】
    hdu 2364 Escape【模拟优先队列】【bfs】
    poj 2965 The Pilots Brothers' refrigerator 【dfs+枚举】【双十一大礼包】
    6.3.3 二叉树重建【算法入门经典】【双十一大礼包】
  • 原文地址:https://www.cnblogs.com/zhaosong-0102/p/7355494.html
Copyright © 2011-2022 走看看