• JDBC连接数据库


    1.Statement

    (1)加载驱动程序:Class.forName(driverClass)

    加载mysql驱动:Class.forName("com.mysql.jdbc.Driver");

    加载oracle驱动:Class.forName("oracle.jdbc.driver.OracleDriver");

    (2)获得数据库连接

    DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/imooc",user,password);

    DriverManager.gerConnection(URL,user,password);

    (3)创建Statement对象:conn.createStatement();

    (4)向数据库发送SQL命令

    (5)处理数据库的返回结果(ResultSet类)

    import java.sql.DriverManager;
    import java.sql.ResultSet;
    import java.sql.SQLException;
    import java.sql.Statement;
    import java.sql.Connection;
    import java.sql.DriverManager;
    import java.sql.SQLException;
    
    
    public class javaTest {
       
        public static void main(String[] args) throws ClassNotFoundException, SQLException  {
            String URL="jdbc:mysql://127.0.0.1:3306/imooc?useUnicode=true&characterEncoding=utf-8";
            String USER="root";
            String PASSWORD="tiger";
            //1.加载驱动程序
            Class.forName("com.mysql.jdbc.Driver");
            //2.获得数据库链接
            Connection conn=DriverManager.getConnection(URL, USER, PASSWORD);
            //3.通过数据库的连接操作数据库,实现增删改查(使用Statement类)
            Statement st=conn.createStatement();
            ResultSet rs=st.executeQuery("select * from user");
            //4.处理数据库的返回结果(使用ResultSet类)
            while(rs.next()){
                System.out.println(rs.getString("user_name")+" "
                              +rs.getString("user_password"));
            }
            
            //关闭资源
            rs.close();
            st.close();
            conn.close();
        }
    }

    2.PreparedStatement

    import java.sql.DriverManager;
    import java.sql.PreparedStatement;
    import java.sql.ResultSet;
    import java.sql.SQLException;
    import java.sql.Statement;
    import java.sql.Connection;
    import java.sql.DriverManager;
    import java.sql.SQLException;
    
    
    public class javaTest {
       
        public static void main(String[] args) throws ClassNotFoundException, SQLException  {
            String URL="jdbc:mysql://127.0.0.1:3306/imooc?useUnicode=true&characterEncoding=utf-8";
            String USER="root";
            String PASSWORD="tiger";
            //1.加载驱动程序
            Class.forName("com.mysql.jdbc.Driver");
            //2.获得数据库链接
            Connection conn=DriverManager.getConnection(URL, USER, PASSWORD);
            //3.通过数据库的连接操作数据库,实现增删改查(使用Statement类)
            String s=""+"insert into user(id,user_name,user_password) values("+"2,?,123)";
            PreparedStatement pst=conn.prepareStatement(s);
            
            pst.setString(1, "xiaoshuai1");
            //pst.setString(2, "123");
                
            pst.execute();        
            //关闭资源        
            pst.close();
            conn.close();
        }
    }

    3.DBUtil

    import java.sql.Connection;
    import java.sql.DriverManager;
    import java.sql.SQLException;
    
    public class DBUtil {
    
        private static final String URL="jdbc:mysql://127.0.0.1:3306/imooc?useUnicode=true&characterEncoding=utf-8";
        private static final String USER="root";
        private static final String PASSWORD="tiger";
        
        private static Connection conn=null;
        
        static {
            try {
                //1.加载驱动程序
                Class.forName("com.mysql.jdbc.Driver");
                //2.获得数据库的连接
                conn=DriverManager.getConnection(URL, USER, PASSWORD);
            } catch (ClassNotFoundException e) {
                e.printStackTrace();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        //将获得的数据库与java的链接返回(返回的类型为Connection)
        public static Connection getConnection(){
            return conn;
        }
    }
  • 相关阅读:
    URL和URI之间的区别
    nom Uncaught Error: Cannot find module 'babel-runtime/regenerator'
    PHP中使用curl获取头信息headers的一些笔记
    Typora中自定义命令上传图片
    iOS链接big sur弹出“文件找不到”
    云开发cannot find module wx-server-sdk
    关于Laravel框架中Guard的底层实现
    PHP8中字符串与数字的比较更智能
    git refusing to merge unrelated histories
    k8s入门-资源文件实现
  • 原文地址:https://www.cnblogs.com/heqiyoujing/p/10247449.html
Copyright © 2020-2023  润新知