JDBC中串連MySQL資料庫

來源:互聯網
上載者:User

標籤:

 1 package qddx.JDBC; 2 import java.sql.*; 3  4 public class JDBC_Connection { 5     static String driverName = "com.mysql.jdbc.Driver";//驅動名 6     static String url="jdbc:mysql://localhost/bbs";//地址 7     static String userName="root";//帳號 8     static String passWord = "674768166ws";//密碼 9     static {10         try{//串連驅動11         Class.forName(driverName);12         }catch(ClassNotFoundException e){13             e.printStackTrace();14         }15     }16     //串連資料庫17     public static Connection getConnection(){18         Connection conn = null;19         try{20         conn = (Connection)DriverManager.getConnection(url,userName,passWord);21         System.out.println("資料庫連接成功");22         conn.setAutoCommit(false);// 設定事務不自動認可23         }catch(SQLException e){24             e.printStackTrace();25         }26         System.out.println("已經串連");27         return conn;28     }29     //關閉資料庫30     public static void free(ResultSet rs,Connection conn,Statement stmt){31         try{32         if(rs!=null){33             rs.close();34             rs=null;35         }36         }catch(SQLException e){37             e.printStackTrace();38         }finally{39             try{40             if(conn!=null){41                 conn.close();42                 conn = null;43             }44             if(stmt !=null){45                 stmt.close();46                 stmt=null;47             }48             }catch(SQLException e){49                 e.printStackTrace();50             }51         }52     }53     public static void main(String[] args) {54         JDBC_Connection.getConnection();//啟動55     }56 57 }

 

JDBC中串連MySQL資料庫

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.