public Connection getNewConnection(){
Connection con=null;
try {
Class.forName("com.mysql.jdbc.Driver");
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
⑵ java web 与sql 2008相连接做个项目但知道java web连接数据库的语句该如何写
java 文件
//取得数据库连接资源文件
private static Properties getProp() throws Exception {
Properties p = new Properties();
FileInputStream is = null;
try {
is = new FileInputStream(".\\conf\\connect.prop");
p.load(is);
} finally {
if (is != null)
is.close();
}
return p;
}
//取得源数据库连接
private static Connection getConnection1(Properties p) throws Exception {
String sURL = (String) p.get("db1.URL");
String sUser = (String) p.get("db1.user");
String sPasswd = (String) p.get("db1.password");
Class.forName("net.sourceforge.jtds.jdbc.Driver"); return DriverManager.getConnection(sURL, sUser, sPasswd);
}
conf.properties:
#源数据库连接
db1.URL=jdbc:jtds:sqlserver://127.0.0.1:1433/stu
db1.user=sa
db1.password=123
⑶ ASP.NET中WEB.config连接网络服务器SQL 2005数据库,代码怎么写
ASP.NET程序中的web.config中的连接字符串为:<add name="Conn" connectionString="server=.;uid=sa;pwd=seeyon;database=Dsystem;" />,name是指的在程序写好的链接数据库的方法名,connectionString中就是我们与数据库连接配置参数:server表示数据库服务器的名字和IP地址,uid是指数据库的用户名,pwd是数据库登录密码,database是指数据库的名字。
ASP.NET程序与sql server 2005数据库连接不单是需要在配置文件中配置好数据库名、用户名、密码、IP地址。还需要在程序中也要写好与数据库的连接方法,然后在web.config中来调用才能正常连接,这样程序在使用过程中才不会报与数据库连接错误。
1、ASP.NET程序与sql server 2005数据库连接方法代码:(注:与数据库连接的方法有很多,但是都是大同小异)
usingSystem;
usingSystem.Collections.Generic;
usingSystem.Text;
usingSystem.Data;
usingSystem.Configuration;
usingSystem.Data.SqlClient;
usingSystem.Data.OleDb;
usingSystem.Data.Sql;
namespaceDLL
{
publicclassDBHelper
{
publicstaticstringconn=ConfigurationManager.ConnectionStrings["Conn"].ToString();
//publicstaticstringconn=ConfigurationManager.AppSettings["SqlConnString"].ToString();
staticSqlConnectioncon=null;
///<summary>
///判断数据库连接状态
///</summary>
///<returns></returns>
()
{
try
{
if(con==null)
{
con=newSqlConnection(conn);
}
if(con.State==ConnectionState.Broken)
{
con.Close();
con.Open();
}
if(con.State==ConnectionState.Closed)
{
con.Open();
}
}
catch(Exceptionex)
{
throwex;
}
returncon;
}
(stringsql,SqlParameter[]par)
{
SqlCommandcom=newSqlCommand(sql,getConnection());
if(par!=null)
{
foreach(SqlParameterparameterinpar)
{
if(parameter.Value==null)
{
parameter.Value=DBNull.Value;
}
com.Parameters.Add(parameter);
}
}
returncom;
}
publicstaticDataSetDataset(stringsql,SqlParameter[]par,stringtableName)
{
DataSetset=newDataSet();
SqlCommandcomm=NewMethod(sql,par);
if(tableName==null||tableName=="")
{
tableName="tableName";
}
SqlDataAdapterdapter=newSqlDataAdapter(sql,getConnection());
dapter.Fill(set,tableName);
returnset;
}
publicstaticDataTableTable(stringsql,SqlParameter[]par,stringtableName)
{
DataTabletable=newDataTable();
try
{
table=Dataset(sql,par,tableName).Tables[0];
returntable;
}
catch(Exceptionex)
{
throwex;
}
}
(stringsql,SqlParameter[]par)
{
SqlDataReaderred=null;
SqlCommandcomm=NewMethod(sql,par);
try
{
red=comm.ExecuteReader();
}
catch(Exceptionex)
{
red.Close();
con.Close();
throwex;
}
returnred;
}
publicstaticintExecut(stringsql,SqlParameter[]par)
{
intnum=0;
SqlCommandcom=NewMethod(sql,par);
try
{
num=com.ExecuteNonQuery();
}
catch(Exceptionex)
{
num=0;
}
con.Close();
returnnum;
}
}
}
2、web.config配置文件的连接代码为:
<?xmlversion="1.0"encoding="UTF-8"?>
<!--
注意:除了手动编辑此文件外,您还可以使用
Web管理工具来配置应用程序的设置。可以使用VisualStudio中的
“网站”->“Asp.Net配置”选项。
设置和注释的完整列表可以在
machine.config.comments中找到,该文件通常位于
WindowsMicrosoft.NetFrameworkvx.xConfig中
-->
<configuration>
<appSettings>
</appSettings>
<!--数据库连接字符串-->
<connectionStrings>
<addname="Conn"connectionString="server=.;uid=sa;pwd=seeyon;database=Dsystem;"/>
</connectionStrings>
<system.web>
<!--
设置compilationdebug="true"可将调试符号
插入已编译的页面中。
但由于这会影响性能,因此请仅在开发过程中将此值
设置为true。
-->
<compilationdebug="true">
</compilation>
<!--
通过<authentication>节可以配置
安全身份验证模式,ASP.NET
使用该模式来识别来访用户身份。
-->
<authenticationmode="Windows"/>
<!--
如果在执行请求的过程中出现未处理的错误,
则通过<customErrors>节
可以配置相应的处理步骤。具体而言,
开发人员通过该节可配置要显示的html错误页,
以代替错误堆栈跟踪。
<customErrorsmode="RemoteOnly"defaultRedirect="GenericErrorPage.htm">
<errorstatusCode="403"redirect="NoAccess.htm"/>
<errorstatusCode="404"redirect="FileNotFound.htm"/>
</customErrors>
-->
</system.web>
<system.webServer>
<defaultDocument>
<files>
<addvalue="Login.aspx"/>
</files>
</defaultDocument>
</system.webServer>
</configuration>
⑷ 在vs2008 Web.config 连接 SqlServer2005,代码如何写
一般使用这个数据库连接字符可以通用"server=.;database=run2005;uid=sa;pwd=;"其中server是服务器名database是数据库名uid是登录数据库的名字pwd是登录密码
⑸ C# web Web.config里sql连接数据库
访问数据库有两种方式:
Windows身份验证登录(不需要用户名和密码,适用于访问本地数据库,应用程序和SQL软件安装在同一台服务器)
SQL Server 身份验证登录(适用于访问其他计算机的数据库,当然也可以用这种方式访问本地数据库)
根据楼主的情况应该用 SQL Server 身份验证登录
数据库连接字符串一般写在 connectionStrings节点下,非强制性(如写在appsettings节点下),只不过这样可以用
ConfigurationManager.ConnectionStrings["connString"].ConnectionString;
方便访问
以下是笔者的一个 web.config 文件的内容
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<connectionStrings>
<add name="connString" connectionString="Data Source=.;Initial Catalog=TestDatabase;Integrated Security=SSPI"/>
</connectionStrings>
</configuration>
附:数据库连接字符串的写法
SQL Server connection strings
SQL ODBC connection strings
Standard Security:< br> "Driver={SQLServer};Server=Your_Server_Name;Database=Your_Database_Name;Uid=Your_Username;Pwd=Your_Password;"
Trusted connection:< br> "Driver={SQLServer};Server=Your_Server_Name;Database=Your_Database_Name;Trusted_Connection=yes;"
SQL OLE DB connection strings
Standard Security:
"Provider=SQLOLEDB;Data Source=Your_Server_Name;Initial Catalog= Your_Database_Name;UserId=Your_Username;Password=Your_Password;"
Trusted connection:
"Provider=SQLOLEDB;Data Source=Your_Server_Name;Initial Catalog=Your_Database_Name;Integrated Security=SSPI;"
SQL OleDbConnection .NET strings
Standard Security:
"Provider=SQLOLEDB;Data Source=Your_Server_Name;Initial Catalog= Your_Database_Name;UserId=Your_Username;Password=Your_Password;"
Trusted connection:
"Provider=SQLOLEDB;Data Source=Your_Server_Name;Initial Catalog=Your_Database_Name;Integrated Security=SSPI;"
SQL SqlConnection .NET strings
Standard Security:
1. "Data Source=Your_Server_Name;Initial Catalog= Your_Database_Name;UserId=Your_Username;Password=Your_Password;" < br>2. "Server=Your_Server_Name;Database=Your_Database_Name;UserID=Your_Username;Password=Your_Password;Trusted_Connection=False"
Trusted connection:
1. "Data Source=Your_Server_Name;Initial Catalog=Your_Database_Name;Integrated Security=SSPI;"
2."Server=Your_Server_Name;Database=Your_Database_Name;Trusted_Connection=True;"
希望回答对你有帮助
⑹ java web 怎么连接sql数据库
JAVA Web开发中与数据库的连接操作,配置:
1、新建数据库。
新建登录角色,在新建数据库的时候把数据库的所有权交给你新建的角色。用用户和密码控制数据库。保证数据库的安全。
2、编写context.xml文件 Xml文件的目的是封装用户和密码,也是封装的一种,方便操作。
以下为context.xml文件样例:
<?xml version="1.0" encoding="utf-8"?>
<Context reloadable = "true">
<Resource
name="jdbc/sampleHS"
type="javax.sql.DataSource"
maxActive="14"
maxIdle="10"
username="hstaoshu"
maxWait="5000"
driverClassName="org.postgresql.Driver"
password="hstaoshu"
url="jdbc:postgresql://localhost:5432/hstaoshu"/>
</Context>
详细说明:
name="jdbc/sampleHS"里面的ssampHS是可改名称,建议根据需要自己命名;
username="hstaoshu"
password="hstaoshu"此两项为你新建的数据库登录角色用户名和密码信息,只有匹配 了才能访问。这里简单为了表示,把用户名和密码弄成了跟数据库名字一样。其实这是很不安全的。
url="jdbc:postgresql://localhost:5432/hstaoshu"/>
这是连接数据库的URl,就像访问网站的地址一样。没有这个是无法访问数据库的。localhost:5432表示本地端口。一般不需要改动,如果你在配置数据库的时候改动过端口,那么你需要把它改回来。/hstaoshu是你的数据库名称。
其他选项请勿擅自改动。
3、编写DAO类。
DAO类的作用是与数据连接后,对数据库的一些操作的封装。封装的作用。为了更好的数据管理。
DAO是真正如何使用数据库的关键步骤,前两步只是部署和配置。
private static InitialContext context = null;
private DataSource dataSource = null;
//一般把跟数据库的连接放在DAO类的构造函数里,只要被实例化,就能和数据库连接。
public BookDAO() {
try {
if (context == null) {
context = new InitialContext();
}
dataSource = (DataSource) context.lookup("java:comp/env/jdbc/sampleHS");
// 连接数据库,前面在context.xml文件配置里的URl
} catch (NamingException e2) {
e2.printStackTrace();
}
}
public Connection getConnection() {
Connection conn = null;
try {
conn = dataSource.getConnection();// 获得数据源的连接对象
} catch (SQLException e) {
e.printStackTrace();
}
return conn;
}
做完上面的三步操作,开发环境已经完全和数据库连接OK,可以开始数据库的操作了。一般来说,对数据库的操作语句都是提前封装好的。这样修改起来会对下面的代码影响降到最小。
如下:
// ------------------数据库操作语句代码封装------------------
/* 查看所有图书 */
private static final String SELECT_ALL_SQL = "SELECT * FROM book";
那么在使用的时候只要直接调用:
pstmt = conn.prepareStatement(SELECT_ALL_SQL);
⑺ 用web service方法使android连接到SQL sever的具体代码
1.可以改用SQL Server身份验证方式。在安全性-登录名中添加一个SQL Server身份验证方式登录的用户。
C#的代码里面数据库连接字符串还是粘贴属性里面的连接字符串,把密码改成自己的密码。
private String ConServerStr = "Data Source=2013-20160523DL;Initial Catalog=test;User ID=houjingyi;Password=*******";
2.一定要先在webservice里面确认对数据库的操作没有问题,再去调android程序。只看到页面出来了很可能数据库连接有问题,这样即使android程序没问题也调不出来。
3.android4.0以后不允许在主线程中访问网络,因为万一主线程阻塞了,会使得界面没有响应。我们开启线程访问即可。
[java]view plain
publicList<HashMap<String,String>>getAllInfo(finalHandlermyhandler)
{
HashMap<String,String>tempHash=newHashMap<String,String>();
List<HashMap<String,String>>list=newArrayList<HashMap<String,String>>();
tempHash.put("Cno","Cno");
tempHash.put("Cname","Cname");
tempHash.put("Cnum","Cnum");
list.clear();
arrayList1.clear();
arrayList2.clear();
arrayList3.clear();
list.add(tempHash);
newThread()
{
publicvoidrun()
{
arrayList1=Soap.GetWebServer("selectAllCargoInfor",arrayList1,arrayList2);
Messagemsg=newMessage();
msg.what=0x123;
msg.obj=arrayList1;
myhandler.sendMessage(msg);
}
}.start();
returnlist;
}
publicvoidinsertCargoInfo(StringCname,StringCnum)
{
arrayList1.clear();
arrayList2.clear();
arrayList1.add("Cname");
arrayList1.add("Cnum");
arrayList2.add(Cname);
arrayList2.add(Cnum);
newThread()
{
publicvoidrun()
{
try
{
Soap.GetWebServer("insertCargoInfo",arrayList1,arrayList2);
}
catch(Exceptione)
{
}
}
}.start();
}
publicvoiddeleteCargoInfo(StringCno)
{
arrayList1.clear();
arrayList2.clear();
arrayList1.add("Cno");
arrayList2.add(Cno);
newThread()
{
publicvoidrun()
{
try
{
Soap.GetWebServer("deleteCargoInfo",arrayList1,arrayList2);
}
catch(Exceptione)
{
}
}
}.start();
}
- 4.android4.0以后子线程里是不能对主线程的UI进行改变的,因此就引出了Handler。主线程里定义Handler供子线程里使用。
finalHandlermyhandler=newHandler()
{
publicvoidhandleMessage(Messagemsg)
{
if(msg.what==0x123)
{
ArrayList<String>drrayList=(ArrayList<String>)msg.obj;
for(intj=0;!drrayList.isEmpty()&&j+2<drrayList.size();j+=3)
{
HashMap<String,String>hashMap=newHashMap<String,String>();
hashMap.put("Cno",drrayList.get(j));
hashMap.put("Cname",drrayList.get(j+1));
hashMap.put("Cnum",drrayList.get(j+2));
list.add(hashMap);
}
adapter=newSimpleAdapter(
MainActivity.this,list,
R.layout.adapter_item,
newString[]{"Cno","Cname","Cnum"},
newint[]{R.id.txt_Cno,R.id.txt_Cname,R.id.txt_Cnum});
listView.setAdapter(adapter);
}
}
};
[java]view plain
⑻ 求在C#2005 WEB中读取SQL数据库内容的一些代码
一个例子:
//先导入命名空间
using System.Data;
using System.SqlClient;
//代码大体如此:
SqlConnection Conn = new SqlConnection("连接字符串");
SqlCommand Cmd = new SqlCommand("SQL查询语句",Conn);
Cmd.Connection.Open(); //打开数据库连接
SqlDataReader Reader = Cmd.ExecuteReader();返回 SqlDataReader 对象
while(Reader.Read()) //读取一条记录
{
//Reader["字段名"]就可以获取数据库中对应字段的值,比如: str1=Reader["Name"].ToString();
}
Reader.Close();//关闭读取
Cmd.Connection.Close();//关闭连接
⑼ ASP.NET中如何用WEB.CONFIG文件来连接SQL数据库
<connectionStrings>
<add name="xiangCtr" connectionString="Data Source=.(本机用点就行了,如果上传到空间就要写数据库IP如“124.24.122.147”);Initial Catalog=数据库名;User ID=登陆帐户;password=密码" providerName="System.Data.SqlClient"/></connectionStrings> ACCESS的数据库跟SQL的规则肯定有所不同的啊、 你只要记得每个数据库在配置文件里面的写法就行了。 其实这个很简单的。 页面调用为 SqlConnection con=System.Configuration.ConfigurationManager.ConnectionStrings["xiangCtr"].ConnectionString //这里["xiangCtr"].写你在配置文件里面定义要链接数据库的name。 然后直接用这个实例化的con就行了。
⑽ 在ASP.NET的web.config中配置好SQL的数据库连接,在代码中如何调用
name是连接字符串的名字
connectionString 是他的值。。
Data Source表示数据源,也就是sql服务的路径和名称,一般为IP地址
user id 是帐号, p.....。。
Initial Catalog表示数据库名称
Integrated Security=True表示打开数库链接时集成windows身份验证
---------
另外:
有三种方式可以修改连接字符串。我知道的。
一个是点“修改连接”再点高级 最下面的datasource就是你要的连接字符串。。。
一个是用sqldatasource会自动的写到web.config 另存为 连接字符串可以添加新的连接字符串
还有一个,一点要点完成,如果不需要哪个sqldatasource不用的话可以删掉,字符串留下来。
还有就是手工,用多了就知道了。
---------