Mysql访问 for橙子小海

package com.mvc.model.dao;

import com.mvc.model.daoutil.DBConn;
import com.mvc.model.entity.Blog;
 
这是连接的代码:
package com.mvc.model.daoutil ;

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 javax.naming.NamingException;

public class DBConn {
   private Connection con = null;
   private Statement stmt = null;
   public ResultSet rst = null;
   public PreparedStatement pstmt = null;
   
   public Connection getConnection() throws SQLException, NamingException
    {
      try
      {
         String Driver = "com.mysql.jdbc.Driver";
         String URL = "jdbc:mysql://localhost:3306/blogsql";
         String Username = "root";
         String Password = "root";
         Class.forName(Driver);
         con = DriverManager.getConnection(URL, Username, Password);
      }
      catch (Exception e)
      {
         System.out.println("This is error message from getConnection() ===" + e);
      }
      return con;
    }
   
   public DBConn(boolean b)
   {
      
   }
   
   public DBConn()    //���캯��
   {
      try {
         con = getConnection();
      if (con != null) stmt
= con.createStatement(); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } } public void excuteQuery(String s) { try { if(stmt != null) { rst = stmt.executeQuery(s); } } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } } public int excuteUpdate(String s) { int status = 0; try{ if(stmt != null) status = stmt.executeUpdate(s); } catch(Exception e){ e.printStackTrace(); } return status; } //============================================ public boolean next() { try{ return rst==null ? null : rst.next(); } catch(Exception e){ e.printStackTrace(); return false; } } //�ͷ��ڴ� public void close() { try{ if(rst != null) rst.close(); if(stmt != null) stmt.close(); if(con != null) con.close(); } catch(Exception e){ e.printStackTrace(); } } public static void main(String [] args) { DBConn sdb = new DBConn(); try { System.out.println(sdb.getConnection()); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (NamingException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } 下面是添加的函数: public class AddWeiboDao { //�����Լ������΢�� public boolean AddDao(Blog b) { String strSQL ="insert into news(Blog_id,Blog_context,Blog_time) " +"values('"+b.getId()+"','"+b.getContext().trim()+"','"+b.getTime()+"')"; //������ݿ����� try{ DBConn dbConn = new DBConn(); dbConn.excuteUpdate(strSQL); dbConn.close(); return true; } catch(Exception e){ e.printStackTrace(); return false; } } }

 

posted @ 2013-09-22 16:55  carlo-z  阅读(270)  评论(0编辑  收藏  举报