zoukankan      html  css  js  c++  java
  • java当中JDBC当中JNDI用来查找dataSource的例子

    [学习笔记]

    8.JNDI用来查找dataSource的例子:

    import javax.naming.InitialContext;
    import javax.naming.Context;

    import com.mysql.jdbc.jdbc2.optional.MysqlConnectionPoolDataSource;

    import java.util.Properties;
    import net.sourceforge.jtds.jdbcx.*;
    public class ClassPut {
            public static void main(String a[]) {
                    try {
    /*                  JtdsDataSource dataSource=new  JtdsDataSource();
                            dataSource.setServerName("localhost");
                            dataSource.setDatabaseName("NorthWind");
                            dataSource.setUser("sa");
                            dataSource.setPassword("1234");
    */
                        MysqlConnectionPoolDataSource ds = new MysqlConnectionPoolDataSource();
                        ds.setURL("jdbc:mysql://localhost:3306/test");
                        ds.setUser("root");
                        ds.setPassword("1234");                      
                            
                            
                            Properties prop = new Properties();
                            prop.setProperty(Context.INITIAL_CONTEXT_FACTORY,
                                            "com.sun.jndi.fscontext.RefFSContextFactory");

                            Context ctx=new InitialContext(prop);
    /*here the following statement writes a file .bindings under the f Disk. because this project is under the f disk.*/
                            ctx.rebind("abc",ds);


                    } catch (Exception e) {
                            e.printStackTrace();
                    }
            }
    }
    上面的程序只是存入硬盘,想查找出来得用下面的程序:

    import javax.naming.InitialContext;
    import javax.naming.Context;

    import com.mysql.jdbc.jdbc2.optional.MysqlConnectionPoolDataSource;

    import java.sql.Connection;
    import java.sql.ResultSet;
    import java.sql.Statement;
    import java.util.Properties;
    import net.sourceforge.jtds.jdbcx.*;
    public class ClassGet {
            public static void main(String a[]) {

                    try {
                            Properties prop = new Properties();
                            prop.setProperty(Context.INITIAL_CONTEXT_FACTORY,
                                            "com.sun.jndi.fscontext.RefFSContextFactory");
                            Context ctx=new InitialContext(prop);

    文章转载自原文:https://blog.csdn.net/qq_43650923/article/details/100652625

  • 相关阅读:
    BZOJ 4199: [Noi2015]品酒大会 后缀自动机+逆序更新
    BZOJ 3676: [Apio2014]回文串 回文自动机
    Remember the Word UVALive
    [APIO2012]派遣 可并堆(左偏树)
    BZOJ 2555: SubString 后缀自动机 + LCT
    力扣题目汇总(转换成小写字母,唯一摩尔斯密码,有序数组平方)
    力扣题目汇总(加一,旋转数组,整数反转)
    力扣题目汇总(存在重复,合并两个有序数组,搜索插入位置)
    力扣题目汇总(买卖股票的最佳时机,最大连续1的个数,缺失的数字)
    力扣题目汇总(最长特殊序列,回文数,移动零)
  • 原文地址:https://www.cnblogs.com/haima1949/p/11608776.html
Copyright © 2011-2022 走看看