//1.Statement
Class.forName("com.mysql.jdbc.Driver");
String url = "jdbc:mysql://localhost:3306/preston_crm?useUnicode=true&characterEncoding=utf8";
String user = "root";
String password = "root";
Connection conn = DriverManager.getConnection(url, user, password);
Statement createStatement = conn.createStatement();
String sql = "select * from crm_manager";
ResultSet result = createStatement.executeQuery(sql);
while(result.next()){
System.out.println(result.getString("manager_name")+":"+result.getString("link_phone"));
}
result.close();
createStatement.close();
conn.close();
//2.PreparedStatement
Class.forName("com.mysql.jdbc.Driver");
String url = "jdbc:mysql://localhost:3306/preston_crm?useUnicode=true&characterEncoding=utf8";
String user = "root";
String password = "root";
Connection connection = DriverManager.getConnection(url, user, password);
String sql = "select * from crm_manager where manager_name like ?";
PreparedStatement ps = connection.prepareStatement(sql);
//ps.setString(1, "%g%");
List<Object> params = new ArrayList<Object>();
params.add("%g%");
for(int i=0; i<params.size(); i++){
ps.setObject(i+1, params.get(i));
}
ResultSet resultSet = ps.executeQuery();
while(resultSet.next()){
System.out.print(resultSet.getString("manager_name")+" : ");
System.out.println(resultSet.getString("link_phone"));
}
resultSet.close();
ps.close();
connection.close();
Class.forName("com.mysql.jdbc.Driver");
String url = "jdbc:mysql://localhost:3306/preston_crm?useUnicode=true&characterEncoding=utf8";
String user = "root";
String password = "root";
Connection conn = DriverManager.getConnection(url, user, password);
Statement createStatement = conn.createStatement();
String sql = "select * from crm_manager";
ResultSet result = createStatement.executeQuery(sql);
while(result.next()){
System.out.println(result.getString("manager_name")+":"+result.getString("link_phone"));
}
result.close();
createStatement.close();
conn.close();
//2.PreparedStatement
Class.forName("com.mysql.jdbc.Driver");
String url = "jdbc:mysql://localhost:3306/preston_crm?useUnicode=true&characterEncoding=utf8";
String user = "root";
String password = "root";
Connection connection = DriverManager.getConnection(url, user, password);
String sql = "select * from crm_manager where manager_name like ?";
PreparedStatement ps = connection.prepareStatement(sql);
//ps.setString(1, "%g%");
List<Object> params = new ArrayList<Object>();
params.add("%g%");
for(int i=0; i<params.size(); i++){
ps.setObject(i+1, params.get(i));
}
ResultSet resultSet = ps.executeQuery();
while(resultSet.next()){
System.out.print(resultSet.getString("manager_name")+" : ");
System.out.println(resultSet.getString("link_phone"));
}
resultSet.close();
ps.close();
connection.close();