教育路上
摘要:java 简单 连接 数据库 ,java简单连接数据库。以下是我们为大家整理的,相信大家阅读完后肯定有了自己的选择吧。
2022-05-28 11:06网络推荐
package org.jdbc; import java.sql.Connection; import java.sql.DriverManager; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement; public class TestJDBC { public static void main(String[] args) { ResultSet rs = null; Statement stmt = null; Connection conn = null; try { Class.forName("com.mysql.jdbc.Driver"); conn = DriverManager.getConnection( "jdbc:mysql://localhost:3306/xsxx", "root", "root"); stmt = conn.createStatement(); rs = stmt.executeQuery("select * from xs"); while (rs.next()) { System.out.print(rs.getInt("id")); System.out.print(rs.getString("name")); System.out.println(rs.getString("major")); } } catch (ClassNotFoundException e) { e.printStackTrace(); } catch (SQLException e) { e.printStackTrace(); } finally { try { if (rs != null) { rs.close(); // 关闭ResultSet对象 rs = null; } if (stmt != null) { stmt.close(); // 关闭Statement对象 stmt = null; } if (conn != null) { conn.close(); // 关闭Connection对象 conn = null; } } catch (SQLException e) { e.printStackTrace(); } } } }
访客的评论 2024/10/04 07:53
文中描述的是准确的吗,如何报名!