为什么80%的码农都做不了架构师?>>>
介绍实际编程中使用最普遍的一种类型:jdbc驱动,一种纯java的驱动,直接与据库相连,不通过其他任何环节。
工具/原料
-
电脑上已安装好了Oracle数据库
-
电脑上已安装好Myeclipse
方法/步骤
-
1
打开Myeclipse后点击如图右上方进入MyEclipse Database Explorer视图
-
2
在如图空白处鼠标右击后点选new进入Create a new connection driver界面
-
假如没看到就找下边的+添加
-
3
在Driver template:中下拉选中Oracle(Thin driver);
在Driver name中填写你希望的名称;
在Connection URL中将@后面的
[:<1521>]: 改成localhost:1521:orcl,这里需要注意的是database_name要改成数据库的SID,orcl为Oracle的数据库的自带实例。 在User name中填写实例中的案例,这里我填写scott;
在password中填写scott的密码,我的是123,所以就写上123;
在Driver JARs中单击右侧的add JARS添加Oracle的驱动ojdbc6.jar,这个驱动在你Oracle的安装目录下\product\11.2.0\dbhome_1\jdbc\lib中可以找到;
-
4
设置好后点击Test Driver,若出现该画面则表示你连接成功了。
-
5
点击OK,在点击Finish,你的Myeclipse就添加成功了Oracle的连接了。
END
测试连接2
-
测试代码:新建一个web项目,在项目中新建一个Servlet代码如下:
package com.city.j2ee.ch02;
import java.io.IOException;
import java.sql.*;
import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.PrintWriter;
public class ShowEmployeeList extends HttpServlet {
private Connection cn=null;
public void init(ServletConfig config) throws ServletException {
// TODO Auto-generated method stub
super.init(config);
String driver=config.getInitParameter("driver");
String url=config.getInitParameter("url");
String user=config.getInitParameter("user");
String password=config.getInitParameter("password");
try{
Class.forName(driver);
cn=DriverManager.getConnection(url,user,password);
}catch(Exception e)
{
System.out.println("Init Error:"+e.getMessage());
}
}
/**
* @see Servlet#destroy()
*/
public void destroy() {
// TODO Auto-generated method stub
super.destroy();
try{
if(cn!=null&&(!cn.isClosed()))
{
cn.close();
cn=null;
}
}catch(Exception e) {
System.out.println("Destroy Error:"+e.getMessage());
}
}
/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
response.setContentType("text/html");
response.setCharacterEncoding("GBK");
PrintWriter out =response.getWriter();
out.println("");
out.println("
A Servlet ");out.println("
");try{
String sql="select ename,job,sal,hiredate from emp";
PreparedStatement ps=cn.prepareStatement(sql);
ResultSet rs=ps.executeQuery();
out.print("
员工列表
");out.print("
");
");out.print("
"); ");out.print("
姓名 职位 工资 加入公司日期 ");out.print("
while(rs.next()){
out.print("
"); ");out.println("
"+ rs.getString("ENAME")+" ");out.println("
"+ rs.getString("JOB")+" ");out.println("
"+ rs.getString("sal")+" ");out.println("
"+ rs.getString("hiredate")+" ");out.println("
}
rs.close();
ps.close();
out.print("
}catch(Exception e) {
out.println("
处理请求发生错误:"+e.getMessage()+"
");}
out.println("");
out.println("");
out.flush();
out.close();
}
/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
doGet(request,response);
}
}
然后在Web Root下的WEB-INF下的web.xml中配置servlet,代码如下:
This is the description of my J2EE component This is the display name of my J2EE component ShowEmployeeList com.city.j2ee.ch02.ShowEmployeeList driver oracle.jdbc.driver.OracleDriver url jdbc:oracle:thin:@localhost:1521:orcl user scott password 123 -
运行效果如下: