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; } } }

 

转载于:https://my.oschina.net/u/1985061/blog/422560

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值