Java,
Java,
这阵子公司不是很忙,就打算重新认识认识一下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();
}
}
}
结果:如图
完成!
相关文章
- 暂无相关文章
用户点评