JDBC basic knowledge
来源:互联网 发布:戈尔鲍迪 知乎 编辑:程序博客网 时间:2024/05/23 11:21
1. What is JDBC? How do you connect to a database?
JDBC stands for Java Database Connectivity. It is an API which provides easy connection to a wide range of databases. To connect to a database we need to load the appropriate driver and then request for a connection object. The Class.forName(….) will load the driver and register it with the DriverManager
Class.forName(“oracle.jdbc.driver.OracleDriver”); //dynamic class loading String url = jdbc:oracle:thin:@hostname:1526:myDB; Connection myConnection = DriverManager.getConnection(url, “username”, “password”); |
The DataSource interface provides an alternative to the DriverManager for making a connection. DataSource makes the code more portable than DriverManager because it works with JNDI. This makes your application code easier to maintain. DataSource allows the use of connection pooling and support for distributed transactions. A DataSource is not only a database but also can be a file or a spreadsheet.
DataSource configuration properties are shown below:
JNDI Name: jdbc/myDataSource URL: jdbc:oracle:thin:@hostname:1526:myDB UserName, Password Implementation classname : oracle.jdbc.pool.OracleConnectionPoolDataSource Classpath: ora_jdbc.jar Connection pooling settings like: minimum pool size, maximum pool size, connection timeout, statement cache size |
Once the DataSource has been set up, then you can get the connection object as follows:
Context ctx = new InitialContext(); DataSource ds = (DataSource)ctx.lookup("jdbc/myDataSource"); Connection myConnection = ds.getConnection(“username”,”password”); |
2. Why should you prefer using DataSource?
Best practice: In a basic implementation a Connection obtained from a DataSource and a DriverManager are identical. But the J2EE best practice is to use DataSource because of its portability, better performance due to pooling of valuable resources and the J2EE standard requires that applications use the container’s resource management facilities to obtain connections to resources. Every major web application container provides pooled database connection management as part of its resource management framework.
Design Pattern: JDBC architecture decouples an abstraction from its implementation so that the implementation can vary independent of the abstraction. This is an example of the bridge design pattern. The JDBC API provides the abstraction and the JDBC drivers provide the implementation. New drivers can be plugged-in to the JDBC API without changing the client code.
3.What are JDBC Statements? What are different types of statements? How can you create them?
A statement object is responsible for sending the SQL statements to the Database. Statement objects are created from the connection object and then executed.
Statement stmt = myConnection.createStatement();
// to read
ResultSet rs = stmt.executeQuery(“SELECT id, name FROM myTable where id =1245”); or
//to insert/update/delete/create
stmt.executeUpdate(“INSERT INTO (field1,field2) values (1,3)”);
The types of statements are:
s Statement (regular statement as shown above)
s PreparedStatement (more efficient than statement due to pre-compilation of SQL)
s CallableStatement (to call stored procedures on the database)
To use prepared statement:
PreparedStatement prepStmt =
myConnection.prepareStatement("SELECT id, name FROM myTable where id = ? ");
prepStmt.setInt(1, 1245);
Callable statements are used for calling stored procedures.
CallableStatement calStmt = myConnection.prepareCall("{call PROC_SHOWMYBOOKS}");
ResultSet rs = cs.executeQuery();
- JDBC basic knowledge
- Basic Knowledge
- Basic knowledge
- Basic knowledge
- Basic knowledge
- Basic Knowledge
- Thread Basic Knowledge
- Basic good knowledge
- Database-basic knowledge(Oracle)
- json Basic knowledge
- root/user basic knowledge...
- MySQL Basic Knowledge
- Basic knowledge about XML
- iOS Basic knowledge
- Web services basic knowledge
- some basic knowledge
- java web basic knowledge
- Basic knowledge of HTML
- PetShop数据库解读
- 大连等八城市签署信息产业区域合作协议
- Myspace中国大股东证实:罗川仍在"全职工作"
- 《新闻周刊》:为什么杨致远还在领导雅虎?
- 9月十大畅销软件排行 杀毒软件独占六席
- JDBC basic knowledge
- 10家央企信息化建设居国内或世界先进水平
- 微软的黑屏是个精心策划的广告
- 使用聚集索引
- eBay获10亿循环信贷额度 用于收购等开支
- 敏捷方法大全
- 张亚勤:金融危机对软件外包的意义
- 用VC解压缩文件(WinRAR)
- C# 代理工厂(1)