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; } } }
  • 相关阅读:
    vue的动画组件(transition)
    vue组件的那些事($children,$refs,$parent)的使用
    vue怎么自定义指令??
    组件之间的通信:父子组件之间的通讯(子组件-改变父组件的值)
    vue的路由初识01
    总结css的使用技巧
    crontab 定时任务
    Linux crontab 命令详解
    python-面向对象
    /var/spool/postfix/maildrop 占用inode索引及磁盘空间解决办法
  • 原文地址:https://www.cnblogs.com/carlo/p/3333449.html
Copyright © 2011-2022 走看看