java与MySQL数据库的连接

1.数据库的安装和建立参见上一篇博客中的第1,2步骤。

或使用SQL语句


[sql] view plaincopy​​​​​​​


  1. # ubuntu Linux
  2. sudo mysql -u root -p
  3. #Windows 7(mysql.exe)

  4. create database testdb;
  5. use testdb;
  6. CREATE TABLE `name_table` (
  7. `_id` int(11) NOT NULL,
  8. `name` varchar(32) CHARACTER SET utf8,
  9. `age` int(11) NOT NULL,
  10. `work` varchar(32) CHARACTER SET utf8,
  11. `others` varchar(512) CHARACTER SET utf8 DEFAULT NULL,
  12. PRIMARY KEY (`_id`)
  13. ) ENGINE=InnoDB DEFAULT CHARSET=utf8;




2.Eclipse的配置。

导入包mysql-connector-java-5.0.5-bin.jar


3.java代码的编写

[java code]


[java] view plaincopy​
​​​​


  1. /*
  2. *@author: ZhengHaibo
  3. *web: blog.csdn.net/nuptboyzhb
  4. *mail: zhb931706659@126.com
  5. *2012-10-6 Nanjing njupt
  6. */
  7. import java.sql.Connection;
  8. import java.sql.DriverManager;
  9. import java.sql.PreparedStatement;
  10. import java.sql.ResultSet;
  11. import java.sql.SQLException;
  12. public class helloworld {
  13. private Connection conn = null;
  14. PreparedStatement statement = null;
  15. // connect to MySQL
  16. void connSQL() {
  17. String urle = "jdbc:mysql://localhost:3306/testdb";//port:3306 database:testdb
  18. String username = "root";//user
  19. String password = "931706659";//password
  20. try {
  21. Class.forName("com.mysql.jdbc.Driver" );//加载驱动,连接数据库
  22. conn = DriverManager.getConnection(urle,username, password );
  23. }
  24. //捕获加载驱动程序异常
  25. catch ( ClassNotFoundException cnfex ) {
  26. System.err.println(
  27. "装载 JDBC/ODBC 驱动程序失败。" );
  28. cnfex.printStackTrace();
  29. }
  30. //捕获连接数据库异常
  31. catch ( SQLException sqlex ) {
  32. System.err.println( "无法连接数据库" );
  33. sqlex.printStackTrace();
  34. }
  35. }

  36. // disconnect to MySQL
  37. void deconnSQL() {
  38. try {
  39. if (conn != null)
  40. conn.close();
  41. } catch (Exception e) {
  42. System.out.println("关闭数据库问题 :");
  43. e.printStackTrace();
  44. }
  45. }

  46. // execute selection language
  47. ResultSet selectSQL(String sql) {
  48. ResultSet rs = null;
  49. try {
  50. statement = conn.prepareStatement(sql);
  51. rs = statement.executeQuery(sql);
  52. } catch (SQLException e) {
  53. e.printStackTrace();
  54. }
  55. return rs;
  56. }

  57. // execute insertion language
  58. boolean insertSQL(String sql) {
  59. try {
  60. statement = conn.prepareStatement(sql);
  61. statement.executeUpdate();
  62. return true;
  63. } catch (SQLException e) {
  64. System.out.println("插入数据库时出错:");
  65. e.printStackTrace();
  66. } catch (Exception e) {
  67. System.out.println("插入时出错:");
  68. e.printStackTrace();
  69. }
  70. return false;
  71. }
  72. //execute delete language
  73. boolean deleteSQL(String sql) {
  74. try {
  75. statement = conn.prepareStatement(sql);
  76. statement.executeUpdate();
  77. return true;
  78. } catch (SQLException e) {
  79. System.out.println("插入数据库时出错:");
  80. e.printStackTrace();
  81. } catch (Exception e) {
  82. System.out.println("插入时出错:");
  83. e.printStackTrace();
  84. }
  85. return false;
  86. }
  87. //execute update language
  88. boolean updateSQL(String sql) {
  89. try {
  90. statement = conn.prepareStatement(sql);
  91. statement.executeUpdate();
  92. return true;
  93. } catch (SQLException e) {
  94. System.out.println("插入数据库时出错:");
  95. e.printStackTrace();
  96. } catch (Exception e) {
  97. System.out.println("插入时出错:");
  98. e.printStackTrace();
  99. }
  100. return false;
  101. }
  102. // show data in ju_users
  103. void layoutStyle2(ResultSet rs) {
  104. System.out.println("-----------------");
  105. System.out.println("执行结果如下所示:");
  106. System.out.println("-----------------");
  107. System.out.println(" id" + "\t\t" + "name" +"\t\t" + "age" + "\t\t" +"work"+ "\t\t" + "others");
  108. System.out.println("-----------------");
  109. try {
  110. while (rs.next()) {
  111. System.out.println(rs.getInt("_id") + "\t\t"
  112. + rs.getString("name") + "\t\t"
  113. +rs.getInt("age") + "\t\t"
  114. + rs.getString("work")+ "\t\t"
  115. + rs.getString("others"));
  116. }
  117. } catch (SQLException e) {
  118. System.out.println("显示时数据库出错。");
  119. e.printStackTrace();
  120. } catch (Exception e) {
  121. System.out.println("显示出错。");
  122. e.printStackTrace();
  123. }
  124. }

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

  126. helloworld h = new helloworld();
  127. h.connSQL();
  128. String s = "select * from name_table";

  129. String insert = "insert into name_table(_id,name,age,work,others) values("+15+",'csdn',"+24+",'M.S.','nupt')";
  130. String update = "update name_table set age =19 where name= 'zhb'";
  131. String delete = "delete from name_table where name= 'csdn'";

  132. if (h.insertSQL(insert) == true) {
  133. System.out.println("insert successfully");
  134. ResultSet resultSet = h.selectSQL(s);
  135. h.layoutStyle2(resultSet);
  136. }
  137. if (h.updateSQL(update) == true) {
  138. System.out.println("update successfully");
  139. ResultSet resultSet = h.selectSQL(s);
  140. h.layoutStyle2(resultSet);
  141. }
  142. if (h.insertSQL(delete) == true) {
  143. System.out.println("delete successfully");
  144. ResultSet resultSet = h.selectSQL(s);
  145. h.layoutStyle2(resultSet);
  146. }
  147. h.deconnSQL();
  148. }
  149. }



4.实验结果

[image]