标签:
一、JDBC的简介
//(1) 注册驱动
DriverManager.registerDriver(newDriver());
//(2) 连接数据库
Connection conn=DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/android","root","didi848484");
//(3) 执行sql语句操作数据库
//1.创建一个 Statement 对象来将 SQL 语句发送到数据库。
Statement stem=conn.createStatement();
//2.执行SQL语句
ResultSet rs=stem.executeQuery("select * from user");
//(4) 遍历结果集
while(rs.next())
{
System.out.print("id:"+rs.getInt("id"));
System.out.print(",username:"+rs.getString("username"));
System.out.print(",password:"+rs.getString("password"));
//.....
System.out.println();
}
//(5) 关闭资源
rs.close();
stem.close();
conn.close();
Class.forName("com.mysql.jdbc.Driver");
//创建ResultSet对象来接收查询结果
ResultSet rs=stem.executeQuery("select * from user");
//通过next()和各类get方法取出数据
while(rs.next())
{
System.out.print("id:"+rs.getInt("id"));
System.out.print(",username:"+rs.getString("username"));
System.out.print(",password:"+rs.getString("password"));
//.....
System.out.println();
}
标签:
原文地址:http://www.cnblogs.com/didixyy/p/6a3f7a2251b4fcf4b8d75823ca081eed.html