jdbc的一个小例子
程序员文章站
2022-03-28 12:31:31
...
package com.itheima.a_jdbc;
import org.junit.Test;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
public class a_hello {
@Test
public void f1() {
System.out.println("hello");
}
@Test
public void f2() throws Exception {
//注册驱动
Class.forName("com.mysql.jdbc.Driver");
//获取连接
Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/day07", "root", "");
//编写sql
String sql = "select * from category";
//创建语句执行者
PreparedStatement st = conn.prepareStatement(sql);
//设置参数
//执行sql
ResultSet rs = st.executeQuery();
//处理结果
while (rs.next()) {
System.out.println(rs.getString("cid") + "::" + rs.getString("cname"));
}
//释放资源
rs.close();
st.close();
conn.close();
}
}