当前位置:Gxlcms > 数据库问题 > java连接MySQL数据库(jdbc)

java连接MySQL数据库(jdbc)

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

zp.itcase.JDBCdemo; import java.sql.Connection; import java.sql.DriverManager; import java.sql.SQLException; import java.sql.Statement; public class JDBCDemo3 { public static void main(String[] args) { Statement stmt = null; Connection conn = null; try { // 1.注册驱动 Class.forName("com.mysql.jdbc.Driver"); // 2.获取连接对象 conn = DriverManager.getConnection("jdbc:mysql:///db3", "root", "root"); // 3. 定义sql String sql = "update account set balance = 1500 where id =3"; // 4.获取执行sql的对象 stmt = conn.createStatement(); // 5.执行sql int count = stmt.executeUpdate(sql); // 6.处理结果 if (count > 0) { System.out.println("修改成功"); } else { System.out.println("修改失败"); } } catch (ClassNotFoundException e) { e.printStackTrace(); } catch (SQLException e) { e.printStackTrace(); } finally { if (stmt != null) { try { stmt.close(); } catch (SQLException e) { e.printStackTrace(); } } if (conn != null) { try { conn.close(); } catch (SQLException e) { e.printStackTrace(); } } } } }

 

java连接MySQL数据库(jdbc)

标签:tac   sql数据库   成功   数据   stat   class   nts   lex   close   

人气教程排行