java 怎么获取数据库的编码以及配置信息?

java 怎么获取数据库的编码以及配置信息?,第1张

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命令。

1.jdbc.properties

driver=com.microsoft.jdbc.sqlserver.SQLServerDriver

url=jdbc:microsoft:sqlserver://Ip地址:1433databaseName=test

user=test

password=test

2.一个Servlet

import java.io.PrintStream

import java.sql.*

import java.util.ResourceBundle

public class DBOperator

{

public DBOperator()

{

}

public static void main(String args[])

{

Connection con = getConnection()

System.out.println("connection success")

closeConnection(con)

System.out.println("close success")

}

public static Connection getConnection()

{

try

{

return DriverManager.getConnection(url, user,

password)

}

catch(SQLException e)

{

e.printStackTrace()

}

return null

}

public static void closeConnection(Connection con)

{

try

{

if(con != null)

con.close()

}

catch(SQLException e)

{

e.printStackTrace()

}

}

private static String url

private static String user

private static String password

static

{

ResourceBundle bundle = ResourceBundle.getBundle("路径")

try

{

Class.forName(bundle.getString("driver"))

}

catch(ClassNotFoundException e)

{

e.printStackTrace()

}

url = bundle.getString("url")

user = bundle.getString("user")

password = bundle.getString("password")

}

}


欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/sjk/6821571.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-03-28
下一篇 2023-03-28

发表评论

登录后才能评论

评论列表(0条)

保存