JDBC连接MySQL数据库

5 篇文章 0 订阅
1 篇文章 0 订阅

步骤:加载数据库

   连接数据库

   对数据库进行操作

   关闭数据库


//标准接口

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属于同一父目录

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值