MySql_connection
程序员文章站
2022-05-13 17:43:33
...
--------------------------------------------------
http://dev.mysql.com/downloads
1.MySQL Server 5.1
2.Navicat 8 for MySQL (数据库交互工具)
--------------------------------------------------
package com; import java.sql.DriverManager; import java.sql.SQLException; import com.mysql.jdbc.Connection; import com.mysql.jdbc.ResultSet; import com.mysql.jdbc.Statement; public class Maintest { public static void main(String[] args) { Connection conn = null; Statement stmt = null; ResultSet rs = null; try { Class.forName("com.mysql.jdbc.Driver"); conn = (Connection) DriverManager.getConnection("jdbc:mysql://localhost/xjq?user=root&password=root"); stmt = (Statement) conn.createStatement(); rs = (ResultSet) stmt.executeQuery("select * from tuser"); while (rs.next()) { System.out.println(rs.getString("fid") + ":"+ rs.getString("fname")); } } catch (ClassNotFoundException e) { e.printStackTrace(); } catch (SQLException ex) { System.out.println("SQLException: " + ex.getMessage()); System.out.println("SQLState: " + ex.getSQLState()); System.out.println("VendorError: " + ex.getErrorCode()); } finally { try { if(rs != null) { rs.close(); rs = null; } if(stmt != null) { stmt.close(); stmt = null; } if(conn != null) { conn.close(); conn = null; } } catch (SQLException e) { e.printStackTrace(); } } } }