---------jdbc中oracle连接如下
首先是。连接数控的类的操作
1、package db;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;
import bean.Person;
/**
*
* 创建数据库的链接
* @author Administrator
*
*/
public class Db {
private String url="JDBC:oracle:thin:@localhost:1521:HYX";
private String user="orcl";
private String password="orcl";
private Connection connection=null;
private PreparedStatement pstat=null;
private Statement stat=null;
private ResultSet rs ;
public List list;
// 构造函数
public Db() {
try {
Class.forName("oracle.jdbc.OracleDriver");
try {
connection=DriverManager.getConnection(url,user,password);
System.out.println("-------------->0k");
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
//查询的方法实现
public ResultSet find(String sql){
rs=null;
try {
list=new ArrayList();
Person person=new Person();
stat=connection.createStatement();
rs=stat.executeQuery(sql);
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return rs;
}
//增加数据的方法
public int sava(String name,String pwd) {
int result=0;
try {
String sql="insert into hyx(id,name,password) values(SEQUENCESONE.NEXTVAL,'"+name+"','"+pwd+"')";
stat=connection.createStatement();
System.out.println("------->>>"+sql);
result=stat.executeUpdate(sql);
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
close(); //关闭数据库链接
}
return result;
}
//关闭连接
public void close(){
if(null!=connection){
try {
connection.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
大家可以可以自己写一个测试类测试一把,把链接都改成你来的链接,测试下是否连的上数据库
--本人测试通过
package db;
import java.sql.ResultSet;
import java.sql.SQLException;
public class TestOracle {
/**
* @param args
*/
public static void main(String[] args) {
Db db=new Db();
// TODO Auto-generated method stub
try {
String sql="select * from hyx";
ResultSet rs= db.find(sql);
while(rs.next()){
System.out.println(rs.getString(1)+rs.getString(2)+rs.getString(3));
}
}catch(Exception e){
e.printStackTrace();
}
}
}
--------------如下是servelt的中调用数据库的操作,只看jdbc的话可以不看如下servlet的调用操作
jsp和web.xml 的配置就不赘述了
其次servlet 控制类
package testone;
import java.io.IOException;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import db.Db;
public class ReqServlet extends HttpServlet {
private String sql;
protected void doPost(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
// TODO Auto-generated method stub
Db db=new Db();
req.setCharacterEncoding("gb2312");
String username=req.getParameter("name");
String password=req.getParameter("pwd");
System.out.println(username+password);
resp.setContentType("text/html; charset=gb2312");
ServletContext sc = getServletContext();
RequestDispatcher rd = null;
if(!username.equals("")&&!password.equals("")){
int rs=db.sava(username, password);
if(rs==1){
req.setAttribute("msg", "用户名:"+username+" "+"密码:"+password);
rd=sc.getRequestDispatcher("/responseshow.jsp"); //定向页面
rd.forward(req, resp);
}else{
rd=sc.getRequestDispatcher("/error.jsp"); //定向页面
rd.forward(req, resp);
// resp.setContentType("/error.jsp");
System.out.println("————————————————》失败");
}
}
}
}