import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
public class JdbcDemo01 {
public static void main(String[] args) throws Exception {
//注册驱动
Class.forName("com.mysql.jdbc.Driver");
Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/db2", "root", "root");
//定义sql语句
// String sql = "update student set money=5000 where name ='关之琳'";
String sql ="select*from student";
//5.获取执行sql的对象 Statement
Statement state = conn.createStatement();
//执行sql
// int count = state.executeUpdate(sql);
ResultSet result = state.executeQuery(sql);
while (result.next()){
String name = result.getString("name");
int money = result.getInt("money");
System.out.println(name+".."+money);
}
//处理结果
result.close();
conn.close();
state.close();
}
} |
|