JDBC 连接Oracle 数据库,JDBC 连接Mysql 数据库

首先是JDBC 连接Oracle  数据库

package com.util;
import com.pojo.UserInfo; import java.sql.*; public class DBConnection { private static Connection conn; private static String userName = "scott";
private static String passWord = "tiger";
private static String url; static {
url = "jdbc:oracle:thin:@localhost:1521:orcl";
} public static Connection getConnection() { try { Class.forName("oracle.jdbc.OracleDriver");
conn = DriverManager.getConnection(url, userName, passWord);
} catch (Exception e) {
e.printStackTrace();
}
return conn;
} //关闭资源的方法
public static void closeResource(ResultSet rs, PreparedStatement pstmt, Connection conn) { try {
if (null != rs) {
rs.close();
}
if (null != pstmt) {
pstmt.close();
}
if (null != conn) {
conn.close();
} } catch (SQLException e) {
e.printStackTrace();
System.out.println("关闭资源出现问题--");
} } public static void main(String[] args) { ResultSet resultSet = null;
PreparedStatement preparedStatement = null;
Connection conn;
DBConnection db = new DBConnection();
conn = db.getConnection();
String sql = "select * from userInfo ";
try {
preparedStatement = conn.prepareStatement(sql);
resultSet = preparedStatement.executeQuery();
while (resultSet.next()){
UserInfo u = new UserInfo();
u.setUsername(resultSet.getString("username"));
System.out.println("用户信息: 用户名为:"+ u.getUsername());
}
} catch (SQLException e) {
e.printStackTrace();
}finally {
closeResource(resultSet,preparedStatement,conn);
} }
}

然后是JDBC 连接MySql 数据库 模板

package com.util;
import com.pojo.UserInfo; import java.sql.*; public class DBConnectionMySql { private static Connection conn; private static String userName = "root";
private static String passWord = "tiger";
private static String url;
private static String driver;
static {
url = "jdbc:mysql://localhost:3306/wlc";
driver = "com.mysql.jdbc.Driver";
} public static Connection getConnection() { try { Class.forName(driver);
conn = DriverManager.getConnection(url, userName, passWord);
} catch (Exception e) {
e.printStackTrace();
}
return conn;
} //关闭资源的方法
public static void closeResource(ResultSet rs, PreparedStatement pstmt, Connection conn) { try {
if (null != rs) {
rs.close();
}
if (null != pstmt) {
pstmt.close();
}
if (null != conn) {
conn.close();
} } catch (SQLException e) {
e.printStackTrace();
System.out.println("关闭资源出现问题--");
} } public static void main(String[] args) { ResultSet resultSet = null;
PreparedStatement preparedStatement = null;
Connection conn;
DBConnectionMySql db = new DBConnectionMySql();
conn = db.getConnection();
String sql = "select * from userInfo ";
try {
preparedStatement = conn.prepareStatement(sql);
resultSet = preparedStatement.executeQuery();
while (resultSet.next()){
UserInfo u = new UserInfo();
u.setUsername(resultSet.getString("username"));
System.out.println("用户信息: 用户名为:"+ u.getUsername());
}
} catch (SQLException e) {
e.printStackTrace();
}finally {
closeResource(resultSet,preparedStatement,conn);
} }
}
上一篇:PostgreSQL stream repication can implement between FreeBSD and CentOS


下一篇:IOS中Label根据上个label的内容设置下个label的frame