怎么通过java代码实现对数据库的连接

源码客栈 论文问答 1

通过java代码如何实现对数据库的增删改查,怎么理解Driver?具体的实现过程请求大神解答-jdbc

回复

共2条回复 我来回复
  • 代码驿站
    这个人很懒,什么都没有留下~
    评论
    public static ResultSet getResult(String sql) {
    
        Connection con = getConnection(); //获取驱动
        try {
            Statement stmt = con.createStatement();
            ResultSet rs = stmt.executeQuery(sql); //获取结果
            //con.close();
            return rs;
        } catch(SQLException e) {
            // TODO Auto-generated catch block
        }
        return null;
    }
    private static Connection getConnection() {
        try {
    
            Properties p = new Properties();
            InputStream in =null;
            String driver = "";
            String username = "";
            String password = "";
            String url = "";
            try { in =new FileInputStream("WebContent/datasource.properties"); //读取配置数据库的文本
                p.load( in );
                driver = p.getProperty("jdbc.driverClassName");
                url = p.getProperty("jdbc.url");
                username = p.getProperty("jdbc.username");
                password = p.getProperty("jdbc.password");
                System.out.println(driver);
            } catch(FileNotFoundException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch(IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            //加载oracle驱动
            Class.forName(driver);
            Connection con =
    
            DriverManager.getConnection(url, username, password);
            return con;
        } catch(ClassNotFoundException e) {
    
            System.out.println("找不到驱动程序类 ,加载驱动失败!");
    
            e.printStackTrace();
    
        } catch(SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return null;
    }
    

    --数据库配置文本内容datasource.properties

    jdbc.driverClassName=oracle.jdbc.driver.OracleDriver
    jdbc.url=jdbc:oracle:thin:@172.18.94.63/orcl
    jdbc.username=
    jdbc.password=
    
    0条评论
  • 代码导航
    这个人很懒,什么都没有留下~
    评论
    public class DBTool {
        //sql 2005
        protected static String dbClassName = "com.microsoft.sqlserver.jdbc.SQLServerDriver";
        protected static String dbUrl = "jdbc:sqlserver://localhost:1433;DatabaseName=数据库名;";
        protected static String dbUser = "***"; //数据库登录名
        protected static String dbPwd = "**"; //密码
        protected static String second = null;
        private static Connection conn = null;
        /**
    * 构造,连接数据库
    @auther 小糯
    */
        private DBTool() {
            try {
                if (conn == null) {
                    Class.forName(dbClassName).newInstance(); //注册驱动
                    conn = DriverManager.getConnection(dbUrl, dbUser, dbPwd); //连接
                } else return;
            } catch(Exception ee) {
                ee.printStackTrace();
            }
        }
        //分割线,数据库基本操作头
        /*
    * 查询
    * @param sql 查询语句
    * @return
    /
    public static ResultSet executeQuery(String sql)
    {
    try
    {
    if(conn==null)
    new DBTool();
    return conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_UPDATABLE).executeQuery(sql);
    } catch (SQLException e) {
    e.printStackTrace();
    return null;
    } finally {}
    }
    /*
    * 增删改
    * @param sql 执行语句
    * @return 执行结果
    */
        private static int executeUpdate(String sql) {
            try {
                if (conn == null) new DBTool();
                return conn.createStatement().executeUpdate(sql);
            } catch(SQLException e) {
                System.out.println(e.getMessage());
                return - 1;
            } finally {}
        }
        //分割线,数据库基本操作尾
    }
    
    0条评论

发表回复

登录后才能评论