连接到SQL Server [英] Connetion to SQL server
问题描述
大家好,
我有一个问题.
我需要使用Java代码从Unix框中调用SQL Server中的存储过程.
有人可以帮帮我吗.
在SQL中,我知道数据库名称,用户名,密码,存储过程名称以及参数.
如果您知道任何驱动程序/方法,请告诉我.
谢谢和问候,
Sushil
Hi All,
I have one problem.
I need to call a stored procedure in SQL server from Unix box using java code.
Can some one help me.
In SQL i know the database name,user name,password,stored procedure name as well as the parameters.
Please let me know if you know any drivers/way of doing it.
Thanks and regards,
Sushil
推荐答案
您可能找不到有用的链接:
-连接到数据库并调用存储过程 [ http://onjava.com/onjava/2003/08/13/stored_procedures.html [ ^ ]
- Java程序员的存储过程 [
Few links you may find useful:
- Connect to database and call stored procedure[^]
- http://onjava.com/onjava/2003/08/13/stored_procedures.html[^]
- Stored Procedures for Java Programmers[^]
大家好,
感谢所有的答复.我自己找到了解决方案.
Hi All,
Thanks for all the replies. I have found the solution myself.
/**
* Program to connect to SQL server which will trigger stored procedure
*/
import java.sql.*;
class ConnectMSSQLServer
{
public void dbConnect(String db_connect_string,String user,String password)
{
try
{
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
Connection conn = DriverManager.getConnection(db_connect_string,user,password);
System.out.println("---------connected-----");
CallableStatement strd_prod = conn.prepareCall("{call dbo.Stored_Procedure_name('param1','param2','param3',..)}");
int i = strd_prod.executeUpdate();
conn.close();
System.out.println(i);
}
catch (Exception e)
{
e.printStackTrace();
}
}
public static void main(String[] args)
{
ConnectMSSQLServer connServer = new ConnectMSSQLServer();
connServer.dbConnect("jdbc:sqlserver://Server_Name;databaseName=database_name","user_name","password");
}
}
如果您的服务器名称为myServer/Instance_name,则必须使用这样的转义字符
If you have server name like myServer/Instance_name then you have to use escape characters like this
connServer.dbConnect("jdbc:sqlserver://Server_Name\\Instance_name;databaseName=database_name","user_name","password");
要获取JDBC驱动程序,请点击以下链接.
http://msdn.microsoft.com/en-us/sqlserver/aa937724 [ ^ ]
谢谢
To get the JDBC driver follow the below link.
http://msdn.microsoft.com/en-us/sqlserver/aa937724[^]
Thanks
这篇关于连接到SQL Server的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!