java操纵mysql;
自己实现的第一个程序,小激动!
package jdbcdemo1; import java.sql.DriverManager; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Connection; /** * @author Administrator * 演示示例1:使用纯Java方式连接数据 */ /** * @author Administrator alt+shift+j */ public class demo1 { public static void main(String[] args) { Connection conn = null; PreparedStatement ps = null; ResultSet rs = null; try { Class.forName("com.mysql.jdbc.Driver"); } catch (ClassNotFoundException e) { e.printStackTrace(); System.out.println("建立连接失败"); } try { conn = DriverManager.getConnection( "jdbc:mysql://localhost:3306/hello?useUnicode=true&characterEncoding=utf-8", "root", "123456"); System.out.println("建立连接成功"); // 3.创建ps ,代表预编译的sql对象 ps = conn.prepareStatement("select* from student"); // 4.执行 rs = ps.executeQuery(); while (rs.next()) { System.out.println(rs.getInt("id") + " " + rs.getString("name") + rs.getFloat("Chinese")); } } catch (SQLException e) { e.printStackTrace(); System.out.println("建立连接失败"); } finally { try { conn.close(); } catch (SQLException e) { e.printStackTrace(); } if (rs != null) { try { rs.close(); } catch (Exception e2) { // TODO: handle exception e2.printStackTrace(); } rs = null; } if (ps != null) { try { ps.close(); } catch (Exception e2) { // TODO: handle exception e2.printStackTrace(); } ps = null; } } } }
原文:https://www.cnblogs.com/helloworld2019/p/10798439.html