1jdbc连接数据库的两种步骤
DriverManager 获取Connection连接 数据库配置使用配置文件储存的方式
Class.forname(com.mysql.jdbc.Driver);
String url = “jdbc:mysql://192.168.5.33:3306/Myschool”;
Connection conn = DriverManager.getConnection(url, “root”, “ok”);
return conn;
Connection conn=getCoon();
Statement stmt=conn.createStatement();
String sql=“show tables”;
ResultSet rs=stmt.executeQuery(sql);//接收sql查询的东西
while (rs.next()){
System.out.println(rs.getString(1));
}
答案:
共同步骤
加载驱动:Class.forname(com.mysql.jdbc.Driver);
获取连接:Connection conn = DriverManager.getConnection(url, “root”, “ok”);
犯法1:Statement:
创建语句管理对象:Statement stmt=conn.getStatement();
装载sql并运行:ResultSet rs=stmt.executeQuery(sql);
int num=stmt.executeQuery(sql);
方法2:PrepareStatement:
PrepareStatement pst=conn.preapreStatement(sql);
ResultSet rs=pst.executeQuery(); mMeiy sql
int num =pst.executeQuery;
遍历结果集rs while
关闭资源
2数据库配置使用配置文件储存的方式
Properties p=new Properties();
p.load(new FileInputStream)(配置文件路经))
String 属性名=p.get(属性名);
3java三层模型是哪三层
dao:与数据库的交互
service:业务逻辑的判断
controller:与前端页面的交互