連接資料庫的配置文件
❶ Spring配置文件中怎麼配置資料庫連接
xml配置文件中配置如下:
java代碼 收藏代碼
<spring:bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<spring:property name="locations">
<spring:list>
<spring:value>classpath:conf/jdbc.properties</spring:value>
</spring:list>
</spring:property>
</spring:bean>
<spring:bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"
destroy-method="close">
<spring:property name="driverClassName"
value="${jdbc.driverClassName}" />
<spring:property name="url"
value="${jdbc.url}" />
<spring:property name="username" value="${jdbc.username}" />
<spring:property name="password" value="${jdbc.password}" />
<spring:property name="maxActive" value="30" />
<spring:property name="maxIdle" value="10" />
<spring:property name="maxWait" value="1000" />
<spring:property name="defaultAutoCommit" value="true" />
</spring:bean>
jdbc.properties配置文件中連接資料庫配置:
Java代碼 收藏代碼
#sqlserver數據源配置
jdbc.driverClassName=com.microsoft.sqlserver.jdbc.SQLServerDriver
jdbc.url=jdbc:sqlserver://192.168.1.60:1408;DatabaseName=test
jdbc.username=sa
jdbc.password=1
#mysql數據源配置
jdbc.driverClassName=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://192.168.1.60:3306/test
jdbc.username=root
jdbc.password=root
#Oracle數據源配置
jdbc.driverClassName=oracle.jdbc.driver.OracleDriver
jdbc.url=jdbc:oracle:thin:@10.60.17.9:1521:orcl
jdbc.username=admin
jdbc.password=admin
❷ 連接兩個資料庫 spring配置文件怎麼配置
這位朋友,根據您的問題解答如下,如有不明之處可以繼續補充:
你在web.xml里這么配置沒問題啊,我載入兩個spring的配置文件都是用逗號分割的,這個逗號是半形的,不是全形的。一般都是配置多個,放一個,services放一個。
❸ C#連接oracle資料庫Add.config配置文件怎麼寫
C#連接Oracle資料庫(查詢數據)
using System;
using System.Collections.Generic;
using System.ComponentModel;
//這行和下一行都要先在引用中填加system.data.oracleclient
using System.Data.OracleClient;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
namespace WindowsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
#region 從region到endregion是手工寫的。別的都是系統自動生成的
//定義連接資料庫的字元串
string constring = "data source=wzd;user=wzd;password=wzd;";
//進行連接
OracleConnection conn = new OracleConnection(constring);
try
{
conn.Open();//打開指定的連接
OracleCommand com = conn.CreateCommand();
//寫好想執行的Sql語句
com.CommandText =
"select name from mytable where card_no='0000000002'";
OracleDataReader odr = com.ExecuteReader();
//讀取數據,如果返回為false的話,就說明到記錄集的尾部了
while (odr.Read())
{
//將讀取到的值顯示到定義的控制項中。
this.lbl.Text = odr.GetOracleString(0).ToString();
}
odr.Close();//關閉reader.這是一定要寫的
}
catch
{
MessageBox.Show("erro");//如果發生異常,則提示出錯
}
finally
{
conn.Close();//關閉打開的連接
}
#endregion
}
}
}
C#中與Oracle連接的代碼
注意:一定要添加這個:
項目->添加引用->.NET->System.Data.OracleClient.dll
using System;
using System.Data;
using System.Windows.Forms;
using System.Data.OracleClient;
namespace Test
{
/// <summary>
/// 簡潔期間,直接將實現寫在構造函數中
/// </summary>
public class Test
{
public Test()
{
//
// TODO: 在此處添加構造函數邏輯
//
string ConnectionString =
"Data Source=LiPu; User Id=SCOTT; Password=scott";
//連接字元串,Data Source 是指資料庫名字.如我用的是本機的Oracle
//的資料庫,名字為LiPu. user id 是
//用戶名,你可以用System 或是你自己添加的一個用戶.Password是
//對應用戶的密碼.
//創建一個新連接
OracleConnection conn = new OracleConnection(ConnectionString);
try
{
conn.Open(); //打開連接
OracleCommand cmd = conn.CreateCommand();
cmd.CommandText = "select * from emp"; //SQL語句
OracleDataReader rs = cmd.ExecuteReader();
//讀取數據,如果rs.Read()返回為false的話,就說明到記錄集的尾部了
while(rs.Read())
{
MessageBox.Show(rs.GetString(1));
}
rs.Close();
}
catch (Exception e)
{
MessageBox.Show(e.Message);
}
finally
{
conn.Close();
}
}
}
}
C#連接Oracle資料庫(更改資料庫中的記錄並查詢更改後的數據)
using System;
using System.Collections.Generic;
//這行和下一行都要先在引用中填加system.data.oracleclient
using System.ComponentModel;
using System.Data.OracleClient;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
namespace WindowsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
#region 從region到endregion是手工寫的。別的都是系統自動生成的
//定義連接資料庫的字元串
string constring = "data source=wzd;user=wzd;password=wzd;";
//進行連接
OracleConnection conn = new OracleConnection(constring);
try
{
conn.Open();//打開指定的連接
OracleCommand com = conn.CreateCommand();
com.CommandText =
"select name from fin_ipr_inmaininfo where card_no="+
"'0000000002'";//寫好想執行的Sql語句
OracleDataReader odr = com.ExecuteReader();
//讀取數據,如果返回為false的話,就說明到記錄集的尾部了
while (odr.Read())
{
//將讀取到的值顯示到定義的控制項中。
this.lbl.Text = odr.GetOracleString(0).ToString();
}
odr.Close();//關閉reader.這是一定要寫的
}
catch
{
MessageBox.Show("erro");//如果發生異常,則提示出錯
}
finally
{
conn.Close();//關閉打開的連接
}
#endregion
}
private void button2_Click(object sender, EventArgs e)
{
#region 從region到endregion是手工寫的。別的都是系統自動生成的
//定義連接資料庫的字元串
string constring = "data source=wzd;user=wzd;password=wzd;";
//進行連接
OracleConnection conn = new OracleConnection(constring);
try
{
conn.Open();//打開指定的連接
OracleCommand com = conn.CreateCommand();
//寫好想執行的Sql語句
com.CommandText =
"update fin_ipr_inmaininfo set name='wzd' where card_no='0000000002'";
com.ExecuteNonQuery();
}
catch
{
MessageBox.Show("erro");//如果發生異常,則提示出錯
}
finally
{
conn.Close();//關閉打開的連接
}
#endregion
}
}
}
❹ java怎麼連接資料庫配置文件
你是說properties文件嗎?
//讀取配置文件DbUtil.properties,這里的DbUtil是此文件里的一個類,就是當前類
p.load(DbUtil.class.getClassLoader().getResourceAsStream("DbUtil.properties"));
//獲取配置文件中的相關參數值
driver=p.getProperty("mysqlDriver");
url=p.getProperty("mysqlUrl");
user=p.getProperty("mysqlUser");
password=p.getProperty("mysqlPassword");
這里是DbUtil.properties文件里的內容:
##oracledatabase
oracleDriver=oracle.jdbc.driver.OracleDriver
oracleUrl=jdbc:oracle:thin:@localhost:1521:orcl
oracleUser=scott
oraclePassword=tiger
##mysqldatabase
mysqlDriver=com.mysql.jdbc.Driver
mysqlUrl=jdbc:mysql://localhost:3306/db_test
mysqlUser=root
mysqlPassword=root
❺ mysql的數據連接池怎麼配置文件
mysql的數據連接池怎麼配置文件
連接先建立一些連接,並且這些連接允許共享,因此這樣就節省了每次連接的時間開銷。Mysql資料庫為例,連接池在Tomcat中的配置與使用。
1、創建資料庫Student,表student
2、配置server.xml文件。Tomcat安裝目錄下conf中server.xml文件。
<GlobalNamingResources>
<Resource
name="jdbc/DBPool"
type="javax.sql.DataSource"
password=""
driverClassName="com.mysql.jdbc.Driver"
maxIdle="2"
maxWait="5000"
username="root"
url="jdbc:mysql://localhost:3306/student"
maxActive="3"
/>
</GlobalNamingResources>
name:指定連接池的名稱
type:指定連接池的類,他負責連接池的事務處理
url:指定要連接的資料庫
driverClassName:指定連接資料庫使用的驅動程序
username:資料庫用戶名
password:資料庫密碼
maxWait:指定最大建立連接等待時間,如果超過此時間將接到異常
maxIdle:指定連接池中連接的最大空閑數
maxActive:指定連接池最大連接數
3、配置web.xml文件。
<web-app>
<resource-ref>
<description>mysql資料庫連接池配置</description>
<res-ref-name>jdbc/DBPool</res-ref-name>
<res-type>javax.sql.DataSource</res-type>
<res-auth>Container</res-auth>
<res-sharing-scope>Shareable</res-sharing-scope>
</resource-ref>
</web-app>
4、配置context.xml文件
與server.xml文件所在的位置相同。
<Context>
<ResourceLink
name="jdbc/DBPool"
type="javax.sql.DataSource"
global="jdbc/DBPool"
/>
</Context>
5、測試
DataSource pool = null;
Context env = null;
Connection conn = null;
Statement st = null;
ResultSet rs = null;
try{
env = (Context)new InitialContext().lookup("java:comp/env");
//檢索指定的對象,返回此上下文的一個新實例
pool = (DataSource)env.lookup("jdbc/DBPool");
//獲得資料庫連接池
if(pool==null){out.printl("找不到指定的連接池!");}
con = pool.getConnection();
st = con.createStatement();
rs = st.executeQuery("select * from student");
}catch(Exception ex){out.printl(ne.toString());}
❻ 如何在配置文件里添加資料庫鏈接
直接在spring的配置文件中applicationContext.xml文件中配置資料庫連接也可以,但是有個問題,需要在url後帶著使用編碼集和指定編碼集,出現了如下問題,&這個符號報錯。
既然這樣只能使用外部配置文件設置一些參數,在spring的配置文件applicationContext.xml中獲取,然後配置連接資料庫。
使用properties配置文件連接資料庫,在src下新建jdbc.properties文件,按照自己的資料庫名,用戶名密碼更改下面的配置。
在spring的配置文件applicatiContext.xml中加入(這里是引入配置文件),更改之前的資料庫配置,名稱和配置文件中的對應上即可。
❼ C#如何用配置文件連接資料庫寫好的配置文件放在哪裡
你新建項目的時候,會有一個web.config的配置文件的
在裡面的<connectionStrings</connectionStrings
<add一個資料庫鏈接字元串就OK了,例如「<add
connectionString="Data
Source=.;Initial
Catalog=test;Integrated
Security=True;Connect
Timeout=30;"
name="DBHelplerDbConnection"/」
這個就是了,connectionString代表資料庫鏈接字元串,name代表你想要引用的時候查找的名稱。
完成以上的配置文件,就在你的DBhelper類里用ConfigurationManager進行連接
以下是我寫的例子=
=。這是原創回答,如有錯誤,希望別見怪哈
string
strCon
=
ConfigurationManager.ConnectionStrings["這里是你剛才配置文件里連接的NAME"].ConnectionString;
就可以直接打開連接了,不知道對你有沒有幫助·-·當學術交流吧
❽ 查詢oracle資料庫連接配置文件是否正確
摘要 Oracle客戶端tnsnames.ora連接配置