Database的Connection String编写方法

 在做Database项目开发的时候,首先应该解决数据库连接的问题,这里最关键的是Connection String的填写,下面是收集的help:

The hardest part of this procedure for most people is getting the ConnectionString to work.

For help, see these links, the "SqlConnection (.NET)" sections
http://www.connectionstrings.com/?carrier=sqlserver2005
http://www.sqlstrings.com/SQL-Server-connection-strings.htm
http://articles.techrepublic.com.com/5100-3513_11-6084879.html

Another thing you might try to get the connection string right is the following:

1. Create a new blank file and name it test.udl.

2. Double click on it, and a "Data Link Properties" dialog should appear.

3. On "Providers" tab, select "Microsoft OLE DB Provider for SQL Server" or "SQL Native Client"

4. On "Connections" tab, try various settings and use the the "Test Connection" button to test them. Click "Ok" when it works.

5. Open the test.udl file in Notepad and copy the line that starts with "Provider=" into your Web.config "ConnectionString" value, BUT delete the little part that says "Provider=SQLNCLI.1;"

DataSource在JDBC(Java Database Connectivity)中扮演关键角色,它是一个管理数据库连接的对象,通过其`getConnection()`方法获取到连接后,你可以使用SQL(Structured Query Language)来执行查询操作。当你编写一个SQL查询,例如一个SELECT语句,然后通过Connection的PreparedStatement、Statement或者其他相关的API执行,查询结果通常会返回一个ResultSet对象。 将SQL转换为List的过程大致如下: 1. **创建Statement/PreparedStatement**: 创建一个PreparedStatement实例,可以防止SQL注入攻击,并提供更好的性能(预编译),比如: ```java String sql = "SELECT * FROM table WHERE condition"; PreparedStatement pstmt = connection.prepareStatement(sql); ``` 2. **设置参数** (如果有条件): 如果SQL中有占位符 (?, ?), 使用`setXXX()`方法设置参数值。 3. **执行查询**: 调用`executeQuery()`方法执行SQL并获取ResultSet对象。 ```java ResultSet rs = pstmt.executeQuery(); ``` 4. **遍历结果集**: 使用`next()`方法逐条读取结果行,并将其转化为适合的数据结构(如List)。例如,如果结果是自增的,你可以使用`List<Map<String, Object>>`,每一条记录映射为一个Map: ```java List<Map<String, Object>> list = new ArrayList<>(); while (rs.next()) { Map<String, Object> row = Maps.newHashMap(); for (int i = 1; i <= rs.getMetaData().getColumnCount(); i++) { row.put(rs.getColumnName(i), rs.getObject(i)); } list.add(row); } ``` 5. **关闭资源**: 最后别忘了关闭ResultSet、PreparedStatement和Connection
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值