求发我邮箱 a90921@126.com
多谢了!
8 个解决方案
#1
我有。。
#2
这个要说清楚啊,可以长篇大论。
小到一个连接池,大到Hibernate那样的框架....
最最基本的,什么时候要连接数据库就什么时候打开连接,但一定要记得关掉,所以最好就是在finally块关。
至于事务,事务最最简单的理解就是:事务中的所有操作,要么全部完成,要么全部不完成。
所以,你认为对数据库的某一系列操作是一个事务的时候,就开始事务,事务结束的时候提交事务。
小到一个连接池,大到Hibernate那样的框架....
最最基本的,什么时候要连接数据库就什么时候打开连接,但一定要记得关掉,所以最好就是在finally块关。
至于事务,事务最最简单的理解就是:事务中的所有操作,要么全部完成,要么全部不完成。
所以,你认为对数据库的某一系列操作是一个事务的时候,就开始事务,事务结束的时候提交事务。
#3
主要是我感觉我老师设计的那个连接数据库的JDBC设计的不怎么样!比较麻烦!
#4
DbUtils:http://commons.apache.org/dbutils/
#5
#6
别这样咯!我英语不咋地啊!
#7
调用配置文件信息:
import java.io.IOException;
import java.io.InputStream;
import java.sql.Connection;
import java.util.Date;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Properties;
public class DBConnection {
static Connection getConnection(){
Connection conn = null;
try {
Class.forName(Load.DRIVER);
conn = DriverManager.getConnection(Load.URL, Load.USERNAME, Load.PASSWORD);
} catch (ClassNotFoundException e) {
System.out.println("数据库驱动加载异常");
} catch (SQLException e) {
System.out.println("数据库连接失败");
}
return conn;
}
final static class Load{
public static final String DRIVER;
public static final String URL;
public static final String USERNAME;
public static final String PASSWORD;
public static Properties properties;
static{
Load();
DRIVER = properties.getProperty("DRIVER");
URL = properties.getProperty("URL");
USERNAME = properties.getProperty("USERNAME");
PASSWORD = properties.getProperty("PASSWORD");
}
private static void Load() {
properties = new Properties();
InputStream in = Load.class.getResourceAsStream("/hu/asgard/property/config.properties");
try {
properties.load(in);
} catch (IOException e) {
System.out.println("获取配置文件失败");;
}
}
}
static void close(Connection conn){
if(conn!=null){
try {
conn.close();
} catch (SQLException e) {
System.out.println("数据库关闭异常");;
}
}
}
}
数据库配置文件信息:
DRIVER=oracle.jdbc.driver.OracleDriver
URL=jdbc:oracle:thin:@192.168.1.125:1521:orcl
USERNAME=scott
PASSWORD=tiger
测试连接方法:
import java.sql.Connection;
import java.sql.Date;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
public class Test {
public static void main(String[] args) {
Connection conn=null;
Statement stat=null;
ResultSet rs=null;
DBConnection dbutil = new DBConnection();
try {
conn = dbutil.getConnection();
stat = conn.createStatement();
String sql = "select * from emp";
rs = stat.executeQuery(sql);
// 查询dept表
// while(rs.next()){
// int deptno=rs.getInt(1);
// String dname=rs.getString(2);
// String loc=rs.getString(3);
// System.out.println(deptno+"\t"+dname+"\t"+loc);
// }
//查询emp表
System.out.println("empno\tename\tjob\tmgr\tdate\tsal\tcomm\tdeptno");
while(rs.next()){
int empno = rs.getInt(1);
String ename = rs.getString(2);
String job = rs.getString(3);
int mgr = rs.getInt(4);
Date date = rs.getDate(5);
int sal = rs.getInt(6);
int comm = rs.getInt(7);
int deptno = rs.getInt(8);
System.out.println(empno+" "+ename+" "+job+" "+mgr+" "+date+" "+sal+" "+comm+" "+deptno);
}
} catch (SQLException e) {
System.out.println("SQL语句执行失败");
}finally{
try {
conn.close();
stat.close();
rs.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
}
这个只是用来说明数据库连接的方式的,实际应用并不这么写
import java.io.IOException;
import java.io.InputStream;
import java.sql.Connection;
import java.util.Date;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Properties;
public class DBConnection {
static Connection getConnection(){
Connection conn = null;
try {
Class.forName(Load.DRIVER);
conn = DriverManager.getConnection(Load.URL, Load.USERNAME, Load.PASSWORD);
} catch (ClassNotFoundException e) {
System.out.println("数据库驱动加载异常");
} catch (SQLException e) {
System.out.println("数据库连接失败");
}
return conn;
}
final static class Load{
public static final String DRIVER;
public static final String URL;
public static final String USERNAME;
public static final String PASSWORD;
public static Properties properties;
static{
Load();
DRIVER = properties.getProperty("DRIVER");
URL = properties.getProperty("URL");
USERNAME = properties.getProperty("USERNAME");
PASSWORD = properties.getProperty("PASSWORD");
}
private static void Load() {
properties = new Properties();
InputStream in = Load.class.getResourceAsStream("/hu/asgard/property/config.properties");
try {
properties.load(in);
} catch (IOException e) {
System.out.println("获取配置文件失败");;
}
}
}
static void close(Connection conn){
if(conn!=null){
try {
conn.close();
} catch (SQLException e) {
System.out.println("数据库关闭异常");;
}
}
}
}
数据库配置文件信息:
DRIVER=oracle.jdbc.driver.OracleDriver
URL=jdbc:oracle:thin:@192.168.1.125:1521:orcl
USERNAME=scott
PASSWORD=tiger
测试连接方法:
import java.sql.Connection;
import java.sql.Date;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
public class Test {
public static void main(String[] args) {
Connection conn=null;
Statement stat=null;
ResultSet rs=null;
DBConnection dbutil = new DBConnection();
try {
conn = dbutil.getConnection();
stat = conn.createStatement();
String sql = "select * from emp";
rs = stat.executeQuery(sql);
// 查询dept表
// while(rs.next()){
// int deptno=rs.getInt(1);
// String dname=rs.getString(2);
// String loc=rs.getString(3);
// System.out.println(deptno+"\t"+dname+"\t"+loc);
// }
//查询emp表
System.out.println("empno\tename\tjob\tmgr\tdate\tsal\tcomm\tdeptno");
while(rs.next()){
int empno = rs.getInt(1);
String ename = rs.getString(2);
String job = rs.getString(3);
int mgr = rs.getInt(4);
Date date = rs.getDate(5);
int sal = rs.getInt(6);
int comm = rs.getInt(7);
int deptno = rs.getInt(8);
System.out.println(empno+" "+ename+" "+job+" "+mgr+" "+date+" "+sal+" "+comm+" "+deptno);
}
} catch (SQLException e) {
System.out.println("SQL语句执行失败");
}finally{
try {
conn.close();
stat.close();
rs.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
}
这个只是用来说明数据库连接的方式的,实际应用并不这么写
#8
多谢各位了!
#1
我有。。
#2
这个要说清楚啊,可以长篇大论。
小到一个连接池,大到Hibernate那样的框架....
最最基本的,什么时候要连接数据库就什么时候打开连接,但一定要记得关掉,所以最好就是在finally块关。
至于事务,事务最最简单的理解就是:事务中的所有操作,要么全部完成,要么全部不完成。
所以,你认为对数据库的某一系列操作是一个事务的时候,就开始事务,事务结束的时候提交事务。
小到一个连接池,大到Hibernate那样的框架....
最最基本的,什么时候要连接数据库就什么时候打开连接,但一定要记得关掉,所以最好就是在finally块关。
至于事务,事务最最简单的理解就是:事务中的所有操作,要么全部完成,要么全部不完成。
所以,你认为对数据库的某一系列操作是一个事务的时候,就开始事务,事务结束的时候提交事务。
#3
主要是我感觉我老师设计的那个连接数据库的JDBC设计的不怎么样!比较麻烦!
#4
DbUtils:http://commons.apache.org/dbutils/
#5
#6
别这样咯!我英语不咋地啊!
#7
调用配置文件信息:
import java.io.IOException;
import java.io.InputStream;
import java.sql.Connection;
import java.util.Date;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Properties;
public class DBConnection {
static Connection getConnection(){
Connection conn = null;
try {
Class.forName(Load.DRIVER);
conn = DriverManager.getConnection(Load.URL, Load.USERNAME, Load.PASSWORD);
} catch (ClassNotFoundException e) {
System.out.println("数据库驱动加载异常");
} catch (SQLException e) {
System.out.println("数据库连接失败");
}
return conn;
}
final static class Load{
public static final String DRIVER;
public static final String URL;
public static final String USERNAME;
public static final String PASSWORD;
public static Properties properties;
static{
Load();
DRIVER = properties.getProperty("DRIVER");
URL = properties.getProperty("URL");
USERNAME = properties.getProperty("USERNAME");
PASSWORD = properties.getProperty("PASSWORD");
}
private static void Load() {
properties = new Properties();
InputStream in = Load.class.getResourceAsStream("/hu/asgard/property/config.properties");
try {
properties.load(in);
} catch (IOException e) {
System.out.println("获取配置文件失败");;
}
}
}
static void close(Connection conn){
if(conn!=null){
try {
conn.close();
} catch (SQLException e) {
System.out.println("数据库关闭异常");;
}
}
}
}
数据库配置文件信息:
DRIVER=oracle.jdbc.driver.OracleDriver
URL=jdbc:oracle:thin:@192.168.1.125:1521:orcl
USERNAME=scott
PASSWORD=tiger
测试连接方法:
import java.sql.Connection;
import java.sql.Date;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
public class Test {
public static void main(String[] args) {
Connection conn=null;
Statement stat=null;
ResultSet rs=null;
DBConnection dbutil = new DBConnection();
try {
conn = dbutil.getConnection();
stat = conn.createStatement();
String sql = "select * from emp";
rs = stat.executeQuery(sql);
// 查询dept表
// while(rs.next()){
// int deptno=rs.getInt(1);
// String dname=rs.getString(2);
// String loc=rs.getString(3);
// System.out.println(deptno+"\t"+dname+"\t"+loc);
// }
//查询emp表
System.out.println("empno\tename\tjob\tmgr\tdate\tsal\tcomm\tdeptno");
while(rs.next()){
int empno = rs.getInt(1);
String ename = rs.getString(2);
String job = rs.getString(3);
int mgr = rs.getInt(4);
Date date = rs.getDate(5);
int sal = rs.getInt(6);
int comm = rs.getInt(7);
int deptno = rs.getInt(8);
System.out.println(empno+" "+ename+" "+job+" "+mgr+" "+date+" "+sal+" "+comm+" "+deptno);
}
} catch (SQLException e) {
System.out.println("SQL语句执行失败");
}finally{
try {
conn.close();
stat.close();
rs.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
}
这个只是用来说明数据库连接的方式的,实际应用并不这么写
import java.io.IOException;
import java.io.InputStream;
import java.sql.Connection;
import java.util.Date;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Properties;
public class DBConnection {
static Connection getConnection(){
Connection conn = null;
try {
Class.forName(Load.DRIVER);
conn = DriverManager.getConnection(Load.URL, Load.USERNAME, Load.PASSWORD);
} catch (ClassNotFoundException e) {
System.out.println("数据库驱动加载异常");
} catch (SQLException e) {
System.out.println("数据库连接失败");
}
return conn;
}
final static class Load{
public static final String DRIVER;
public static final String URL;
public static final String USERNAME;
public static final String PASSWORD;
public static Properties properties;
static{
Load();
DRIVER = properties.getProperty("DRIVER");
URL = properties.getProperty("URL");
USERNAME = properties.getProperty("USERNAME");
PASSWORD = properties.getProperty("PASSWORD");
}
private static void Load() {
properties = new Properties();
InputStream in = Load.class.getResourceAsStream("/hu/asgard/property/config.properties");
try {
properties.load(in);
} catch (IOException e) {
System.out.println("获取配置文件失败");;
}
}
}
static void close(Connection conn){
if(conn!=null){
try {
conn.close();
} catch (SQLException e) {
System.out.println("数据库关闭异常");;
}
}
}
}
数据库配置文件信息:
DRIVER=oracle.jdbc.driver.OracleDriver
URL=jdbc:oracle:thin:@192.168.1.125:1521:orcl
USERNAME=scott
PASSWORD=tiger
测试连接方法:
import java.sql.Connection;
import java.sql.Date;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
public class Test {
public static void main(String[] args) {
Connection conn=null;
Statement stat=null;
ResultSet rs=null;
DBConnection dbutil = new DBConnection();
try {
conn = dbutil.getConnection();
stat = conn.createStatement();
String sql = "select * from emp";
rs = stat.executeQuery(sql);
// 查询dept表
// while(rs.next()){
// int deptno=rs.getInt(1);
// String dname=rs.getString(2);
// String loc=rs.getString(3);
// System.out.println(deptno+"\t"+dname+"\t"+loc);
// }
//查询emp表
System.out.println("empno\tename\tjob\tmgr\tdate\tsal\tcomm\tdeptno");
while(rs.next()){
int empno = rs.getInt(1);
String ename = rs.getString(2);
String job = rs.getString(3);
int mgr = rs.getInt(4);
Date date = rs.getDate(5);
int sal = rs.getInt(6);
int comm = rs.getInt(7);
int deptno = rs.getInt(8);
System.out.println(empno+" "+ename+" "+job+" "+mgr+" "+date+" "+sal+" "+comm+" "+deptno);
}
} catch (SQLException e) {
System.out.println("SQL语句执行失败");
}finally{
try {
conn.close();
stat.close();
rs.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
}
这个只是用来说明数据库连接的方式的,实际应用并不这么写
#8
多谢各位了!