Linux(Ubuntu平臺) Java 通過JDBC連接MySQL數據庫,與Windows平臺類似,步驟如下:
下載 jdbc: mysql-connector-java-5.1.18.tar.gz
解壓 jdbc:tar -zxvf mysql-connector-java-5.1.18.tar.gz
配置 jdbc: cp mysql-connector-java-5.1.18-bin.jar /usr/local/jdk1.6.0_22/jre/lib/ext/
Java示例:
private static String sql="select top, id, name, country, dtime from gametop800 where id=\"com.rovio.angrybirds\"";
public mysqlConnect(){
String dbDriver = "com.mysql.jdbc.Driver";
String url = "jdbc:mysql://localhost/top800";
String username = "root";
String password = "";
Statement mStatement = null;
ResultSet mResultSet = null;
Connection mConnection = null;
String sql = "select top, id, name, country, dtime from gametop800 where top<=20";
try{
Class.forName(dbDriver).newInstance();
mConnection = DriverManager.getConnection(url, username, password);
mStatement = mConnection.createStatement();
mResultSet = mStatement.executeQuery(sql);
try{
while(mResultSet.next()){
System.out.print(mResultSet.getInt(1) + "\t");
System.out.print(mResultSet.getString(2) + "\t");
System.out.print(mResultSet.getString(3) + "\t");
System.out.println(mResultSet.getString(4));
}
}catch (Exception e){
System.out.println("數據庫讀取錯誤! \n" + e.getMessage());
}
}catch (SQLException e){
System.out.println("連接數據庫錯誤: \n" + url + "\n" + e.getMessage());
}catch (Exception e){
e.printStackTrace();
}finally{
// mStatement.close();
// mConnection.close();
}
}
執行結果:
更多文章、技術交流、商務合作、聯系博主
微信掃碼或搜索:z360901061
微信掃一掃加我為好友
QQ號聯系: 360901061
您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點擊下面給點支持吧,站長非常感激您!手機微信長按不能支付解決辦法:請將微信支付二維碼保存到相冊,切換到微信,然后點擊微信右上角掃一掃功能,選擇支付二維碼完成支付。
【本文對您有幫助就好】元

