zoukankan      html  css  js  c++  java
  • JDBC 的编程步骤

    1、加载数据库驱动

    2、获得数据库连接

    3、创建语句

    4、执行查询

    5、遍历结果集

    6、关闭数据库连接

    尽量把数据库的连接步骤写成一个单独的java类,使用的时候直接new一个对象,

    import java.sql.Connection;

    import java.sql.DriverManager;
    import java.sql.SQLException;
    public class shujuku {
    String url="jdbc:mysql://localhost:3306/数据库名";
    String userName="root";
    String password="mima";
    Connection connection;
    public Connection conncetion(){
    try {
    Class.forName("com.mysql.jdbc.Driver");//加载相应的驱动
    try {
    connection=DriverManager.getConnection(url, userName, password);
    if(connection!=null){
    System.out.println("数据库连接成功!");
    //connection.close();
    }else {
    System.out.println("连接失败!");
    }
    } catch (SQLException e) {

    e.printStackTrace();
    }
    } catch (ClassNotFoundException e) {
    e.printStackTrace();
    }
    return connection;
    }
    }

    使用时:

    Connection conn;
    Statement stmt ;
    ResultSet rs;

    conn = new shujuku().conncetion();
    stmt = conn.createStatement();
    rs = stmt.executeQuery(sql查询语句);

    int count=stmt.executeUpdate(sql更新语句);

    PreparedStatement ps=null;

    ps=conn.prepareStatement(sql更新语句);

    int count=ps.executeUpdate();

  • 相关阅读:
    left join问题
    SQL索引
    数据库查询优化
    define and inline
    程序的内存分配
    __closure
    this指针
    java笔记
    Visual Studio Code(VSCODE)语言设置
    Excel 2010如何打开多个独立窗口?
  • 原文地址:https://www.cnblogs.com/zhangxue521/p/5751011.html
Copyright © 2011-2022 走看看