当前位置:   article > 正文

Java链接mySQL数据库进行增删改查_java和mysql水果店链接怎样删除记录

java和mysql水果店链接怎样删除记录

Java链接mySQL数据库代码

改和查对于增加是一样的

  1. public class JDBCTest {
  2. /**
  3. * ResultSet封装了JDBC结果集,进行查询的结果
  4. */
  5. @Test
  6. public void testResultSet() {
  7. Connection connection = null;
  8. Statement statement = null;
  9. ResultSet rs = null;
  10. try {
  11. connection = JDBCTools.getConnection();
  12. statement = (Statement) connection.createStatement();
  13. String sql = "SELECT * FROM jdbc_1";
  14. rs = statement.executeQuery(sql);
  15. while (rs.next()) {
  16. System.out.print(rs.getInt(1) + "\t");
  17. System.out.print(rs.getString(2) + "\t");
  18. System.out.print(rs.getString(3));
  19. System.out.println();
  20. }
  21. } catch (Exception e) {
  22. e.printStackTrace();
  23. } finally {
  24. JDBCTools.release(rs, statement, connection);
  25. }
  26. }
  27. /**
  28. * 通用更新方法
  29. * @throws SQLException
  30. */
  31. public void updata(String sql) throws SQLException {
  32. Connection connection = null;
  33. Statement statement = null;
  34. try {
  35. connection = getConnection();
  36. // 获取Statement 对象
  37. statement = (Statement) connection.createStatement();
  38. // 执行sql语句
  39. statement.executeUpdate(sql);
  40. } catch (Exception e) {
  41. // TODO Auto-generated catch block
  42. e.printStackTrace();
  43. } finally {
  44. try {
  45. // 关闭statement对象
  46. if (statement != null) {
  47. statement.close();
  48. }
  49. } catch (Exception e) {
  50. // TODO Auto-generated catch block
  51. e.printStackTrace();
  52. } finally {
  53. // 关闭数据库连接
  54. if (connection != null) {
  55. connection.close();
  56. }
  57. }
  58. }
  59. }
  60. /**
  61. * 1.通过jdbc向指定表中插入数据
  62. *
  63. * @throws Exception
  64. *
  65. */
  66. @Test
  67. public void testStatement() throws Exception {
  68. // 获取数据库链接
  69. Connection connection = null;
  70. Statement statement = null;
  71. try {
  72. connection = getConnection();
  73. // 要执行的sql语句
  74. String sql = "INSERT INTO jdbc_1 (jname,addr) VALUE ('李力','浑江市')";
  75. // 获取Statement 对象
  76. statement = (Statement) connection.createStatement();
  77. // 执行sql语句
  78. statement.executeUpdate(sql);
  79. } catch (Exception e) {
  80. // TODO Auto-generated catch block
  81. e.printStackTrace();
  82. } finally {
  83. try {
  84. // 关闭statement对象
  85. if (statement != null) {
  86. statement.close();
  87. }
  88. } catch (Exception e) {
  89. // TODO Auto-generated catch block
  90. e.printStackTrace();
  91. } finally {
  92. // 关闭数据库连接
  93. if (connection != null) {
  94. connection.close();
  95. }
  96. }
  97. }
  98. }
  99. /**
  100. * DriverManager类是驱动的管理类 利用DriverManager连接数据库,进行数据库驱动注册
  101. *
  102. * @throws Exception
  103. */
  104. @Test
  105. public void testDriverManager() throws Exception {
  106. String driverClass = null;
  107. String jdbcUrl = null;
  108. String user = null;
  109. String password = null;
  110. // 读取properties文件
  111. InputStream in = getClass().getClassLoader().getResourceAsStream(
  112. "jdbc.properties");
  113. Properties properties = new Properties();
  114. properties.load(in);
  115. driverClass = properties.getProperty("driver");
  116. jdbcUrl = properties.getProperty("url");
  117. user = properties.getProperty("user");
  118. password = properties.getProperty("password");
  119. Class.forName(driverClass);
  120. Connection connection = (Connection) DriverManager.getConnection(
  121. jdbcUrl, user, password);
  122. System.out.println(connection);
  123. }
  124. /**
  125. * 驱动测试,对链接驱动进行测试
  126. *
  127. * @throws SQLException
  128. *
  129. */
  130. @Test
  131. public void testDriver() throws SQLException {
  132. // 创建Drivers实现类
  133. Driver driver = new com.mysql.jdbc.Driver();
  134. String url = "jdbc:mysql://localhost:3306/jdbc";
  135. Properties info = new Properties();
  136. info.put("user", "root");
  137. info.put("password", "root");
  138. // 调用driver接口的Connection
  139. Connection connection = (Connection) driver.connect(url, info);
  140. System.out.println(connection);
  141. }
  142. /**
  143. * 编写通用方法获取任意数据库链接,不用修改源程序
  144. *
  145. * @throws ClassNotFoundException
  146. * @throws IllegalAccessException
  147. * @throws InstantiationException
  148. * @throws SQLException
  149. * @throws IOException
  150. */
  151. public Connection getConnection() throws InstantiationException,
  152. IllegalAccessException, ClassNotFoundException, SQLException,
  153. IOException {
  154. String driverClass = null;
  155. String jdbcUrl = null;
  156. String user = null;
  157. String password = null;
  158. // 读取properties文件
  159. InputStream in = getClass().getClassLoader().getResourceAsStream(
  160. "jdbc.properties");
  161. Properties properties = new Properties();
  162. properties.load(in);
  163. driverClass = properties.getProperty("driver");
  164. jdbcUrl = properties.getProperty("url");
  165. user = properties.getProperty("user");
  166. password = properties.getProperty("password");
  167. Driver driver = (Driver) Class.forName(driverClass).newInstance();
  168. Properties info = new Properties();
  169. info.put("user", user);
  170. info.put("password", password);
  171. Connection connection = (Connection) driver.connect(jdbcUrl, info);
  172. return connection;
  173. }
  174. @Test
  175. public void testConnection() throws InstantiationException,
  176. IllegalAccessException, ClassNotFoundException, SQLException,
  177. IOException {
  178. System.out.println(getConnection());
  179. }
  180. }
  1. public class JDBCTools {
  2. /**
  3. * 结果查询关闭
  4. * @param rs
  5. * @param statement
  6. * @param conn
  7. */
  8. public static void release(ResultSet rs,Statement statement, Connection conn) {
  9. if (rs != null) {
  10. try {
  11. rs.close();
  12. } catch (SQLException e) {
  13. // TODO Auto-generated catch block
  14. e.printStackTrace();
  15. }
  16. }
  17. if (statement != null) {
  18. try {
  19. statement.close();
  20. } catch (Exception e2) {
  21. e2.printStackTrace();
  22. }
  23. }
  24. if (conn != null) {
  25. try {
  26. conn.close();
  27. } catch (Exception e2) {
  28. e2.printStackTrace();
  29. }
  30. }
  31. }
  32. /**
  33. * 数据库更新方法
  34. * @param sql
  35. */
  36. public void uodate(String sql) {
  37. Connection connection = null;
  38. Statement statement = null;
  39. try {
  40. connection = JDBCTools.getConnection();
  41. statement = (Statement) connection.createStatement();
  42. statement.executeUpdate(sql);
  43. } catch (Exception e) {
  44. e.printStackTrace();
  45. } finally {
  46. JDBCTools.release(statement, connection);
  47. }
  48. }
  49. /**
  50. * 关闭数据库连接的方法
  51. * @param statement
  52. * @param conn
  53. */
  54. public static void release(Statement statement, Connection conn) {
  55. if (statement != null) {
  56. try {
  57. statement.close();
  58. } catch (Exception e2) {
  59. e2.printStackTrace();
  60. }
  61. }
  62. if (conn != null) {
  63. try {
  64. conn.close();
  65. } catch (Exception e2) {
  66. e2.printStackTrace();
  67. }
  68. }
  69. }
  70. /**
  71. * 编写通用方法获取任意数据库链接,不用修改源程序
  72. *
  73. * @return
  74. * @throws ClassNotFoundException
  75. * @throws IllegalAccessException
  76. * @throws InstantiationException
  77. * @throws SQLException
  78. * @throws IOException
  79. */
  80. public static Connection getConnection() throws InstantiationException,
  81. IllegalAccessException, ClassNotFoundException, SQLException,
  82. IOException {
  83. String driverClass = null;
  84. String jdbcUrl = null;
  85. String user = null;
  86. String password = null;
  87. // 读取properties文件
  88. InputStream in = JDBCTools.class.getClassLoader().getResourceAsStream(
  89. "jdbc.properties");
  90. Properties properties = new Properties();
  91. properties.load(in);
  92. driverClass = properties.getProperty("driver");
  93. jdbcUrl = properties.getProperty("url");
  94. user = properties.getProperty("user");
  95. password = properties.getProperty("password");
  96. Driver driver = (Driver) Class.forName(driverClass).newInstance();
  97. Properties info = new Properties();
  98. info.put("user", user);
  99. info.put("password", password);
  100. Connection connection = (Connection) driver.connect(jdbcUrl, info);
  101. return connection;
  102. }
  103. }



声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/Monodyee/article/detail/575433
推荐阅读
相关标签
  

闽ICP备14008679号