当前位置:Gxlcms > mysql > java学习之联接Mysql

java学习之联接Mysql

时间:2021-07-01 10:21:17 帮助过:27人阅读

java 学习之连接 Mysql 首先导入 mysql-connector-java-5.1.10-bin.ja 下载地址: http://download.csdn.net/detail/u014112584/7359185 鼠标放在项目上,右击选择Properties-----Java Build Path ------Add External JARS 测试代码: import java.sql.Connec

java 学习之连接 Mysql

首先导入mysql-connector-java-5.1.10-bin.ja

下载地址:http://download.csdn.net/detail/u014112584/7359185

鼠标放在项目上,右击选择Properties----->Java Build Path ------>Add External JARS



测试代码:

  1. import java.sql.Connection;
  2. import java.sql.DriverManager;
  3. import java.sql.ResultSet;
  4. import java.sql.SQLException;
  5. import com.mysql.jdbc.PreparedStatement;
  6. import com.mysql.jdbc.Statement;
  7. public class MysqlTest {
  8. static String drivername="com.mysql.jdbc.Driver";
  9. static String url="jdbc:mysql://localhost:3306/expression";//指向数据源
  10. static String username="root";
  11. static String password="";
  12. static java.sql.Statement stmt=null;
  13. static ResultSet re=null;
  14. static Connection conn=null;
  15. static PreparedStatement pstm=null;
  16. /*
  17. * 构造函数进行初始化
  18. */
  19. public MysqlTest(){
  20. try{
  21. Class.forName(drivername);//将驱动加载到运行环境中,加载的时候,驱动会自动向DriverManager完成注册
  22. System.out.println("创建驱动成功");
  23. }catch(ClassNotFoundException e){
  24. e.printStackTrace();
  25. }
  26. }
  27. /*
  28. * 获取连接
  29. */
  30. public static Connection getConnection(){
  31. conn=null;
  32. try{
  33. conn=(Connection)DriverManager.getConnection(url, username, password);//有了驱动和连接地址后,需要使用DriverManager来获取连接
  34. System.out.println("连接数据库成功!");
  35. }catch(SQLException e){
  36. e.printStackTrace();
  37. }
  38. return conn;
  39. }
  40. /**
  41. * 关闭连接
  42. * @param args
  43. */
  44. public static void free(ResultSet rs,Connection conn,java.sql.Statement stmt2){
  45. if(rs!=null){
  46. try {
  47. rs.close();
  48. } catch (SQLException e) {
  49. // TODO Auto-generated catch block
  50. System.out.println("关闭ResultSet失败!");
  51. e.printStackTrace();
  52. }finally{
  53. if(conn!=null){
  54. try {
  55. conn.close();
  56. } catch (SQLException e) {
  57. // TODO Auto-generated catch block
  58. System.out.println("关闭Connection失败!");
  59. e.printStackTrace();
  60. }finally{
  61. if(stmt2!=null){
  62. try {
  63. stmt2.close();
  64. } catch (SQLException e) {
  65. // TODO Auto-generated catch block
  66. System.out.println("关闭Statement失败!");
  67. e.printStackTrace();
  68. }
  69. }
  70. }
  71. }
  72. }
  73. }
  74. }
  75. public static void main(String[]args){
  76. MysqlTest.getConnection();
  77. try {
  78. stmt=conn.createStatement();
  79. } catch (SQLException e) {
  80. // TODO Auto-generated catch block
  81. e.printStackTrace();
  82. }
  83. try {
  84. re=stmt.executeQuery("select * from data");
  85. } catch (SQLException e) {
  86. // TODO Auto-generated catch block
  87. e.printStackTrace();
  88. }
  89. int i=1;
  90. try {
  91. while(re.next()){
  92. System.out.println(i++);
  93. }
  94. } catch (SQLException e) {
  95. // TODO Auto-generated catch block
  96. e.printStackTrace();
  97. }
  98. free(re,conn,stmt);
  99. System.out.println("OK");
  100. }
  101. }

java 访问数据库更多例子http://download.csdn.net/detail/u014112584/7359179

人气教程排行