新聞中心
這里有您想知道的互聯(lián)網(wǎng)營銷解決方案
如何連接Mysql的jdbc-創(chuàng)新互聯(lián)
這篇文章將為大家詳細講解有關如何連接Mysql的jdbc,小編覺得挺實用的,因此分享給大家做個參考,希望大家閱讀完這篇文章后可以有所收獲。
package util; import java.sql.Connection; import java.sql.DriverManager; import java.sql.PreparedStatement; import java.sql.ResultSet; public class DBUtil { Connection conn = null; PreparedStatement stmt = null; String Driver = "com..jdbc.Driver"; String url = "jdbc:mysql://10.137.5.23/mcip?useUnicode=true&characterEncoding=GBK"; String user = "mcip"; String password = "mcip"; ResultSet rs = null; //完成連接的創(chuàng)建 public Connection getConnection() throws Exception{ Class.forName(Driver); if(conn == null){ conn = DriverManager.getConnection(url, user, password); } return conn; } //創(chuàng)建語句對象 public PreparedStatement createStatement(String sql) throws Exception{ stmt = getConnection().prepareStatement(sql); return stmt; } //執(zhí)行有結果集返回的方法 public ResultSet excuteQuery() throws Exception{ rs = stmt.executeQuery(); return rs; } //執(zhí)行沒有結果集返回的方法 public int excuteUpdate() throws Exception{ return stmt.executeUpdate(); } //關閉對象 public void close(){ if(rs != null)try{rs.close();}catch(Exception e){} if(stmt != null)try{stmt.close();}catch(Exception e){} if(conn != null)try{conn.close();}catch(Exception e){} } }
關于“如何連接Mysql的jdbc”這篇文章就分享到這里了,希望以上內(nèi)容可以對大家有一定的幫助,使各位可以學到更多知識,如果覺得文章不錯,請把它分享出去讓更多的人看到。
網(wǎng)頁標題:如何連接Mysql的jdbc-創(chuàng)新互聯(lián)
本文URL:http://fisionsoft.com.cn/article/pghhp.html