Guest User

Untitled

a guest
Nov 23rd, 2017
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.09 KB | None | 0 0
  1. import java.sql.Connection;
  2. import java.sql.DriverManager;
  3. import java.sql.SQLException;
  4.  
  5. /**
  6. * 1* @author Administrator
  7. * 1*
  8. * 1
  9. */
  10. public class DBUtil {
  11.  
  12.  
  13. public Connection connectMysql() {
  14. Connection conn = null;
  15. try{
  16. Class.forName("com.mysql.jdbc.Driver");
  17. System.out.println("成功加载MySQL驱动!");
  18. }catch(ClassNotFoundException e1){
  19. System.out.println("找不到MySQL驱动!");
  20. e1.printStackTrace();
  21. }
  22.  
  23. String url="jdbc:mysql://localhost:3306/test0";
  24. try {
  25. conn = DriverManager.getConnection(url, "root","00000000");
  26. System.out.print("成功连接到数据库!");
  27. } catch (SQLException e){
  28. e.printStackTrace();
  29. }
  30. finally {
  31. return conn;
  32. }
  33. }
  34. public void connClose(Connection connection){
  35. try {
  36. connection.close();
  37. } catch (SQLException e) {
  38. e.printStackTrace();
  39. }
  40. }
  41.  
  42. public static void main(String[] args) {
  43. DBUtil dbUtil =new DBUtil();
  44. }
  45. }
Add Comment
Please, Sign In to add comment