标签:host pre jdk 创建 port 执行 ext mysq java基础
MySQL数据库查询
1 import java.sql.*; 2 public class JdbcDemo1{ 3 public static void main(String[] args){ 4 try{ 5 //1.加载数据库驱动程序 6 Class.forName("org.gjt.mm.mysql.Driver"); 7 8 //2.创建与数据库的连接 9 Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/test", "root", "root"); 10 11 //3.创建Statement对象,并执行sql语句 12 Statement stmt = conn.createStatement(); 13 String sql = "select * from user"; 14 stmt.execute(sql); //获取多个结果 15 ResultSet rs = stmt.getResultSet(); 16 17 //4.处理结果集 18 while(rs.next()){ 19 String uname = rs.getString("uname"); 20 int age = rs.getInt("age"); 21 System.out.println(uname + ":" + age); 22 } 23 24 //5.关闭查询语句和连接 25 rs.close(); 26 stmt.close(); 27 conn.close(); 28 }catch(Exception e){ 29 e.printStackTrace(); 30 } 31 } 32 }
主要类 Statement、ResultSet,具体方法参考jdk
标签:host pre jdk 创建 port 执行 ext mysq java基础
原文地址:http://www.cnblogs.com/lhat/p/6110493.html