java连接mysql的一般操作流程
java连接mysql
package com.test; import java.sql.Connection; import java.sql.DriverManager; import java.sql.ResultSet; import java.sql.Statement; public class TestJDBC { public static void main(String[] args) { Connection conn = null; Statement stmt = null; ResultSet rs = null; try{ Class.forName("com.mysql.jdbc.Driver"); conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncode=utf-8","root","admin"); stmt = conn.createStatement(); rs = stmt.executeQuery("SELECT * FROM students"); while(rs.next()){ System.out.println(rs.getString("name")); System.out.println(rs.getString("sex")); } }catch (Exception e) { e.printStackTrace(); }finally{ if(rs != null){ try{ rs.close(); }catch (Exception e) { e.printStackTrace(); } } if(stmt != null){ try{ stmt.close(); }catch (Exception e) { e.printStackTrace(); } } if(conn != null){ try{ conn.close(); }catch (Exception e) { e.printStackTrace(); } } } } }
相关推荐
CoderToy 2020-11-16
emmm00 2020-11-17
王艺强 2020-11-17
ribavnu 2020-11-16
bianruifeng 2020-11-16
wangshuangbao 2020-11-13
苏康申 2020-11-13
vivenwan 2020-11-13
moyekongling 2020-11-13
云中舞步 2020-11-12
要啥自行车一把梭 2020-11-12
aydh 2020-11-12
kuwoyinlehe 2020-11-12
minerk 2020-11-12
vitasfly 2020-11-12
jazywoo在路上 2020-11-11
敏敏张 2020-11-11
世樹 2020-11-11