[学习笔记]

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

java当中JDBC当中JNDI用来查找dataSource的例子的更多相关文章

  1. java当中JDBC当中请给出一个DataSource的单态模式(SingleTon)HelloWorld例子

    [学习笔记] 2.DataSource的单态模式(SingleTon)程序 咱们还接着上面的例子来说.1万个人要看书.千万确保要只建立一个图书馆.要是一不留神,建了两个或三个图书馆,那可就亏大发了.对 ...

  2. java当中JDBC当中Scrollable和Updatable ResultSet的用法和Helloworld例子

    [学习笔记] 在前面的jdbc的Helloworld程序当中,我们接触了最简单的 Statement.那种Statement的光标只能向前移.意思就是访问完2,只能继续访问3,不能再回过头来访问1.还 ...

  3. java中JDBC当中请给出一个DataSource的HelloWorld例子

    在前面 的jdbc的Helloworld程序当中,我们用DriverManager来获取数据库连接.事实上通过这种方法获取数据库连接,是比较耗费计算机资 源的.当然了,这也是没有办法的事儿.就像我们买 ...

  4. java当中JDBC当中请给出一个sql server的helloworld例子

    [学习笔记] 1.sql server的helloworld例子: import java.sql.*; public class JdbcHelloSqlServer {  public stati ...

  5. java当中JDBC当中请给出一个sql server的stored procedure例子

    3.sql server的stored procedure例子: import java.sql.*;public class StoredProc0 {public static void main ...

  6. java当中JDBC当中请给出一个Oracle DataSource and SingleTon例子

    [学习笔记] 6.Oracle DataSource and SingleTon: import oracle.jdbc.pool.OracleDataSource;import java.sql.C ...

  7. java当中JDBC当中的transaction例子

    [学习笔记] 7.jdbc的transaction例子: import java.sql.*; public class MySQlTransaction1 { public static void ...

  8. java当中JDBC当中请给出一个sql server的dataSource的helloworld例子

     [学习笔记] 4. sql server的dataSource的helloworld: import java.sql.*;import javax.sql.*;import net.sourcef ...

  9. java当中JDBC当中请给出一个SQLServer DataSource and SingleTon例子

    [学习笔记] 5.SQLServer DataSource and SingleTon: import net.sourceforge.jtds.jdbcx.*;import java.sql.*;i ...

随机推荐

  1. Redis 高可用架构设计(转载)

    转载自:https://mp.weixin.qq.com/s?__biz=MzA3NDcyMTQyNQ==&mid=2649263292&idx=1&sn=b170390684 ...

  2. BZOJ 4919: [Lydsy1706月赛]大根堆 set启发式合并

    这个和 bzoj 5469 几乎是同一道题,但是这里给出另一种做法. 你发现你要求的是一个树上 LIS,而序列上的 LIS 有一个特别神奇的 $O(n\log n) $ 做法. 就是维护一个单调递增的 ...

  3. Android Studio3.0的下载及其安装详解加eclipse下载安装配置jdk9

    关注我,每天都有优质技术文章推送,工作,学习累了的时候放松一下自己. 本篇文章同步微信公众号  欢迎大家关注我的微信公众号:「醉翁猫咪」 今天我们来讲解如何下载android studio 3.0及其 ...

  4. Spark-Streaming DirectKafka count 案例

    Spark-Streaming DirectKafka count 统计跟直接 kafka 统计类似,只不过这里使用的是 Direct 的方式,Direct方式使用的 kafka 低级API,不同的地 ...

  5. c++ 获取字符串中最长的回文子串

    #include <vector> #include <iostream> #include <string> using namespace std; strin ...

  6. avalon用background-image不起作用,怎么来选取前几个的图片进行渲染

    <span ms-css="{backgroundImage: 'url('+item.image + ')'}" ms-for="($index,item) in ...

  7. sprintf格式化字符串漏洞(转)

    深入解析sprintf格式化字符串漏洞 特征: 如何利用: 可以看到, php源码中只对15种类型做了匹配, 其他字符类型都直接break了,php未做任何处理,直接跳过,所以导致了这个问题: 没做字 ...

  8. ngxtop安装和使用

    监控nginx的请求信息,总共收到了多少个请求,有哪些成功的,有哪些失败的 ngxtop安装 1.安装python-pip 1) yum install epel-release 出现如下错误:No ...

  9. Tensorflow r1.12及tensorflow serving r1.12 GPU版本编译遇到的问题

    1.git clone tensorflow serving 及tensorflow代码 2. ERROR: /root/.cache/bazel/_bazel_root/f71d782da17fd8 ...

  10. EasyUi datagrid列表增加复选框

    本文为博主原创,未经允许不得转载 1.增加复选框列 {     field: 'oid',     title: '<input type=\"checkbox\" name ...