• 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; } } }
  • 相关阅读:
    scrapy爬虫框架学习
    python:面向对象—— __slots__来限制实例的属性命名范围
    列表list 的常用操作
    python replace()用法
    python 字符串 空字符串 len()
    python 字符串 编码问题
    python中语句、函数、类、模块、包之间的关系
    matplotlib 绘制正玄余玄曲线
    Es6/CommonJs/AMD模块系统的差异以及用法
    BOM和DOM的区别
  • 原文地址:https://www.cnblogs.com/carlo/p/3333449.html
Copyright © 2020-2023  润新知