這陣子公司不是很忙,就打算重新認識認識一下java! 以前在學校寫了一年多的j2ee(java web)有一些些java基礎 工作後寫了幾年的iOS,有些東西忘記了,但是跟着度娘走,慢慢的把java撿起來!
先寫一個數據庫連接入門一下:
1.安裝java的運行環境JDK:
下載地址:http://www.oracle.com/technetwork/java/javase/downloads/jdk8-downloads-2133151.html
2.安裝編碼工具IDE:Eclipse,
下載地址:http://www.eclipse.org/downloads/packages/eclipse-ide-java-ee-developers/keplersr2
3.配置一下classpath
以上三個步驟自行百度完成之後就可以擼起袖子寫java了
寫數據庫連接Demo的準備:
1.安裝數據庫:mysql
下載地址:(百度搜:mysql下載)
2.安裝數據庫的可視化編輯工具:Navicat
下載地址:http://www.xt70.com/soft/show/7946.html
數據庫連接的最原始的方式:
1.JDBC方式連接
package com.demo.javaBasic;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import com.mysql.jdbc.*;
public class SQLTest {
public void conn() throws SQLException {
Connection connection = null;
try {
Class.forName("com.mysql.jdbc.Driver"); // 加載MYSQL JDBC驅動程序
// Class.forName("org.gjt.mm.mysql.Driver");
System.out.println("Success loading Mysql Driver!");
Connection connect = (Connection) DriverManager.getConnection(
"jdbc:mysql://localhost:3306/test", "mysql賬號", "mysql密碼");
// 連接URL爲 jdbc:mysql//服務器地址/數據庫名 ,後面的2個參數分別是登陸用戶名和密碼
System.out.println("Success connect Mysql server!");
Statement stmt = (Statement) connect.createStatement();
ResultSet rs = stmt.executeQuery("select * from Users");
// user 爲你表的名稱
while (rs.next()) {
System.out.println(rs.getString("name"));
System.out.println(rs.getString("acount"));
}
} catch (Exception e) {
System.out.print("get data error!");
e.printStackTrace();
}finally{
if (connection != null) {
connection.close();
}
}
}
}
給公司的同事看到上面的代碼之後,被鄙視了!都什麼年代了還用最原始的JDBC來連接,數據庫的連接與關閉時開銷非常大的,需要用連接池來對接數據庫,所以百度了一下:java數據庫連接池(參考文章:http://www.cnblogs.com/onlywujun/articles/3007608.html),有好多種方案,但是 我爲了入門就簡單的寫了DBCP(database connect pool)的demo。
2.DBCP方式
首先需要三個工具包:commons-pool.jar,commons-collection.jar,commons-DBCP.jar。
dbcp依賴的jar包下載:http://download.csdn.net/detail/peng_hong_fu/9647235
package com.demo.javaBasic;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import javax.sql.DataSource;
import org.apache.commons.dbcp.BasicDataSource;
public class DBCPDemo {
private static DataSource dataSource;
public static void initDataSource(){
String driverClassName = "com.mysql.jdbc.Driver";
String url = "jdbc:mysql://localhost:3306/test";
String username = "mysql賬號";
String password = "mysql密碼";
int initialSize = 10;
int minIdle = 0;
int maxIdle = 100;
int maxWait = 0;
int maxActive = 0;
BasicDataSource bds = new BasicDataSource();
bds.setUrl(url);
bds.setDriverClassName(driverClassName);
bds.setUsername(username);
bds.setPassword(password);
bds.setInitialSize(initialSize);
bds.setMaxActive(maxActive);
bds.setMinIdle(minIdle);
bds.setMaxIdle(maxIdle);
bds.setMaxWait(maxWait);
dataSource = (DataSource) bds;
}
public static Connection getConnection() throws SQLException {
if (dataSource == null) {
initDataSource();
}
Connection conn = null;
if (dataSource != null) {
conn = ((BasicDataSource) dataSource).getConnection();
}
return conn;
}
public static void main(String[] args) throws SQLException {
Connection conn = null;
PreparedStatement pst = null;
ResultSet rs = null;
String ZLJGBM = null;
String sql = "select * from Users";
try {
conn = DBCPDemo.getConnection();
pst = (PreparedStatement) conn.prepareStatement(sql);
rs = pst.executeQuery();
while(rs.next()){
ZLJGBM = rs.getString("name");
System.out.println(ZLJGBM);
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
結果:如圖
完成!