重庆分公司,新征程启航
为企业提供网站建设、域名注册、服务器等服务
你的代码是这一行出错了吧,
十载的芜湖网站建设经验,针对设计、前端、开发、售后、文案、推广等六对一服务,响应快,48小时及时工作处理。成都全网营销推广的优势是能够根据用户设备显示端的尺寸不同,自动调整芜湖建站的显示方式,使网站能够适用不同显示终端,在浏览器中调整网站的宽度,无论在任何一种浏览器上浏览网站,都能展现优雅布局与设计,从而大程度地提升浏览体验。创新互联从事“芜湖网站设计”,“芜湖网站推广”以来,每个客户项目都认真落实执行。
dbConn = DriverManager.getConnection(dbURL, userName, userPwd);
我估计是你的dbURL写错了··
我附送一个sqlserver2005连接数据库的帮助类给你··
像这种连接 做成单例模式要比普通模式好很多···
import java.sql.Connection;
import java.sql.DriverManager;
public class DBHelp {
private final static String JDBCURL="jdbc:sqlserver://localhost:1433;databasename=test";
private final static String USER="sa";
private final static String PWD="";
static{
try {
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
}
public static Connection getCon()throws Exception{
Connection con=DriverManager.getConnection(JDBCURL,USER,PWD);
return con;
}
}
数据库操作帮助类包括sqlserver、oracle、mysql、access、sqlite,同时包括通用的分页函数,
用sqlserver
在config文件中添加cnnstr也就是连接字符串
添加一个数据库帮助类
打开数据库连接,这里使用sqlcommand方式
然后如果是远程的数据库只要修改config文件中的服务器地址就行了
前一段时间在博客园中找到的一个工厂模式的数据库帮助类,具体是出自哪一位高人记不清楚了,现在Mark一下.
1、数据库类型枚举
复制代码
public enum DbProviderType : byte
{
SqlServer,
MySql,
SQLite,
Oracle,
ODBC,
OleDb,
Firebird,
PostgreSql,
DB2,
Informix,
SqlServerCe
}
复制代码
2、DbProviderFactory工厂类
复制代码
public class ProviderFactory
{
private static DictionaryDbProviderType, string providerInvariantNames = new DictionaryDbProviderType, string();
private static DictionaryDbProviderType, DbProviderFactory providerFactoies = new DictionaryDbProviderType, DbProviderFactory(20);
static ProviderFactory()
{
providerInvariantNames.Add(DbProviderType.SqlServer, "System.Data.SqlClient");
providerInvariantNames.Add(DbProviderType.OleDb, "System.Data.OleDb");
providerInvariantNames.Add(DbProviderType.ODBC, "System.Data.ODBC");
providerInvariantNames.Add(DbProviderType.Oracle, "Oracle.DataAccess.Client");
providerInvariantNames.Add(DbProviderType.MySql, "MySql.Data.MySqlClient");
providerInvariantNames.Add(DbProviderType.SQLite, "System.Data.SQLite");
providerInvariantNames.Add(DbProviderType.Firebird, "FirebirdSql.Data.Firebird");
providerInvariantNames.Add(DbProviderType.PostgreSql, "Npgsql");
providerInvariantNames.Add(DbProviderType.DB2, "IBM.Data.DB2.iSeries");
providerInvariantNames.Add(DbProviderType.Informix, "IBM.Data.Informix");
providerInvariantNames.Add(DbProviderType.SqlServerCe, "System.Data.SqlServerCe");
}
public static string GetProviderInvariantName(DbProviderType providerType)
{
return providerInvariantNames[providerType];
}
public static DbProviderFactory GetDbProviderFactory(DbProviderType providerType)
{
if (!providerFactoies.ContainsKey(providerType))
{
providerFactoies.Add(providerType, ImportDbProviderFactory(providerType));
}
return providerFactoies[providerType];
}
private static DbProviderFactory ImportDbProviderFactory(DbProviderType providerType)
{
string providerName = providerInvariantNames[providerType];
DbProviderFactory factory = null;
try
{
factory = DbProviderFactories.GetFactory(providerName);
}
catch (ArgumentException e)
{
factory = null;
}
return factory;
}
}
复制代码
3、数据库操作帮助类
复制代码
public sealed class DbHelper
{
public string ConnectionString { get; set; }
private DbProviderFactory providerFactory;
public DbHelper(string connectionString, DbProviderType providerType)
{
ConnectionString = connectionString;
providerFactory = ProviderFactory.GetDbProviderFactory(providerType);
if (providerFactory == null)
{
throw new ArgumentException("Can't load DbProviderFactory for given value of providerType");
}
}
********** 具体的ADO.NET操作方法略************
public int ExecuteNonQuery(string sql, IListDbParameter parameters)
public DbDataReader ExecuteReader(string sql, IListDbParameter parameters)
}
复制代码
4、数据库配置文件
复制代码
connectionStrings
add name="DARJUAN_DB" connectionString="data source=DARJUAN-PC;database=DARJUAN_DB;user id=***;password=***;connection reset=false;Timeout=120;connection lifetime=120; min pool size=20; max pool size=50" providerName="System.Data.SqlClient" /
/connectionStrings
复制代码
system.data
DbProviderFactories
add name="Odbc Data Provider" invariant="System.Data.Odbc" description=".Net Framework Data Provider for Odbc" type="System.Data.Odbc.OdbcFactory, System.Data, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" /
add name="OleDb Data Provider" invariant="System.Data.OleDb" description=".Net Framework Data Provider for OleDb" type="System.Data.OleDb.OleDbFactory, System.Data, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" /
add name="OracleClient Data Provider" invariant="System.Data.OracleClient" description=".Net Framework Data Provider for Oracle" type="System.Data.OracleClient.OracleClientFactory, System.Data.OracleClient, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" /
add name="SqlClient Data Provider" invariant="System.Data.SqlClient" description=".Net Framework Data Provider for SqlServer" type="System.Data.SqlClient.SqlClientFactory, System.Data, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" /
add name="Microsoft SQL Server Compact Data Provider" invariant="System.Data.SqlServerCe.4.0" description=".NET Framework Data Provider for Microsoft SQL Server Compact" type="System.Data.SqlServerCe.SqlCeProviderFactory, System.Data.SqlServerCe, Version=4.0.0.0, Culture=neutral, PublicKeyToken=89845dcd8080cc91" /
add name="SQLite Data Provider" invariant="System.Data.SQLite" description=".Net Framework Data Provider for SQLite" type="System.Data.SQLite.SQLiteFactory, System.Data.SQLite" /
add name="Informix Data Provider" invariant="IBM.Data.Informix" description=".Net Framework Data Provider for Informix" type="IBM.Data.Informix.IfxFactory, IBM.Data.Informix" /
add name="DB2 Data Provider" invariant="IBM.Data.DB2.iSeries" description=".Net Framework Data Provider for DB2 iSeries" type="IBM.Data.DB2.iSeries.DB2Factory, IBM.Data.DB2.iSeries" /
add name="Firebird Data Provider" invariant="FirebirdSql.Data.FirebirdClient" description="Firebird" type="FirebirdSql.Data.FirebirdClient.FirebirdClientFactory, FirebirdSql.Data.FirebirdClient" /
add name="Oracle Data Provider" invariant="Oracle.DataAccess.Client" description=".Net Framework Data Provider for Oracle" type="Oracle.DataAccess.Client.OracleClientFactory, Oracle.DataAccess" /
add name="PostgreSQL Data Provider Data Provider" invariant="Npgsql" description=".Net Framework Data Provider for PostgreSQL" type="Npgsql.NpgsqlFactory, System.Data" /
/DbProviderFactories
/system.data