记忆口诀:ps set() rs get()
import java.sql.*;
public class Main {
//记忆:ps set() rs get()
public static void main(String[] args) throws Exception{
//1.加载驱动,获得连接
Class.forName("com.mysql.jdbc.Driver");//包.类
String url = "jdbc:mysql://localhost:3307/mook";//jdbc在前,mysql在后,与上面相反
String password = "1";
String user = "root";
Connection conn = DriverManager.getConnection(url,user,password);
//2.利用PrepareStatement进行查询 注意:ps对象由conn.prepareStatement(sql)获得(括号里有个sql)
String sql = "select user_name from user1 where id = ?";
PreparedStatement ps = conn.prepareStatement(sql);
ps.setInt(1,1);
//3.利用ResultSet接收结果,获得结果
ResultSet rs = ps.executeQuery();
while(rs.next()){
System.out.println(rs.getString(1));
}
}
}