java當(dāng)中JDBC當(dāng)中JNDI用來查找dataSource的例子
JNDI用來查找dataSource的例子:
馬克- to-win:馬克 java社區(qū):防盜版實(shí)名手機(jī)尾號: 73203。
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);
/*it will lookup things under the f disk---.bindings file, lookup will directly instantiate
an object of net.sourceforge.jtds.jdbcx.JtdsDataSource in memory. */
Object obj=ctx.lookup("abc");
System.out.println(obj);
MysqlConnectionPoolDataSource ds = (MysqlConnectionPoolDataSource)obj;
Connection con = ds.getConnection();
Statement s = con.createStatement();
ResultSet rs = s.executeQuery("select count(*) from books");
if(rs.next())
System.out.println("There are " + rs.getInt(1)
+ " records in books table.");
con.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
9.sqlserver2008's problem:
1)java連接sql server 2008遇到的問題之一:
使用 sql server 2008 時(shí),無法使用SA用戶登陸(應(yīng)該是所有sql server版本都會(huì)出現(xiàn)這樣的問題)
解決方法:
1、打Microsoft Sql Server Management Studio,以windows驗(yàn)證方式登陸,打開服務(wù)器屬性窗口(在數(shù)據(jù)庫服務(wù)器圖標(biāo)上點(diǎn)右鍵選"屬性"(right click the server name,choose property)),選擇"安全性"選項(xiàng),
2、服務(wù)器驗(yàn)證方式選擇SQL SERVERTT和windows驗(yàn)證方式,確認(rèn)
3、然后再到"安全性-登陸名"處設(shè)置SA用戶屬性,
4、設(shè)置密碼
5、狀態(tài)-啟用SA用戶
做完這五個(gè)操作后,we can now login as "sa" in Sql Server Management Studio.
2) sqlserver2008默認(rèn)情況下是禁用了tcp/ip連接。啟動(dòng)tcp/ip連接的方法,
開始 \Microsoft SQL Server 2008\配置工具\(yùn)目錄下的SQL Server Configuration Manager,選擇\sqlserver network configuration\mssqlserver協(xié)議,
然后右邊窗口有個(gè)tcp/ip協(xié)議,然后啟動(dòng)它,在啟動(dòng)TCP/IP協(xié)議后還要把其屬性
中的TCP端口號改為1433,,把sqlserver