新手求指教 JAVA 操作跟读取SQL 2000的方法。

xiaopang187 2011-08-09 11:34:33
各位高手大大们。 JAVA 操作连接 SQL怎么弄啊
我听说下载驱动包 我也下了 不知道是那个版本 也不知道放到那个文件夹
问了好久也没结果、 还有SQL SP4也打了补丁 。。剩下的完全不懂了 T T。
貌似后面还要设置什么开放端口 1433 用户名连接什么的 、
能不能给我哥完整的流程呀 谢谢了
...全文
66 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
softroad 2011-08-09
  • 打赏
  • 举报
回复

//=====================================================================
//
// File: connectURL.java
// Summary: This Microsoft SQL Server JDBC Driver sample application
// demonstrates how to connect to a SQL Server database by using
// a connection URL. It also demonstrates how to retrieve data
// from a SQL Server database by using an SQL statement.
//
//---------------------------------------------------------------------
//
// This file is part of the Microsoft SQL Server JDBC Driver Code Samples.
// Copyright (C) Microsoft Corporation. All rights reserved.
//
// This source code is intended only as a supplement to Microsoft
// Development Tools and/or on-line documentation. See these other
// materials for detailed information regarding Microsoft code samples.
//
// THIS CODE AND INFORMATION ARE PROVIDED "AS IS" WITHOUT WARRANTY OF
// ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO
// THE IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A
// PARTICULAR PURPOSE.
//
//=====================================================================

import java.sql.*;

public class connectURL {

public static void main(String[] args) {

// Create a variable for the connection string.
String connectionUrl = "jdbc:sqlserver://localhost:1433;" +
"databaseName=AdventureWorks;integratedSecurity=true;";

// Declare the JDBC objects.
Connection con = null;
Statement stmt = null;
ResultSet rs = null;

try {
// Establish the connection.
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
con = DriverManager.getConnection(connectionUrl);

// Create and execute an SQL statement that returns some data.
String SQL = "SELECT TOP 10 * FROM Person.Contact";
stmt = con.createStatement();
rs = stmt.executeQuery(SQL);

// Iterate through the data in the result set and display it.
while (rs.next()) {
System.out.println(rs.getString(4) + " " + rs.getString(6));
}
}

// Handle any errors that may have occurred.
catch (Exception e) {
e.printStackTrace();
}

finally {
if (rs != null) try { rs.close(); } catch(Exception e) {}
if (stmt != null) try { stmt.close(); } catch(Exception e) {}
if (con != null) try { con.close(); } catch(Exception e) {}
}
}
}


运行有错的话把错误信息发来。
xiaopang187 2011-08-09
  • 打赏
  • 举报
回复
大哥 那要这个代码就行了嘛 我下的驱动包还没放到文件夹里。
还有我刚学JAVA不久 这个代码不是很理解
还有没看到怎么操作SQL呀
xiaopang187 2011-08-09
  • 打赏
  • 举报
回复
嗯 好的 谢谢 我试试

51,409

社区成员

发帖
与我相关
我的任务
社区描述
Java相关技术讨论
javaspring bootspring cloud 技术论坛(原bbs)
社区管理员
  • Java相关社区
  • 小虚竹
  • 谙忆
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧