本文中将列举三种不同的连接写法,但是本质上都为一种。
一、准备工作
1.1 找到JAR包
地址为数据库的安装路径,我的在E盘,以下为我的路径仅供参考。
1.2 给项目添加jar包
右击项目->properites->在列表中找到 Java Build Path->点击Libraties-> Add External JARs..->
二、开始操作
1、在一个文件中实现
test_jdbc.java
package test_wms;
import java.sql.Connection;
import java.sql.Date;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
public class test_jdbc {
//oracle的驱动
static final String JDBC_DRIVER = "oracle.jdbc.driver.OracleDriver";
//连接数据库的路径 如果是本机@localhost
static final String url = "jdbc:oracle:thin:@172.17.1.189:1522:mests";
static final String user = "root";
static final String password = "123456";
public static void main(String[] args) {
Connection conn = null;
Statement stmt = null;
try {
//建立驱动
Class.forName(JDBC_DRIVER);
System.out.println("打开数据库中..");
//进行连接
conn = DriverManager.getConnection(url, user, password);
System.out.println("实例化statement..");
stmt = conn.createStatement();
String sql;
//编写sql
sql = "select goods_barcode,goods_sku,create_date from test_goods_info";
ResultSet rs = stmt.executeQuery(sql);
while (rs.next()) {
String bar = rs.getString("goods_barcode");
String sku = rs.getString("goods_sku");
Date credate = rs.getDate("create_date");
System.out.println("条码:" + bar + "," + "sku:" + sku + "," + "创建时间:" + credate);
}
rs.close();
stmt.close();
conn.close();
} catch (SQLException se) {
se.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
} finally {
// 再次关闭
try {
if (stmt != null)
stmt.close();
} catch (SQLException se2) {
}
try {
if (conn != null)
conn.close();
} catch (SQLException se) {
se.printStackTrace();
}
}
System.out.println("執行結束!");
}
}
2、第二个为进阶版,把连接抛给Conn的文件处理。
Conn.java
package test_wms;
import java.io.FileInputStream;
import java.io.InputStream;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.Properties;
public class Conn {
static final String driverClass = "oracle.jdbc.driver.OracleDriver";
static final String url="jdbc:oracle:thin:@172.17.1.189:1522:mests";
static final String username="root";
static final String password="123456";
public static Connection getconn() {
Connection conn=null;
try {
Class.forName(driverClass);
conn=DriverManager.getConnection(url,username,password);
System.out.println("連接成功!");
} catch (SQLException se) {
System.out.println("conn的數據庫配置有問題");
se.printStackTrace();
}catch (Exception e) {
e.printStackTrace();
}
return conn;
}
}
test_select.java
package test_wms;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
public class test_select {
public static void main(String[] args) {
Connection conn=null;
Statement st=null;
ResultSet rs=null;
try {
conn=Conn.getconn();
st=conn.createStatement();
String sql;
sql="select goods_barcode,goods_sku from test_goods_info";
rs=st.executeQuery(sql);
while(rs.next()) {
String bar=rs.getString("goods_barcode");
String sku=rs.getString("goods_sku");
System.out.println("條碼:"+bar+","+"SKU:"+sku);
}
rs.close();
st.close();
conn.close();
} catch (SQLException se) {
System.out.println("獲取的數據庫配置有問題");
se.printStackTrace();
}catch (Exception e) {
e.printStackTrace();
}finally {
// 再次關閉
try {
if (st != null)
st.close();
} catch (SQLException se2) {
}
try {
if (conn != null)
conn.close();
} catch (SQLException se) {
se.printStackTrace();
}
}
}}
3、这是作为真正项目中会用到的方式->将配置存入.properites文件中
3.1 创建一个config文件放入database.properties
**注意:
- 文件存放的路径关系到DbUtil.java->FileInputStream("config/database.properties");这里的路径。如果错误会报错系统找不到该文件。
- database.properties中的键值对一定要和DbUtil.java中获取的一样,否者空指针异常。
database.properties
database=ORACLE
driverClass = oracle.jdbc.driver.OracleDriver
timeout = 10
url = jdbc:oracle:thin:@172.17.1.189:1522:mests
username = mesap
password = mesap
DbUtil.java
package test_wms;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.Properties;
public class DbUtil {
static String driverClass = null;
static String url = null;
static String username = null;
static String password = null;
static {
//创建properties对象,获取配置信息
//路径记得配置
try {
Properties properties=new Properties();
InputStream input=new FileInputStream("config/database.properties");
properties.load(input);
driverClass=properties.getProperty("driverClass");
url=properties.getProperty("url");
username=properties.getProperty("username");
password=properties.getProperty("password");
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public static Connection getconn() {
Connection conn=null;
try {
Class.forName(driverClass);
conn=DriverManager.getConnection(url,username,password);
System.out.println("連接成功!");
} catch (SQLException se) {
System.out.println("conn的數據庫配置有問題");
se.printStackTrace();
}catch (Exception e) {
e.printStackTrace();
}
return conn;
}
}
test_select.java
package test_wms;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
public class test_select {
public static void main(String[] args) {
Connection conn=null;
Statement st=null;
ResultSet rs=null;
try {
conn=DbUtil.getconn();
st=conn.createStatement();
String sql;
sql="select goods_barcode,goods_sku from test_goods_info";
rs=st.executeQuery(sql);
while(rs.next()) {
String bar=rs.getString("goods_barcode");
String sku=rs.getString("goods_sku");
System.out.println("條碼:"+bar+","+"SKU:"+sku);
}
rs.close();
st.close();
conn.close();
} catch (SQLException se) {
System.out.println("獲取的數據庫配置有問題");
se.printStackTrace();
}catch (Exception e) {
e.printStackTrace();
}finally {
// 再次關閉
try {
if (st != null)
st.close();
} catch (SQLException se2) {
}
try {
if (conn != null)
conn.close();
} catch (SQLException se) {
se.printStackTrace();
}
}
}}
以上,完结撒花✿✿ヽ(°▽°)ノ✿