云萌主云应用官方论坛

标题: JAVA mysql数据库连接 [打印本页]

作者: lady-niuniu    时间: 2017-1-10 11:56
标题: JAVA mysql数据库连接
来源:http://www.open-open.com/code/view/1454633126620

  1. package learn;


  2. import java.sql.Connection;

  3. import java.sql.DriverManager;

  4. import java.sql.SQLException;

  5. import java.sql.Statement;

  6. import java.sql.ResultSet;


  7. public class Example01 {



  8. public static void main(String[] args) {

  9. // TODO Auto-generated method stub

  10. String URL="jdbc:mysql://127.0.0.1:3306/test1";

  11. String DRIVER="com.mysql.jdbc.Driver";

  12. String USERNAME="root";

  13. String PASSWD="123456";

  14. String sql;


  15. Connection connection =null;


  16. try{

  17. Class.forName(DRIVER);

  18. connection=DriverManager.getConnection(URL,USERNAME,PASSWD);

  19. Statement stmt = connection.createStatement();

  20. sql = "select * from t1";

  21. ResultSet rs=stmt.executeQuery(sql);

  22. while(rs.next()){

  23. System.out.println(rs.getInt(1)+" "+rs.getString(2)+" "+rs.getString(3));

  24. }

  25. }catch(SQLException e){

  26. System.out.println("test1");

  27. e.printStackTrace();

  28. }catch(ClassNotFoundException e){

  29. System.out.println("test2");

  30. e.printStackTrace();

  31. }finally{

  32. try{

  33. connection.close();

  34. }catch(SQLException e){

  35. System.out.println("test3");

  36. e.printStackTrace();

  37. }

  38. }

  39. }


  40. }

复制代码







欢迎光临 云萌主云应用官方论坛 (https://yunmengzhu.com/) Powered by Discuz! X3.4