zoukankan      html  css  js  c++  java
  • 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; } } }
  • 相关阅读:
    Leetcode 611.有效三角形的个数
    Leetcode 606.根据二叉树创建字符串
    Leetcode 605.种花问题
    潘潘_策略投资:期货存在跨期套利和跨品种套利的机会
    雷公资本:我的交易系统02 --- 可预知的趋势
    西子凌波47:回答问题 2016-10-28
    西子凌波43:说几句 2016-03-07
    西子凌波39:当下状态2 (2015-11-07 22:33:22)
    西子凌波36:放假了。说一些题外话。 2015-09-04
    西子凌波35:简单的走势,还有不明白的吗? 2015-08-21
  • 原文地址:https://www.cnblogs.com/carlo/p/3333449.html
Copyright © 2011-2022 走看看