Java开发网 Java开发网
注册 | 登录 | 帮助 | 搜索 | 排行榜 | 发帖统计  

您没有登录

» Java开发网 » Java SE 综合讨论区  

按打印兼容模式打印这个话题 打印话题    把这个话题寄给朋友 寄给朋友    该主题的所有更新都将Email到你的邮箱 订阅主题
flat modethreaded modego to previous topicgo to next topicgo to back
作者 java为什么能连接到数据库---我想知道低层的结构
panwenjie



发贴: 0
积分: 0
于 2003-02-06 15:25 user profilesend a private message to usersearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
任何语言都能连接数据库,而语言和数据库没有任何联系,数据库有他自己的引擎,

要想连接数据库肯定要遵循一些低层的东西,请问这些低层的东西是什么?
1.class.forname(".....")
connection conn=Drivermanager.getconnection(".........")
为什么执行了这两句就创建了一个到特定数据库的连接?
封装的代码是怎么写的?
2.为什么要forname("驱动程序");驱动程序的具体低层代码是怎么写的,请举一个

sql2000的列子,(不然我还是搞不懂是怎么样连接到数据库中去的),通过什么方式

,肯定不是表面上这简单的几行代码,我想知道背后的东西,请高手说明!
3.statement sta=conn.createStatement();
sta.excuteQuery("sql")为什么会执行sql语句?
封装的代码是怎么写的?
我猜测:已经创建了一个数据库的连接了,现在创建了一个查询,这好理解,关

键是执行查询,他是怎么找我要查询的表的呢?也是怎么让他识别这句sql语句的

呢?这可能就是封装的主要的代码吧。
有谁能解答以上问题那真是帮了我一个不小的忙啊,本人万分感谢!!



作者 Re:java为什么能连接到数据库---我想知道低层的结构 [Re:panwenjie]
panwenjie



发贴: 0
积分: 0
于 2003-02-08 16:40 user profilesend a private message to usersearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
请floater回答


作者 Re:java为什么能连接到数据库---我想知道低层的结构 [Re:panwenjie]
floater

Java Jedi

总版主


发贴: 3233
积分: 421
于 2003-02-09 01:49 user profilesend a private message to usersearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
panwenjie wrote:
任何语言都能连接数据库,而语言和数据库没有任何联系,数据库有他自己的引擎,

This is correct
要想连接数据库肯定要遵循一些低层的东西,请问这些低层的东西是什么?
each db has its protocol/language/interface to communicate with, e.g., sybase, sql 2k, mysql, oracle, etc. They all have their own ways to talk with. I don't know any of these ways. However, they all have other ways/protocols to talk with, odbc and jdbc. So we talk with these different dbs using odbc and jdbc.
1.class.forname(".....")
connection conn=Drivermanager.getconnection(".........")
为什么执行了这两句就创建了一个到特定数据库的连接?
This is defined in the jdbc interface/specification. Normally, you don't need to know the underlying structure/logic, unless you are a driver developer, not an application developer. As app developers, all we care is a connection to dbs so we could write sql code to it.
封装的代码是怎么写的?
Just as above you wrote
2.为什么要forname("驱动程序");驱动程序的具体低层代码是怎么写的,请举一个

sql2000的列子,(不然我还是搞不懂是怎么样连接到数据库中去的),通过什么方式

,肯定不是表面上这简单的几行代码,我想知道背后的东西,请高手说明!
All you need is just these few lines. That's the beauty of JDBC,
it encaspulates the irrelevent details so users of JDBC have less burden.
Here is an example to mysql db:


import java.sql.*;

public class MySQLTesting
{
public static void main(String[] args)
{
String url = "jdbc:MySQL://localhost:3306/mysql";
//String driver = "org.git.mm.mysql.Driver";
String driver = "com.mysql.jdbc.Driver";
String username = "webusr";
String password = "webusr";

String query = "SELECT * FROM y2k";
Connection conn = null;
Statement stmnt = null;

try
{
System.err.println("--- start connection ...");

Class.forName(driver);
System.err.println("--- jdbc driver loaded ...");

conn = DriverManager.getConnection(url, username, password);
System.err.println("--- connection established ...");

stmnt = conn.createStatement();
ResultSet result = stmnt.executeQuery(query);

System.err.println("--------------- result ---------------");
while (result.next())
{
System.err.println("result =" + result.getString(1) +
" " + result.getString(2));
}
}
catch (ClassNotFoundException e)
{
e.printStackTrace();
}
catch(SQLException ex)
{
ex.printStackTrace();
}
finally
{
try
{
if (stmnt != null) stmnt.close();
if (conn != null) conn.close();
}
catch(SQLException ex)
{
ex.printStackTrace();
}
}
}
}

In the above code, there are 2 variables, url and driver. If you use sql2000 or any other db, you need to check the jdbc docs to find out the driver.
As you see, I have a commented line there for testing purpose. For sybase, it should be com.sybase.jdbc.SybDriver or com.sybase.jdbc2.jdbc.SybDriver. You can download sql2k jdbc driver from ms site.

3.statement sta=conn.createStatement();
sta.excuteQuery("sql")为什么会执行sql语句?
封装的代码是怎么写的?
我猜测:已经创建了一个数据库的连接了,现在创建了一个查询,这好理解,关

键是执行查询,他是怎么找我要查询的表的呢?也是怎么让他识别这句sql语句的
the db is defined in the variable url at the end(mysql is the db to use). The sql statement is defined in query variable.
呢?这可能就是封装的主要的代码吧。
有谁能解答以上问题那真是帮了我一个不小的忙啊,本人万分感谢!!
Since you have access to the ftp, get the books for jdbc from there. What we discuss here is just a skeleton, you should be able to go from here to get the details in those books.



"Any fool can write code that a computer can understand. Good programmers write code that humans can understand."
- Martin Fowler, Refactoring - Improving the Design of Existing Code
作者 Re:java为什么能连接到数据库---我想知道低层的结构 [Re:panwenjie]
panwenjie



发贴: 0
积分: 0
于 2003-02-09 17:32 user profilesend a private message to usersearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
floater 你的回答很精彩,哪里有这些书下在,最好是中文的,没有的话只好是英文的了,
问你一个问题,你的大黑屏的代码是怎么弄上来的?是不是ms-doc里截的?
还有这个论坛是怎么存放你写的答复的?是不是用一个字段,这个字段的类型是什么?我很好奇!!!字段能存放这么多的内容吗?
谢谢!



作者 Re:java为什么能连接到数据库---我想知道低层的结构 [Re:panwenjie]
floater

Java Jedi

总版主


发贴: 3233
积分: 421
于 2003-02-10 01:31 user profilesend a private message to usersearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
panwenjie wrote:
floater 你的回答很精彩,哪里有这些书下在,最好是中文的,没有的话只好是英文的了,
问你一个问题,你的大黑屏的代码是怎么弄上来的?是不是ms-doc里截的?
还有这个论坛是怎么存放你写的答复的?是不是用一个字段,这个字段的类型是什么?我很好奇!!!字段能存放这么多的内容吗?
谢谢!

There are book in ftp here, don't remember chinese or english. Search for any title with JDBC or J2EE(JDBC is part of J2EE).

The black code segment came from the "code" tag. Not sure you can see this syntax:

code


As for how this bbs stores the text, ask this in the Jute board. That's the support board for Jute, home breed product from here.


floater edited on 2003-02-21 11:26

"Any fool can write code that a computer can understand. Good programmers write code that humans can understand."
- Martin Fowler, Refactoring - Improving the Design of Existing Code
作者 教你找到的方法! [Re:panwenjie]
铁针





发贴: 90
积分: 1
于 2003-02-21 09:26 user profilesend a private message to usersend email to 铁针search all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
在JDK安装路径里找那些JAR文件,请注意把它们COPY到另外路径中去玩哦,不然要是没弄好的话,JDK又要重装了。
使用解压缩工具解开看看,你就找到很多东西了!数据库连接的里面这样的话是在运行时候去创建CLASS实例对象的
forName("sun.jdbc.odbc.JdbcOdbcDriver");
按照JAVA命名空间的原则,你也应猜到了对应的CLASS文件放的路径。
有关这个类的CLASS文件是在\jre\lib\rt.jar




作者 Re:java为什么能连接到数据库---我想知道低层的结构 [Re:panwenjie]
whisperwind





发贴: 170
积分: 20
于 2003-02-21 11:06 user profilesend a private message to usersearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
jdbc驱动程序分四种类型
1。jdbc-odbc连接驱动程序:顾名思义就是通过本地odbc驱动作为桥梁来连接数据库,要实现这种驱动就要用jni来跟本地odbc驱动打交道
2。JAVA加本地代码驱动程序:将JDBC调用通过一个库来转换成数据库的API调用
3。网络JDBC驱动程序:将JDBC调用转换为一个独立于数据库的网络协议,然后通过一个中间件服务器来处理实际的数据库调用
4。专用JDBC驱动:将JDBC调用转化为数据库相关的调用
因为第四种效率最高,所以很多厂商都提供了第四种类型的驱动(TYPE 4)。所以它的实现都是数据库相关的,如果你想学习可以看看TYPE 4类型驱动的源代码(例如MYSQL JDBC驱动的源代码)



作者 Re:java为什么能连接到数据库---我想知道低层的结构 [Re:panwenjie]
huzengli



发贴: 0
积分: 0
于 2003-02-21 15:21 user profilesend a private message to usersearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
sun定义了jdbc的接口,但是接口的实现是各个数据库厂商自己完成。



flat modethreaded modego to previous topicgo to next topicgo to back
  已读帖子
  新的帖子
  被删除的帖子
Jump to the top of page

   Powered by Jute Powerful Forum® Version Jute 1.5.6 Ent
Copyright © 2002-2021 Cjsdn Team. All Righits Reserved. 闽ICP备05005120号-1
客服电话 18559299278    客服信箱 714923@qq.com    客服QQ 714923