public tt() { // TODO Auto-generated constructor stub try { Class.forName("com.mysql.jdbc.Driver"); Connection conn = (Connection) DriverManager.getConnection(Driver, username,password); java.sql.Statement stmt = conn.createStatement(); ResultSet rs=stmt.executeQuery("select * from aa1"); int a=0; while(rs.next()){ System.out.println(String.valueOf(a)+rs.getString("val")); a++; } } catch (Exception e) { e.printStackTrace(); } }}
// 1. 注册驱动 try { Class.forName("com.mysql.jdbc.Driver"); } catch (ClassNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); }// Mysql 的驱动 // 2. 获取数据库的连接 java.sql.Connection conn = java.sql.DriverManager.getConnection( "jdbc:mysql://localhost/test?useUnicode=true&characterEncoding=GBK", "root", null); // 3. 获取表达式 java.sql.Statement stmt = conn.createStatement(); // 执行插入数据的 SQL stmt.executeUpdate("insert into MyUser(username, password) values('JDBC 中文测试', '密码')"); // 4. 执行 SQL java.sql.ResultSet rs = stmt.executeQuery("select * from MyUser"); // 5. 显示结果集里面的数据 while(rs.next()) { System.out.println(rs.getInt(1)); System.out.println(rs.getString("username")); System.out.println(rs.getString("password")); System.out.println(); } // 6. 释放资源 rs.close(); stmt.close(); conn.close();