zoukankan      html  css  js  c++  java
  • java连接MySQL数据库(jdbc)

    package zp.itcase.JDBCdemo;
    
    import java.sql.Connection;
    import java.sql.DriverManager;
    import java.sql.SQLException;
    import java.sql.Statement;
    
    public class JDBCDemo3 {
        public static void main(String[] args) {
            Statement stmt = null;
            Connection conn = null;
            try {
                // 1.注册驱动
                Class.forName("com.mysql.jdbc.Driver");
                // 2.获取连接对象
                conn = DriverManager.getConnection("jdbc:mysql:///db3", "root", "root");
                // 3. 定义sql
                String sql = "update account set balance = 1500 where id =3";
                // 4.获取执行sql的对象
                stmt = conn.createStatement();
                // 5.执行sql
                int count = stmt.executeUpdate(sql);
                // 6.处理结果
                if (count > 0) {
                    System.out.println("修改成功");
                } else {
                    System.out.println("修改失败");
                }
    
    
            } catch (ClassNotFoundException e) {
                e.printStackTrace();
            } catch (SQLException e) {
                e.printStackTrace();
            } finally {
                if (stmt != null) {
                    try {
                        stmt.close();
                    } catch (SQLException e) {
                        e.printStackTrace();
                    }
                }
                if (conn != null) {
                    try {
                        conn.close();
                    } catch (SQLException e) {
                        e.printStackTrace();
                    }
                }
            }
        }
    }
  • 相关阅读:
    flash player over linux
    chmod 命令
    A*算法
    adb找不到设备
    ubuntu14.04安装wine以及国际版QQ
    linux man
    X-window
    linux file system
    linux command
    directUI
  • 原文地址:https://www.cnblogs.com/peng-zhao/p/11503615.html
Copyright © 2011-2022 走看看