MySQL数据库连接JDBC
2013-05-04 20:19
211 查看
1 package com.frankstyle; 2 3 import java.sql.Connection; 4 import java.sql.DriverManager; 5 import java.sql.ResultSet; 6 import java.sql.SQLException; 7 import java.sql.Statement; 8 9 /** 10 * @author wang520123fan 11 * 12 */ 13 public class JDBC_Connection 14 { 15 static String driverName="com.mysql.jdbc.Driver"; 16 static String url="jdbc:mysql://localhost:3306/library";//需要设置自己的数据库名 17 static String username="root"; 18 static String password="520123"; 19 20 static 21 { 22 try{ 23 Class.forName(driverName);//通过路径来创建驱动 24 System.out.println("创建驱动成功"); 25 }catch(ClassNotFoundException e) 26 { 27 e.printStackTrace(); 28 } 29 } 30 31 /** 32 * @return 返回连接 33 */ 34 public static Connection getConnection() 35 { 36 Connection conn=null; 37 try{ 38 conn=(Connection)DriverManager.getConnection(url,username,password); 39 System.out.println("连接数据库成功"); 40 }catch(SQLException e) 41 { 42 e.printStackTrace(); 43 } 44 return conn; 45 } 46 47 /** 48 * @param conn 需要关闭的 Connection 49 * @param stmt 需要关闭的 Statement 50 * @param rs 需要关闭的 ResultSet 51 */ 52 public void close(Connection conn,Statement stmt,ResultSet rs)//从内到外断开连接 53 { 54 try{ 55 if(rs!=null) 56 rs.close(); 57 }catch(SQLException e) 58 { 59 e.printStackTrace(); 60 System.out.println("关闭ResultSet失败!"); 61 }finally 62 { 63 rs=null; 64 try{ 65 if(stmt!=null) 66 stmt.close(); 67 }catch(SQLException e) 68 { 69 e.printStackTrace(); 70 System.out.println("关闭Statement失败!"); 71 }finally 72 { 73 stmt=null; 74 try{ 75 if(conn!=null) 76 conn.close(); 77 }catch(SQLException e) 78 { 79 e.printStackTrace(); 80 System.out.println("关闭Connection失败!"); 81 }finally 82 { 83 conn=null; 84 } 85 } 86 } 87 88 } 89 }
相关文章推荐
- Java 通过JDBC连接Mysql数据库的方法和实例【图文说明】
- JDBC连接MySQL数据库中文乱码
- 1、利用jdbc,连接mysql数据库
- Java通过JDBC连接Mysql数据库的方法和实例【图文说明】
- jdbc 连接mysql数据库
- JDBC连接MySQL数据库
- [JDBC]Eclipse连接MySQL数据库(傻瓜篇)
- Java使用jdbc连接MySql数据库,实现增删改查
- JSP使用JDBC连接MYSQL数据库的方法
- JDBC连接MYSQL数据库
- JDBC通过SSH Tunnel连接MySQL数据库
- JDBC连接MySql数据库步骤 以及查询、插入、删除、更新等
- mysql数据库无法连接(JDBC)java.net.ConnectException: Connection timed out
- java 通过JDBC连接Mysql数据库的方法和实例
- jdbc 连接mysql数据库
- JDBC连接MySQL数据库及示例
- 【MySQL】使用JDBC时,无法连接远程MySQL数据库
- JDBC实现MySQL数据库连接
- JDBC连接MYSQL数据库
- 一、JAVA通过JDBC连接mysql数据库(连接)