當前位置:首頁 » 操作系統 » java資料庫配置文件

java資料庫配置文件

發布時間: 2022-04-20 19:11:52

java中調用配置文件的值與從資料庫中取出的值進行比較

讀取配置文件的代碼:
import java.io.FileInputStream;
import java.io.IOException;
import java.util.Properties;

public class App {
public static void main(String[] args) {
Properties prop = new Properties();

try {
prop.load(new FileInputStream("config/config.properties"));

System.out.println(prop.getProperty("database"));
System.out.println(prop.getProperty("dbuser"));
System.out.println(prop.getProperty("dbpassword"));
} catch(IOException e) {
e.printStackTrace();
}
}
}

⑵ 求助高手幫我解決java中連接資料庫時配置文件的問題,總是拋出空指針的異常

this.driver = prop.getProperty(driver);
是這行拋異常了,看異常也能找到。是你帶嗎的第20行。
是你的prop是空對象,說明:
InputStream is = this.getClass().getResourceAsStream(
"dbconfig.properties");
Properties prop = new Properties();
沒有讀取到配置文件,請仔細檢查配置文件是否存在。
如果是在src根目錄里 加
InputStream is = this.getClass().getResourceAsStream(
"classpath:dbconfig.properties");

⑶ java 中如何寫資料庫連接字元竄的properties配置文件

單獨定義一個文件:DBConfig.properties
driver=com.microsoft.jdbc.sqlserver.SQLServerDriver
url=jdbc:microsoft:sqlserver://localhost:1433;databasename=dbName
user=sa
password=
調用這些信息連接來資料庫.一般是在類里.
ResourceBundle bundle = ResourceBundle.getBundle("DBConfig");
String driver = bundle.getString("driver");
String url = bundle.getString("url");
String user = bundle.getString("user");
String password = bundle.getString("password");

try {
Class.forName(driver);
conn = DriverManager.getConnection(url, user, password);
} catch (ClassNotFoundException e) {
System.out.println(e.getMessage());
} catch (SQLException e) {
System.out.println(e.getMessage());
}
驗證用戶登陸其實就是一個查詢方法,根據頁面取出的內容.做個equals()判斷就可以了.

⑷ 資料庫配置文件是哪一個

配置資料庫配置文件的方法: 1.首先先創建一個db.properties的配置文件。在配置文件中輸入配置信息如下: driver=com.microsoft.sqlserver.jdbc.SQLServerDriver url=jdbc:sqlserver://localhost:1433;DatabaseName=books user=sa password=sa 2.創建一個載入db.properties的文件Env.java。在java文件中載入配置信息如下: public class Evn extends Properties{ private static Evn instance; private Evn(){ //通過構造方法讀取配置文件 InputStream is=getClass().getResourceAsStream("/db.properties"); try { load(is); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } public static Evn getInstance(){ //單例模式創建、獲得對象實例。if(instance==null){ makeInstance(); } return instance; } public static synchronized void makeInstance() { if(instance==null){ instance=new Evn(); } } } 3.讀取數據方法 public class Test { public static void main(String[] args){ String driver=Env.getInstance().getProperty("driver"); String url=Env.getInstance().getProperty("url"); String user=Env.getInstance().getProperty("user"); String password=Env.getInstance().getProperty("password"); System.out.println(driver); System.out.println(url); System.out.println(user); System.out.println(password); } }

⑸ 配置資料庫配置文件的方法

1.首先先創建一個db.properties的配置文件。 在配置文件中輸入配置信息如下: driver=com.microsoft.sqlserver.jdbc.SQLServerDriver url=jdbc:sqlserver://localhost:1433;DatabaseName=books user=sa password=sa 2.創建一個載入db.properties的文件Env.java。 在java文件中載入配置信息如下: public class Evn extends Properties{ private static Evn instance; private Evn(){ //通過構造方法讀取配置文件 InputStream is=getClass().getResourceAsStream("/db.properties"); try { load(is); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } public static Evn getInstance(){ //單例模式創建、獲得對象實例。 if(instance==null){ makeInstance(); } return instance; } public static synchronized void makeInstance() { if(instance==null){ instance=new Evn(); } } } 3.讀取數據方法 public class Test { public static void main(String[] args){ String driver=Env.getInstance().getProperty("driver"); String url=Env.getInstance().getProperty("url"); String user=Env.getInstance().getProperty("user"); String password=Env.getInstance().getProperty("password"); System.out.println(driver); System.out.println(url); System.out.println(user); System.out.println(password); } }

滿意請採納

⑹ java連接mysql的配置文件

driver=com.mysql.jdbc.Driver
url=jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=utf8
user=xxx
password=xxx

選中Window選項中Preference,在彈出的菜單中輸入spell,把第一個復選框「Enable spell checking「給去掉

⑺ java 配置資料庫文件是哪個文件

java本身並沒有規定資料庫配置文件必須寫到哪個文件中。開發者自己決定。tomcat的資料庫配置一般寫到server.xml中,但不是絕對的。

⑻ java 怎麼獲取資料庫的編碼以及配置信息

Java也可以執行Mysql命令,你可以使用sql
語句,如下:
mysql>
SHOW
VARIABLES
LIKE
'%char%set%';
+--------------------------+----------------------------------+
|
Variable_name
|
Value
|
+--------------------------+----------------------------------+
|
character_set_client
|
utf8
|
|
character_set_connection
|
utf8
|
|
character_set_database
|
gbk
|
|
character_set_filesystem
|
binary
|
|
character_set_results
|
utf8
|
|
character_set_server
|
gbk
|
|
character_set_system
|
utf8
|
|
character_sets_dir
|
/u01/mysql/share/mysql/charsets/
|
+--------------------------+----------------------------------+
8
rows
in
set
mysql>
SHOW
VARIABLES
LIKE
'port';
+---------------+-------+
|
Variable_name
|
Value
|
+---------------+-------+
|
port
|
3306
|
+---------------+-------+
1
row
in
set
你如果想獲取所有配置信息可以使用:
SHOW
VARIABLES命令。

⑼ 資料庫配置文件怎麼填寫

一般放置在配置文件中按以下方式樓主寫的app.config使用Web.config是相同的節點
<新增名稱=「資料庫」
的connectionString
=「供應商=
Microsoft.Jet。
OLEDB.4.0;數據源=數據\
data.mdb中「
的providerName
=」System.Data.OleDb「p>
程序這樣的引用ConfigurationManager中。
。的ConnectionStrings
[「資料庫」]的ConnectionString;注意添加引用

熱點內容
會員注冊源碼 發布:2024-10-01 09:15:57 瀏覽:369
linux內 發布:2024-10-01 09:13:39 瀏覽:918
火葯可以壓縮 發布:2024-10-01 09:11:39 瀏覽:70
為什麼微信清除了緩存 發布:2024-10-01 09:03:20 瀏覽:848
如何關閉手機私密密碼 發布:2024-10-01 08:31:49 瀏覽:482
androidframework學習 發布:2024-10-01 08:20:06 瀏覽:63
思維編程化 發布:2024-10-01 08:19:00 瀏覽:404
尊雲伺服器對接 發布:2024-10-01 08:16:36 瀏覽:3
qq賬號密碼忘在哪裡 發布:2024-10-01 07:39:43 瀏覽:669
ade雲存儲平台 發布:2024-10-01 07:18:57 瀏覽:270