亿点连接怎么使用用JDBC连接SQL Server

当前位置:&&技术问答>
连接sqlserver的url是jdbc:microsoft:sqlserver://192.168.0.100:1433,他连那一个数据库呢?
&&&&来源:&互联网& 发布时间:
&&&&本文导语:&
在我的SQL SERVER中有很多个数据库,我连接sqlserver的url是jdbc:microsoft:sqlserver://192.168.0.100:1433,结果那连接的是我的第一个数据库,我如何修改它连别的数据库呢?
jdbc:microsoft:sqlserver://192.168.0....
在我的SQL SERVER中有很多个数据库,我连接sqlserver的url是jdbc:microsoft:sqlserver://192.168.0.100:1433,结果那连接的是我的第一个数据库,我如何修改它连别的数据库呢?
jdbc:microsoft:sqlserver://192.168.0.100:1433:databasename=yourdbname
可以在getConnection的address里面声明,如楼上,也可以使用con.setCatalog(database);来确定
给你个小例子,你看看就会明白了
import java.sql.*;
public class Test {
public static void main(String[] args) {
String address = "jdbc:microsoft:sqlserver://192.168.0.24:1433";//本机ip
String user="sa";//数据库用户密码
String passwd="";//口令
String database = "TESTDB";//目标数据库
Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver");//申明
Connection con = DriverManager.getConnection(address,user,passwd);//建立链接
con.setCatalog(database);//确定目标数据库名
Statement smt = con.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_READ_ONLY);
String selCode = "SELECT * FROM UserLog";
ResultSet r = smt.executeQuery(selCode);
if(r.last()) {//取结果集结果数目
i = r.getRow();
System.out.println(i);
catch(Exception e) {
System.out.println(e);//捕捉异常
您可能感兴趣的文章:
本站()旨在分享和传播互联网科技相关的资讯和技术,将尽最大努力为读者提供更好的信息聚合和浏览方式。本站()站内文章除注明原创外,均为转载、整理或搜集自网络。欢迎任何形式的转载,转载请注明出处。
相关文章推荐
特别声明:169IT网站部分信息来自互联网,如果侵犯您的权利,请及时告知,本站将立即删除!
(C)2012-,E-mail:www_169it_(请将#改为@)JDBC URL Formats for the vCenter Server Database
JDBC URL Formats for the vCenter Server Database
The vCenter Server installer generates and validates the JDBC URL for the vCenter Server database. If the installer fails to connect to the database using the generated JDBC URL, the installer will prompt you to specify the JDBC URL.
JDBC URL Formats for Microsoft SQL Server Databases
For Microsoft SQL Server databases, you can use the following example JDBC URLs as a model:
Connect to default (unnamed) SQL Server instance by host name:
jdbc:sqlserver://host;databaseName=database
Connect to named instance by host name and instance name:
jdbc:sqlserver://host;instanceName=instance;databaseName=database
Connect to SQL Server by host name and port:
jdbc:sqlserver://host:port;databaseName=database
Connect by port:
jdbc:sqlserver://localhost:1422;databaseName\=VIM_VCDB (user name, password, and database type to be passed separately)
Connect to local server with integrated security:
jdbc:sqlserver://localhost\\SQLEXP_VIM;databaseName=VIM_VCDB;integratedSecurity=true
Connect to local server without integrated security:
jdbc:sqlserver://localhost\\SQLEXP_VIM;databaseName\=VIM_VCDB
(user name, password, and database type to be passed separately)
VMware vCenter Server JDBC configuration for Microsoft SQL Server might not work by default with direct IPv6 addresses. You must use one of the following forms:
Use the host name form for a standard Type-4 JDBC URL (recommended):
jdbc:sqlserver://database-fully-qualified-host-name:port
Use direct IPv6 address format:
jdbc:sqlserver://;serverName=[IPv6-address]
For more information about JDBC URL formatting for MS SQL databases, including port and instance configuration options, see the
Web site. At the time of this topic's publication, the information was available at .
JDBC URL Formats for Oracle Databases
For Oracle databases, you can use the following example JDBC URLs as a model:
This format requires host name and address, port (default 1521) and service name (for example, "oracle.world"):
jdbc:oracle:thin:@host:port/service
This format requires host name and address, port (default 1521) and SID (for example, "ORCL"):
jdbc:oracle:thin:@host:port:SID
This format is for a fully configured Oracle client with Oracle Net, which is useful for non-TCP configuration or Oracle RAC (real application clusters):
jdbc:oracle:thin:@tnsname
The following example is for an Oracle RAC with a thin driver, without the full Oracle client installed:
jdbc:oracle:thin:@(DESCRIPTION=(ADDRESS=(PROTOCOL=TCP)(HOST=rac1-vip)(PORT=1521))(ADDRESS=(PROTOCOL=TCP)(HOST=rac2-vip)(PORT=1521))(LOAD_BALANCE=yes)(FAILOVER=ON)(CONNECT_DATA=(SERVER=DEDICATED)(SERVICE_NAME=RAC.DBTEAM)(FAILOVER_MODE=(BACKUP=rac1)(TYPE=SELECT)(METHOD=BASIC)))))
In this example, rac1-vip is first node virtual IP, rac2-vip is second node virtual IP, RAC.DBTEAM is RAC DB service name, and rac1 is name of failover node.
For more information about JDBC URL formatting for Oracle databases, see
Web site. At the time of this topic's publication, the information was available at
JDBC URL Formats for IBM DB2 Databases
For IBM DB2 databases, you can use the following example JDBC URLs as a model:
This format requires host name and address, port (for example, 50000) and database name (as created at the server):
jdbc:db2://host:port/database
This format is for a fully configured DB2 client (namely "IBM Data Server Client"), where
is the local database alias. This example is useful for non-TCP configurations:
jdbc:db2:database
For more information about
JDBC URL formatting for IBM DB2 databases, see the publib. Web site.Java通过JDBC连接上Sql server2012_百度文库
两大类热门资源免费畅读
续费一年阅读会员,立省24元!
Java通过JDBC连接上Sql server2012
&&Java通过JDBC连接上Sql server2012
阅读已结束,下载文档到电脑
想免费下载更多文档?
定制HR最喜欢的简历
下载文档到电脑,方便使用
还剩4页未读,继续阅读
定制HR最喜欢的简历
你可能喜欢步骤分为3部:1.通过sql server 配置管理器配置1433端口
      & 2.将sqljdbc41.jar类库添加到对应的工程中
      & 3.在java程序中连接数据库
步骤1:打开sql server 配置管理器,点击TCP/IP右键,选择启用。将禁用的TCP/IP协议打开。
然后重启sql server(mssqlserver)服务,使得tcp/ip协议生效。
步骤2:到microsoft官网下载sqljdbc41.jar类库。/zh-CN/download/details.aspx?id=11774
将下载的压缩包解压,找到sqljdbc41.jar类库即可。
然后进入eclipse界面,找到的当前工程文件,点击右键,选中properties-&Libraries-&add external jars-&找到我们刚刚下载到的sqljdbc41.jar类库,添加即可。
import java.sql.*;public class test2 {&& &public static void main(String[] args) {&& &&& &// TODO Auto-generated method stub&& &&& &PreparedStatement ps=&&&&& //(这里也可以使用statement,视情况而定)&& &&& &Connection ct=&& &&& &ResultSet rs=&& &&& &&& &&& &try {&& &&& &&& &&& &&& &&& &//1.加载驱动&& &&& &&& &Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");&& &&& &&& &String url="jdbc:sqlserver://localhost:1433;databaseName=test1";&& &&& &&& &String user="sa";//sa超级管理员&& &&& &&& &String password="123456";//密码&& &&& &&& &//2.连接&& &&& &&& &ct=DriverManager.getConnection( url,user,password);&& &&& &&& &//3.创建发送端&& &&& &&
&&&&&&&&&&& pstmt&=&conn.prepareStatement("INSERT&INTO&staff(name,&age)&VALUES&(?,&?)");&
      pstmt.setString(1, newen);&&&&&&&&&&
&      pstmt.setInt(2, 25);&&&&
      pstmt.executeUpdate();
        System.out.println("成功插入一条数据记录!");& && &&& &&& &}&& &&& &} catch (Exception e) {&& &&& &&& &// TODO: handle exception&& &&& &&& &e.printStackTrace();&& &&& &}finally{&& &&& &&& &&& &&& &&& &&& &&& &//关闭资源,加强程序的健壮性&& &&& &&& &try {&& &&& &&& &&& &if(rs!=null){&& &&& &&& &&& &&& &rs.close();&& &&& &&& &&& &}&& &&& &&& &&& &if(ps!=null){&& &&& &&& &&& &&& &ps.close();&& &&& &&& &&& &}&& &&& &&& &} catch (Exception e) {&& &&& &&& &&& &// TODO Auto-generated catch block&& &&& &&& &&& &e.printStackTrace();&& &&& &&& &}&& &&& &}&& &}}
至此,jdbc连接数据库over!!!
效果类似如下:
阅读(...) 评论()

我要回帖

更多关于 怎么使用宽带连接 的文章

 

随机推荐