
ASP.NET程序中的web.config中的连接字符串为: <add name="Conn" connectionString="server=.uid=sapwd=seeyondatabase=Dsystem" />,name是指的在程序写好的链接数据库的方法名,connectionString中就是我们与数据库连接配置参数:server表示数据库服务器的名字和IP地址,uid是指数据库的用户名,pwd是数据库登录密码,database是指数据库的名字。
ASP.NET程序与sql server 2005数据库连接不单是需要在配置文件中配置好数据库名、用户名、密码、IP地址。还需要在程序中也要写好与数据库的连接方法,然后在web.config中来调用才能正常连接,这样程序在使用过程中才不会报与数据库连接错误。
1、ASP.NET程序与sql server 2005数据库连接方法代码:(注:与数据库连接的方法有很多,但是都是大同小异)
using Systemusing System.Collections.Generic
using System.Text
using System.Data
using System.Configuration
using System.Data.SqlClient
using System.Data.OleDb
using System.Data.Sql
namespace DLL
{
public class DBHelper
{
public static string conn = ConfigurationManager.ConnectionStrings["Conn"].ToString()
//public static string conn =ConfigurationManager.AppSettings["SqlConnString"].ToString()
static SqlConnection con = null
/// <summary>
/// 判断数据库连接状态
/// </summary>
/// <returns></returns>
public static SqlConnection getConnection()
{
try
{
if (con == null)
{
con = new SqlConnection(conn)
}
if (con.State == ConnectionState.Broken)
{
con.Close()
con.Open()
}
if (con.State == ConnectionState.Closed)
{
con.Open()
}
}
catch (Exception ex)
{
throw ex
}
return con
}
public static SqlCommand NewMethod(string sql, SqlParameter[] par)
{
SqlCommand com = new SqlCommand(sql, getConnection())
if (par != null)
{
foreach (SqlParameter parameter in par)
{
if (parameter.Value == null)
{
parameter.Value = DBNull.Value
}
com.Parameters.Add(parameter)
}
}
return com
}
public static DataSet Dataset(string sql, SqlParameter[] par, string tableName)
{
DataSet set = new DataSet()
SqlCommand comm = NewMethod(sql,par)
if(tableName==null || tableName=="")
{
tableName = "tableName"
}
SqlDataAdapter dapter = new SqlDataAdapter(sql, getConnection())
dapter.Fill(set,tableName)
return set
}
public static DataTable Table(string sql, SqlParameter[] par, string tableName)
{
DataTable table = new DataTable()
try
{
table = Dataset(sql, par, tableName).Tables[0]
return table
}
catch (Exception ex)
{
throw ex
}
}
public static SqlDataReader Reader(string sql,SqlParameter[] par)
{
SqlDataReader red = null
SqlCommand comm = NewMethod(sql,par)
try
{
red = comm.ExecuteReader()
}
catch (Exception ex)
{
red.Close()
con.Close()
throw ex
}
return red
}
public static int Execut(string sql, SqlParameter[] par)
{
int num = 0
SqlCommand com = NewMethod(sql, par)
try
{
num = com.ExecuteNonQuery()
}
catch (Exception ex)
{
num = 0
}
con.Close()
return num
}
}
}
2、web.config配置文件的连接代码为:
<?xml version="1.0" encoding="UTF-8"?><!--
注意: 除了手动编辑此文件外,您还可以使用
Web 管理工具来配置应用程序的设置。可以使用 Visual Studio 中的
“网站”->“Asp.Net 配置”选项。
设置和注释的完整列表可以在
machine.config.comments 中找到,该文件通常位于
\Windows\Microsoft.Net\Framework\vx.x\Config 中
-->
<configuration>
<appSettings>
</appSettings>
<!-- 数据库连接字符串-->
<connectionStrings>
<add name="Conn" connectionString="server=.uid=sapwd=seeyondatabase=Dsystem" />
</connectionStrings>
<system.web>
<!--
设置 compilation debug="true" 可将调试符号
插入已编译的页面中。
但由于这会影响性能,因此请仅在开发过程中将此值
设置为 true。
-->
<compilation debug="true">
</compilation>
<!--
通过 <authentication> 节可以配置
安全身份验证模式,ASP.NET
使用该模式来识别来访用户身份。
-->
<authentication mode="Windows" />
<!--
如果在执行请求的过程中出现未处理的错误,
则通过 <customErrors> 节
可以配置相应的处理步骤。具体而言,
开发人员通过该节可配置要显示的 html 错误页,
以代替错误堆栈跟踪。
<customErrors mode="RemoteOnly" defaultRedirect="GenericErrorPage.htm">
<error statusCode="403" redirect="NoAccess.htm" />
<error statusCode="404" redirect="FileNotFound.htm" />
</customErrors>
-->
</system.web>
<system.webServer>
<defaultDocument>
<files>
<add value="Login.aspx" />
</files>
</defaultDocument>
</system.webServer>
</configuration>
1.首先要移动mysql-connector-java-5.1.44-bin.jar到tomactde的lib目录下(我的目录是这样:F:\tomcat\apache-tomcat-7.0.63\lib)这是一个连接数据库要用到包,一般在下载mysql的时候选择配置会下载,然后移动到tomact的lib下;
2.在你要连接数据库的项目中新建一个jsp文件,将下列代码复制进去;
<%@ page contentType="text/htmlcharset=UTF-8" language="java" %> <%@ page import="com.mysql.jdbc.Driver"%><%@ page import="java.sql.*" %> //使用DriverManager获取数据库连接,其中返回的Connection就代表了Java程序和数据库的连接 <html><head> <title>MySQL connect test</title></head><body><% String driverName = "com.mysql.jdbc.Driver" String userName = "root" //你的数据库用户名 String passWorld = "your_password"//你的数据库密码 String dbName = "test" //数据库名称 String tableName = "abc" //表的名称 String url = "jdbc:mysql://localhost/"+dbName+"?user="+userName+"&password="+passWorld try { Class.forName("com.mysql.jdbc.Driver").newInstance() Connection connection = DriverManager.getConnection(url) Statement statement = connection.createStatement() String sql = "SELECT * FROM "+tableName ResultSet result = statement.executeQuery(sql) ResultSetMetaData rmate = result.getMetaData() int numCount = rmate.getColumnCount() while ( result.next() ) { out.print(result.getInt(2)) out.print(result.getString(1))// out.print(result.getInt(3)) out.print("<br>") } result.close() statement.close() connection.close() } catch (Exception e) { e.getMessage() }%></body></html>
3.然后运行该代码就可以在页面看见你的数据了。在这里同时提供一个可以在IDEA快速查看数据库的方法;
4.点击IDEA右侧的DataBase,进入如下页面,点击要查看的数据库类型,我是MySQL;
5. 然后进入如下界面,输入数据库名称,账号,密码,然后先测试一下连接,测试通过后,就可以点击OK;
6.然后就可以查看你的数据信息啦。
拓展资料:
Java Web,是用Java技术来解决相关web互联网领域的技术总和。web包括:web服务器和web客户端两部分。Java在客户端的应用有java applet,不过使用得很少,Java在服务器端的应用非常的丰富,比如Servlet,JSP和第三方框架等等。Java技术对Web领域的发展注入了强大的动力。
Java的Web框架虽然各不相同,但基本也都是遵循特定的路数的:使用Servlet或者Filter拦截请求,使用MVC的思想设计架构,使用约定,XML或 Annotation实现配置,运用Java面向对象的特点,面向对象实现请求和响应的流程,支持Jsp,Freemarker,Velocity等视图。
// 之所以要使用下面这条语句,是因为要使用MySQL的驱动,所以我们要把它驱动起来,// 可以通过Class.forName把它加载进去,也可以通过初始化来驱动起来,下面三种形式都可以
Class.forName("com.mysql.jdbc.Driver")// 动态加载mysql驱动
// or:
// com.mysql.jdbc.Driver driver = new com.mysql.jdbc.Driver()
// or:
// new com.mysql.jdbc.Driver()
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)