floater
Java Jedi
总版主
发贴: 3233
积分: 421
|
于 2003-02-09 01:49
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
|