步骤:加载数据库
连接数据库
对数据库进行操作
关闭数据库
//标准接口
public interface JdbcUtil {
//IO 流 把连接数据库的信息写入到文件中
// 获得连接对象的方法
public Connection getConnection();
// 释放资源的方法
public void close(Connection conn, Statement stmt);
// 释放资源的方法
public void close(Connection conn, Statement stmt, ResultSet rs);
}
//实现接口的类
public class MyJdbcUtil implements JdbcUtil{
static String url=null;
static String user=null;
static String password=null;
BufferedReader bufferedReader=null;
static Connection connection=null;
public static void getPath(){
/*try {
//E://app//javaweb//myjdbcinterface//src//implementsmyjdbc//JDBC.txt
bufferedReader=new BufferedReader(new InputStreamReader(new FileInputStream("JDBC.txt")));
url=bufferedReader.readLine();
user=bufferedReader.readLine();
password=bufferedReader.readLine();
} catch (FileNotFoundException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
try {
bufferedReader.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}*/
/* ResourceBundle rBundle=ResourceBundle.getBundle("MyJdbc");
url=rBundle.getString("url");
user=rBundle.getString("user");
password=rBundle.getString("password");*/
Properties properties=new Properties();
//java.lang.Class.getResourceAsStream(String name)查找具有给定名称的资源。
InputStream inputStream=JdbcUtil.class.getResourceAsStream("/MyJdbc.properties");
try {
// load(InputStream inStream)
// 从输入流中读取属性列表(键和元素对)。
properties.load(inputStream);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
// getProperty(String key)
// 用指定的键在此属性列表中搜索属性。
url=properties.getProperty("url");
user=properties.getProperty("user");
password=properties.getProperty("password");
}
static{
System.out.println("static静态代码块");
try {
Class.forName("com.mysql.jdbc.Driver");
getPath();
connection=DriverManager.getConnection(url,user,password);
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public Connection getConnection() {
// Connection connection=null;
// try {
// Class.forName("com.mysql.jdbc.Driver");
// getPath();
// connection=DriverManager.getConnection(url,user,password);
// } catch (ClassNotFoundException e) {
// // TODO Auto-generated catch block
// e.printStackTrace();
// } catch (SQLException e) {
// // TODO Auto-generated catch block
// e.printStackTrace();
// }
return connection;
}
@Override
public void close(Connection conn, Statement stmt) {
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();
}
}
}
@Override
public void close(Connection conn, Statement stmt, ResultSet rs) {
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();
}
}
if(rs!=null){
try {
rs.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
//测试类
package implementsmyjdbc;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import com.mysql.jdbc.Statement;
public class MyJdbcTest {
public static void main(String[] args) {
MyJdbcUtil myJdbcUtil=new MyJdbcUtil();
Connection connection=myJdbcUtil.getConnection();
String sql="select * from teacher";
java.sql.Statement statement=null;
ResultSet set=null;
try {
statement=connection.createStatement();
set=statement.executeQuery(sql);
while(set.next()){
System.out.println(set.getString("Tid")+"\t"+set.getString("Tname"));
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
注:.properties文件保存在src根目录下
若使用IO流时 ,路径直接使用文件名访问时,文件保存在项目下,与src属于同一父目录