Java连接数据库需要加载对应的数据库驱动。驱动的话自己去下载,然后按如图所示添加到项目中。
oracle数据库连接:
package 数据库连接测试;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;
public class oracle测试 {
final static String DRIVER = "oracle.jdbc.OracleDriver";
final static String URL = "jdbc:oracle:thin:@localhost:1521:orcl";
final static String USERNAME = "SCOTT";
final static String PASSWORD = "tiger";
public List queryInfo() {
Connection conn = null;
Statement stmt = null;
ResultSet rs = null;
List<String> list = new ArrayList<>();
try {
// 1加载数据库驱动
Class.forName(DRIVER);
// 2获取数据库连接
conn = DriverManager.getConnection(URL, USERNAME, PASSWORD);
// 3通过连接,获取一个Statement的对象,用来操作数据库
String sql = "select * from emp";
stmt = conn.createStatement();
rs = stmt.executeQuery(sql);
while (rs.next()) {
String name = rs.getString("ename");
list.add(name);
}
} catch (SQLException | ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
if (conn != null) {
try {
conn.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if (rs != null) {
try {
rs.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if (stmt != null) {
try {
stmt.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
return list;
}
public static void main(String[] args) {
oracle测试 oo = new oracle测试();
List<String> list = oo.queryInfo();
for (String name : list) {
System.out.println(name);
}
}
}
MySQL数据库连接:
package 数据库连接测试;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;
public class mysql连接 {
final static String DRIVER = "com.mysql.jdbc.Driver";
final static String URL = "jdbc:mysql://localhost:3306/shop";
final static String user = "root";
final static String password = "";
public List queryInfo() {
List list = new ArrayList<String>();
Statement stmt = null;
ResultSet rs = null;
Connection conn = null;
try {
Class.forName(DRIVER);
conn = DriverManager.getConnection(URL, user, password);
// 判断是否成功连接到数据库
// if (!conn.isClosed())
// System.out.println("Succeeded connecting to the Database!");
stmt = conn.createStatement();
rs = stmt.executeQuery("SELECT * FROM admin");
while (rs.next()) {
String name = rs.getString("username");
list.add(name);
}
} catch (SQLException | ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally {
if(rs != null){
try {
rs.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if(conn != null){
try {
conn.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if(stmt != null){
try {
stmt.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
return list;
}
public static void main(String[] args) {
mysql连接 mm = new mysql连接();
List<String> list = new ArrayList();
list = mm.queryInfo();
for (String name : list) {
System.out.println(name);
}
}
}