• JavaJDBC整理


     

    1.1.1    导入驱动jar包

    创建lib目录,用于存放当前项目需要的所有jar包

    选择jar包,右键执行build path / Add to Build Path

     

    前版本

    package com.oracle.demo01;

    import java.sql.Connection;

    import java.sql.DriverManager;

    import java.sql.SQLException;

    import java.sql.Statement;

    public class Demo01 {

             public static void main(String[] args) throws ClassNotFoundException, SQLException  {

    //               1.      注册驱动.

                       //               告知JVM使用的是哪一个数据库的驱动

                       Class.forName("com.mysql.jdbc.Driver");

    //               2.      获得连接.

    //               使用JDBC中的类,完成对MySQL数据库的连接

                       //localhost 127.0.0.1

                       String url="jdbc:mysql://localhost:3306/java1203?useUnicode=true&characterEncoding=UTF-8";

                       String user="root";

                       String password="123456";

                       Connection conn=DriverManager.getConnection(url, user, password);

            

    //               3.      获得语句执行平台

    //               通过连接对象获取对SQL语句的执行者对象

                      

                       Statement sta=conn.createStatement();

    //               4.      执行sql语句

    //               使用执行者对象,向数据库执行SQL语句

                       String sql="insert into sort(sname) values('口红')";

                       //增删改都用executeUpdate  查询用executeQuery 方法

                       int row=sta.executeUpdate(sql);

                       System.out.println(row);

    //               获取到数据库的执行后的结果

    //               5.      处理结果

    //               6.      释放资源.

    //               调用一堆close()方法

                       sta.close();

                       conn.close();

             }

    }

    中间版本

    package com.oracle.tools;

    import java.sql.Connection;

    import java.sql.DriverManager;

    import java.sql.PreparedStatement;

    import java.sql.ResultSet;

    import java.sql.SQLException;

    public class JDBCUtils {

             public static Connection getConn() {

                       Connection conn = null;

                       try {

                                Class.forName("com.mysql.jdbc.Driver");

                                String url = "jdbc:mysql://localhost:3306/store_v1.0(自己数据库名的库名)?useUnicode=true&characterEncoding=UTF-8";

                                String user = "root";//数据库账号

                                String password = "123456";//数据数据库密码

                                conn = DriverManager.getConnection(url, user, password);

                       } catch (ClassNotFoundException e) {

                                // TODO Auto-generated catch block

                                e.printStackTrace();

                       } catch (SQLException e) {

                                // TODO Auto-generated catch block

                                e.printStackTrace();

                       }

                       return conn;

             }

             // 增删改释放资源的方法

             public static void Close(Connection conn, PreparedStatement pst) {

                       if (pst != null) {

                                try {

                                         pst.close();

                                } catch (SQLException e) {

                                         // TODO Auto-generated catch block

                                         e.printStackTrace();

                                }

                       }

                       if (conn != null) {

                                try {

                                         conn.close();

                                } catch (SQLException e) {

                                         // TODO Auto-generated catch block

                                         e.printStackTrace();

                                }

                       }

             }

             // 查询释放资源

             public static void Close(ResultSet rs, PreparedStatement pst, Connection conn) {

                       if (rs != null) {

                                try {

                                         rs.close();

                                } catch (SQLException e) {

                                         // TODO Auto-generated catch block

                                         e.printStackTrace();

                                }

                       }

                       if (pst != null) {

                                try {

                                         pst.close();

                                } catch (SQLException e) {

                                         // TODO Auto-generated catch block

                                         e.printStackTrace();

                                }

                       }

                       if (conn != null) {

                                try {

                                         conn.close();

                                } catch (SQLException e) {

                                         // TODO Auto-generated catch block

                                         e.printStackTrace();

                                }

                       }

             }

    }

    最后版本

     

    注意:

    不允许出现标点符号和空格等符号

    package com.oracle.tools;

    import java.io.FileInputStream;

    import java.io.FileNotFoundException;

    import java.io.IOException;

    import java.sql.Connection;

    import java.sql.DriverManager;

    import java.sql.PreparedStatement;

    import java.sql.ResultSet;

    import java.sql.SQLException;

    import java.util.Properties;

    public class JDBCutils {

             public static Connection getConn(){

                       //注册驱动

                       Connection conn=null;

                       Properties pro=new Properties();

                      

                                try {

                                         FileInputStream fis=new FileInputStream("src/db.properties");

                                         pro.load(fis);

                                         Class.forName(pro.getProperty("driverClass"));

                                         //获取连接

                                         String url=pro.getProperty("url");

                                         String username=pro.getProperty("username");

                                         String password=pro.getProperty("password");

                                         conn=DriverManager.getConnection(url,username,password);

                                         } catch (ClassNotFoundException e) {

                                                   e.printStackTrace();

                                         } catch (SQLException e) {

                                                   e.printStackTrace();

                                         } catch (FileNotFoundException e) {

                                                   // TODO Auto-generated catch block

                                                   e.printStackTrace();

                                         } catch (IOException e) {

                                                   // TODO Auto-generated catch block

                                                   e.printStackTrace();

                                         }

                                return conn;

             }

             //释放资源

             public static void close(Connection conn,PreparedStatement pst){

                       if(pst!=null){

                                try {

                                         pst.close();

                                } catch (SQLException e) {

                                         e.printStackTrace();

                                }

                       }

                       if(conn!=null){

                                try {

                                         conn.close();

                                } catch (SQLException e) {

                                         e.printStackTrace();

                                }

                       }

             }

             public static void close(Connection conn,PreparedStatement pst,ResultSet rs){

                       if(rs!=null){

                                try {

                                         rs.close();

                                } catch (SQLException e) {

                                         e.printStackTrace();

                                }

                       }

                       if(pst!=null){

                                try {

                                         pst.close();

                                } catch (SQLException e) {

                                         e.printStackTrace();

                                }

                       }

                       if(conn!=null){

                                try {

                                         conn.close();

                                } catch (SQLException e) {

                                         e.printStackTrace();

                                }

                       }

             }

            

    }

  • 相关阅读:
    iOS
    关于HTTP协议学习(三)
    关于HTTP协议学习(二)
    关于HTTP协议学习(一)
    Swift之Swift编码规范
    老罗学习MVC之旅:MVC组件分析
    android 左右翻页
    android 检测网络是否可用
    android 在线升级借助开源中国App源码
    android 渐变展示启动屏
  • 原文地址:https://www.cnblogs.com/mengmengi/p/10652250.html
Copyright © 2020-2023  润新知