简体   繁体   English

beans.xml中的ms sql配置

[英]ms sql configuration in beans.xml

I've installed the MS SQL Server 2008 and I want to use it in a Java project with Struts. 我已经安装了MS SQL Server 2008,并且想在Struts的Java项目中使用它。 Unfortunately I cannot configure it in Java. 不幸的是,我无法在Java中对其进行配置。 I am using the Windows authentication for MsSql. 我正在使用Windows身份验证的MsSql。 Is that possible? 那可能吗?

My beans.xml file looks like: 我的beans.xml文件如下所示:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" 
  "http://www.springframework.org/dtd/spring-beans.dtd">

<beans>

    <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
        <property name="driverClassName" value="com.microsoft.sqlserver.jdbc.SQLServerDriver" />
        <!-- S2-Install-Start: INSERT DB SERVER HERE -->
        <property name="url" value="jdbc:sqlserver://localhost:1433;DatabaseName=MyDatabase" />
        S2-Install-End:
        <property name="username" value="Stefana\Steffi" />
        S2-Install-Start: INSERT DB PASSWORD HERE
        <property name="password" value="" />
        S2-Install-End:
    </bean>

I don't know what should I write in the user and password fields? 我不知道该在用户名和密码字段中写什么? When I connect to the Ms Sql Server, I get the following: 当我连接到Ms Sql Server时,得到以下信息:

Servertype: DatabaseEngine

Servername: Stefana\SQLEXPRESS

Authentication: Windows Authentication

Username:Stefana\Steffi

Password:

You can use Windows authentication if you use the jTDS JDBC Driver for SQL Server. 如果将jTDS JDBC驱动程序用于SQL Server,则可以使用Windows身份验证。 The jTDS driver is free, open source and generally more powerful than the official Microsoft one. jTDS驱动程序是免费的,开源的,并且通常比Microsoft官方驱动程序更强大。

If you include the jTDS jar in your project, I believe this datasource URL should work: 如果您在项目中包含jTDS jar,我相信此数据源URL应该可以工作:

jdbc:jtds://localhost:1433/MyDatabase;domain=Stefana

Because you're running on Windows the jTDS driver is capable of using a native library to automatically log you in with your current credentials. 因为您在Windows上运行,所以jTDS驱动程序能够使用本机库自动使用当前凭据登录。 However it's usually a better idea to explicitly specify the username and password, because that way your web app won't behave differently depending on who starts it up. 但是,通常最好是明确指定用户名和密码,因为那样一来,您的Web应用程序的行为就不会因启动它的人而有所不同。

Note also that SQL Server 2008 probably won't have TCP connections enabled by default. 还要注意,默认情况下,SQL Server 2008可能不会启用TCP连接。 Unless you turn that on you won't be able to connect with either the Microsoft driver or the jTDS one. 除非您将其打开,否则将无法与Microsoft驱动程序或jTDS连接。

To enable TCP connections: 要启用TCP连接:

  • Open Sql Server Configuration Manager (it should be on your Start menu) 打开Sql Server配置管理器 (应该在“开始”菜单上)
  • In the tree on the left, navigate to SQL Server Network Configuration then Protocols for MSSQLSERVER 在左侧的树中,导航到“ SQL Server网络配置 ”,然后导航到“ MSSQLSERVER的协议”
  • You should see TCP/IP in the list of protocols. 您应该在协议列表中看到TCP / IP
  • If its status is Disabled then double-click it, and change the Enabled option to Yes and click OK 如果其状态为“ 禁用”,则双击它,并将“ 启用”选项更改为“ 是” ,然后单击“ 确定”。
  • You now need to restart SQL Server. 现在,您需要重新启动SQL Server。 Navigate to the SQL Server Services item 导航到“ SQL Server服务”
  • Right-click on SQL Server (MSSQLSERVER) in the list of services 右键单击服务列表中的SQL Server(MSSQLSERVER)
  • Choose Restart 选择重新启动
  • You should now be able to connect to SQL Server from your Java web app 您现在应该可以从Java Web应用程序连接到SQL Server

This is not the right thing to do. 这不是正确的事情。

Do you own that database? 您是否拥有该数据库? Your config says "localhost", so I'll assume yes. 您的配置说“ localhost”,所以我假设是。 You should be using another username and password for your application from SQL Server, not Windows Authentication. 您应该使用来自SQL Server(而不是Windows身份验证)的应用程序的其他用户名和密码。 I'd create a separate user just for this application. 我将为此应用程序创建一个单独的用户。 Give it access only to the application schema, and GRANT the minimum permissions necessary to accomplish its mission (eg no DELETE permission if not needed; no access to SYSTEM tables if not needed; no running stored procedures if not needed). 仅授予其访问应用程序模式的权限,并授予GRANT完成其任务所需的最低权限(例如,如果不需要,则没有DELETE权限;如果不需要,则没有对SYSTEM表的访问权;如果不需要,则没有正在运行的存储过程)。

A better solution is to use a JNDI data source and not have passwords in plain text on your machine. 更好的解决方案是使用JNDI数据源,并且在计算机上不使用纯文本格式的密码。

replace the property: 替换属性:

<property name="url" value="jdbc:sqlserver://localhost:1433;DatabaseName=MyDatabase" />

for: 对于:

<property name="url" value="jdbc:sqlserver://localhost:1433;DatabaseName=MyDatabase;" />

Done!!! 做完了!!!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM