package tms.jdbc;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
/*
* 简单链接
数据库
*/
public class Demo1 {
public static void main(String[] args) throws Exception {
//1、利用反射机制间接加载连接数据库
Class.forName("com.
mysql.jdbc.Driver");
//2、取得与
mysql数据库连接的桥梁
Connection conn = DriverManager.getConnection("jdbc:mysql://127.0.0.1/mydb", "root", "tms123");
//3、创建封装sql命令的对象
String sql = "select * from person";
Statement createStatement = conn.createStatement();
//4、执行sql命令,并返回符合条件的记录
ResultSet rs = createStatement.executeQuery(sql);
//处理结果集合
while(rs.next()) {
int id = rs.getInt("id");
String name = rs.getString("name");
System.out.println("id:"+id+" name:"+name);
}
//依次关闭连接对象
rs.close();
createStatement.close();
conn.close();
}
}