jdbc连接数据库之修改

 时间:2024-10-13 10:32:48

1、mysql加载数据库driver=com.mysql.jdbc.Driver数据库的链接url=jdbc:mysql://localhost:3306/demo1用户名username=root密码password=123456设置配置文件信息

jdbc连接数据库之修改

2、属性的定义public class DBUtil {private static Properties properties = new Properties();private static String driver;private static String url;private static String username;private static String password;static{

jdbc连接数据库之修改

3、// 获取配置文件中的相关参数值try {读取配置文件properties.load(DBUtil.class.getCl锾攒揉敫assLoader().getResourceAsStream("db.properties"));driver = properties.getProperty("driver"); url=properties.getProperty("url"); username=properties.getProperty("username"); password=properties.getProperty("password");

jdbc连接数据库之修改

4、 try {Class.forName(driver);} catch (ClassNotFoundException e) {// TODO Auto-generated catch blocke.printStackTrace();}} catch (IOException e) {System.out.println("加载文件失败");}}

jdbc连接数据库之修改

5、 //连接数据库的方法public static Connection getConnection() throws SQLEx艘早祓胂ception{Connection con=null;con=DriverManager.getConnection(url,username,password);return con;} //关闭数据库的方法public static void closeConnection(Connection con){try {con.close();} catch (SQLException e) {System.out.println("关闭资源失败");}}}

jdbc连接数据库之修改

6、public class Test {public static void main(String[] args) {Connection con =null;try {con=DBUtil.getConnection();} catch (SQLException e) {// TODO Auto-generated catch blocke.printStackTrace();}finally{DBUtil.closeConnection(con);}System.out.println(con);}}

jdbc连接数据库之修改
  • Eclipse怎么使用
  • 如何编写数据字典
  • Eclipse控制台乱码怎么解决?
  • 如何修改eclipse的workspace目录
  • 通过SmartArt在EXCEL中制作关系图
  • 热门搜索
    catch是什么意思 m2ts用什么播放器 怨天尤人的意思 sugar是什么意思 夺眶而出的意思 its是什么意思 清明古诗的意思 金箔金命是什么意思 爱乐维吃到什么时候 主宰的意思