JDBC

自己写个jdbc工具类如下:
package ToolClass;

import java.sql.*;

public class TestJDBC
{
	Connection con = null;
	Statement state = null;
	ResultSet rs = null;

	String Url = "jdbc:oracle:thin:@127.0.0.1:1521:NIIT";
	String User = "bbs";
	String PassWord = "bbs";

	public TestJDBC()
	{
		try
		{
			Class.forName("oracle.jdbc.driver.OracleDriver");
		}
		catch (ClassNotFoundException e)
		{
			System.out.println("Could   not   load   the   driver ");
		}
	}

	public void update1(String sql)
	{
		int i = 0;
		try
		{
			con = DriverManager.getConnection(Url, User, PassWord);
			state = con.createStatement();
			i = state.executeUpdate(sql);
		}
		catch (SQLException e)
		{
			System.out.println("Connection   Fail! " + e.getMessage());
		}
		if (i > 0)
		{
			System.out.println("操作成功!");
		}
		else
		{
			System.out.println("操作失败!");
		}
	}

	public int update2(String sql)
	{
		int i = 0;
		try
		{
			con = DriverManager.getConnection(Url, User, PassWord);
			state = con.createStatement();
			i = state.executeUpdate(sql);
		}
		catch (SQLException e)
		{
			System.out.println("Connection   Fail! " + e.getMessage());
		}
		return i;
	}

	public ResultSet query(String sql)
	{
		try
		{
			con = DriverManager.getConnection(Url, User, PassWord);
			state = con.createStatement();
			rs = state.executeQuery(sql);
		}
		catch (SQLException e)
		{
			System.out.println("Connection   Fail! " + e.getMessage());

		}
		return rs;
	}

	public void close()
	{

		if (rs != null)
		{
			try
			{
				rs.close();
			}
			catch (SQLException e)
			{
				e.printStackTrace();
			}
		}
		if (state != null)
		{
			try
			{
				state.close();

			}
			catch (SQLException e)
			{
				e.printStackTrace();
			}
		}
		if (state != null)
		{
			try
			{
				con.close();
			}
			catch (SQLException e)
			{
				e.printStackTrace();
			}
		}
	}
}


测试类:
package test;

import java.io.IOException;
import java.util.Calendar;

import javax.servlet.ServletException;
import javax.servlet.http.*;

import ToolClass.TestJDBC;
public class testdate extends HttpServlet {
	public void doGet (HttpServletRequest request, HttpServletResponse response) 
							throws IOException,ServletException {
		TestJDBC jdbc = new TestJDBC ();
		Calendar obj=Calendar.getInstance();
		String a=String.valueOf(obj.get(Calendar.MONTH));
		String b=String.valueOf(obj.get(Calendar.DATE));
		String c=String.valueOf(obj.get(Calendar.YEAR));
		String datetime=c+"-"+a+"-"+b;
		System.out.println(c+"-"+a+"-"+b);
		
		String sql="INSERT INTO shijian VALUES ( 'ddd',to_date ( '"+datetime+"' , 'YYYY-MM-DD' ) )";
		
		String sql2="update  post_info set post_time=(to_date ( '2011-8-6' , 'YYYY-MM-DD' )) where post_id='2011865'";
		jdbc.update1(sql);
		System.out.println(sql);
		jdbc.close();
	}
	public void doPost (HttpServletRequest request, HttpServletResponse response) 
					throws IOException,ServletException {
		doGet (request,response);
	}
}


这样把jdbc中要用到的方法都封装到一个类中,下次再有项目时候就不用在写连接了,直接拷贝这个类就可以调用了,上面的那个类还是很简单的操作,你可以自己写个人的jdbc工具类,这应该没个程序员必须要有的工具类……

你可能感兴趣的:(jdbc)