这是什么啊

java基本网络请求及jdbc基本使用

get请求
public class Demo03_HttpUrlConnection {
public static void main(String[] args) throws Exception {
String baseUrl = "http://10.0.184.253:8080/Day28_Server/LoginServlet";
String params = "username=aaa&password=123";
URL url = new URL(baseUrl + "?" + params);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setRequestMethod("GET");
connection.setConnectTimeout(5000);
connection.setDoInput(true);
if (connection.getResponseCode() == 200) {
InputStream inputStream = connection.getInputStream();
BufferedReader br = new BufferedReader(new InputStreamReader(
inputStream));
String result = br.readLine();
System.out.println("服务器回应:" + result);
}
}
}
post请求
public class Demo05_HttpUrlConnection {
public static void main(String[] args) throws Exception {
String baseUrl = "http://10.0.184.253:8080/Day28_Server/LoginServlet";
URL url = new URL(baseUrl);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setConnectTimeout(5000);
connection.setRequestMethod("POST");
connection.setDoInput(true);
connection.setDoOutput(true);
String params = "username=admin&password=123";
OutputStream outputStream = connection.getOutputStream();
outputStream.write(params.getBytes());
System.out.println("客户端已经建数据写给服务器了。。。");
if (connection.getResponseCode() == 200) {
InputStream inputStream = connection.getInputStream();
byte[] buf = new byte[100];
int len = 0;
len = inputStream.read(buf);
System.out.println("服务器说:" + new String(buf, 0, len));
}
}
}
阿帕奇get请求
public class Demo01_HttpClient {
public static void main(String[] args) {
HttpClient httpClient = new DefaultHttpClient();
String baseUrl = "https://www.baidu.com/img/bd_logo1.png";
HttpGet httpGet = new HttpGet(baseUrl);
HttpResponse httpResponse = httpClient.execute(httpGet);
if (httpResponse.getStatusLine().getStatusCode() == 200) {
HttpEntity httpEntity = httpResponse.getEntity();
byte[] data = EntityUtils.toByteArray(httpEntity);
FileOutputStream fos = new FileOutputStream("logo.png");
fos.write(data);
System.out.println("下载完啦。。。。");
fos.close();
}
}
}
阿帕奇post
public class Demo03_HttpClient_Login_Post {
public static void main(String[] args) throws IOException {
String baseUrl = "http://10.0.184.253:8080/Day28_Server/LoginServlet";
HttpClient httpClient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost(baseUrl);
String username = "admin";
String password = "123";
NameValuePair pair1 = new BasicNameValuePair("username", username);(服务器上的名称,本地字符串对象)
NameValuePair pair2 = new BasicNameValuePair("password", password);
List<NameValuePair> list = new ArrayList<>();
list.add(pair1);
list.add(pair2);
HttpEntity httpEntity1 = new UrlEncodedFormEntity(list);
httpPost.setEntity(httpEntity1);
HttpResponse httpResponse = httpClient.execute(httpPost);
if (httpResponse.getStatusLine().getStatusCode() == 200) {
HttpEntity httpEntity2 = httpResponse.getEntity();
System.out.println(EntityUtils.toString(httpEntity2, "utf-8"));
}
}
}
服务端
public class LoginServlet extends HttpServlet {
 private static final long serialVersionUID = 1L;
 public LoginServlet() {
 }
 protected void doGet(HttpServletRequest request,
   HttpServletResponse response) throws ServletException, IOException {
  this.doPost(request, response);
 }
 protected void doPost(HttpServletRequest request,
   HttpServletResponse response) throws ServletException, IOException {
  request.setCharacterEncoding("utf-8");
  response.setContentType("text/html;charset=utf-8");
  String username = request.getParameter("username");
  String password = request.getParameter("password");
  System.out.println("用户名:" + username + ",密码:" + password);
  PrintWriter out = response.getWriter();
  if ("admin".equals(username) && "123".equals(password)) {
   out.print(username + ",您登录成功");
  } else {
   out.print(username + ",登录失败。。");
  }
 }
}
客户端
public class Demo01_HttpUrlConnection {
public static void main(String[] args) {
String baseUrl = "https://www.baidu.com/img/bd_logo1.png";
InputStream inputStream = null;
FileOutputStream fos = null;
HttpURLConnection connection = null;
URL url = new URL(baseUrl);
connection = (HttpURLConnection) url.openConnection();
connection.setRequestMethod("GET");
connection.setConnectTimeout(5000);
connection.setDoOutput(true);
connection.connect();
int code = connection.getResponseCode();
System.out.println("状态码:" + code);
if (code == HttpURLConnection.HTTP_OK) {
inputStream = connection.getInputStream();
String fileName = baseUrl
.substring(baseUrl.lastIndexOf("/") + 1);
File file = new File("c:\\copy", fileName);
fos = new FileOutputStream(file);
byte[] buf = new byte[1024];
int len = 0;
while ((len = inputStream.read(buf)) != -1) {
fos.write(buf, 0, len);
}
System.out.println("图片下载完毕。。");
}

}
}

}
jdbc使用        
package com.qianfeng.jdbc;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
public class Demo_02_JDBC_Select {
 public static void main(String[] args) throws Exception {
  // 查询
  // step1:加载驱动
  Class.forName("org.sqlite.JDBC");
  // step2:提供url参数
  String dbUrl = "jdbc:sqlite:/c:/pro/sql.db";
  // step3:获取连接
  Connection conn = DriverManager.getConnection(dbUrl);
  // step4:创建传送器对象,提供sql语句,年龄大于20岁的学生信息
  String sql = "select name ,id,age,sex,classno from student where age > ? and name like ?;";// sql语句不完整
  PreparedStatement pstmt = conn.prepareStatement(sql);// 传送器对象pstmt
  // step5:需要为?赋值,然后再执行
  pstmt.setString(2, "%o%");
  pstmt.setInt(1, 19);
  // resultSet:存储查询后的结果
  ResultSet resultSet = pstmt.executeQuery();
  // step6:处理结果,从resultSet获取数据
  System.out.println("ID\t姓名\t年龄\t性别\t班级编号");
  while (resultSet.next()) {
   // 获取数据
   int id = resultSet.getInt(2);
   String name = resultSet.getString("name");
   int age = resultSet.getInt("age");
   String sex = resultSet.getString(4);
   int classno = resultSet.getInt("classno");
   System.out.println(id + "\t" + name + "\t" + age + "\t" + sex
     + "\t" + classno);
  }
  // step7:关闭
  conn.close();
 }
}
Jdbc添加元素
public static void main(String[] args) throws ClassNotFoundException, SQLException {
  Class.forName("org.sqlite.JDBC");// 用于完成通过java代码,向数据库中插入一条语句// step1:通过反射,加载驱动包
  System.out.println("加载成功");
  String dbUrl = "jdbc:sqlite:/c:/pro/sql.db";// step2:提供连接的参数:url:数据库的位置,用户名,密码
  Connection conn =conn = DriverManager.getConnection(dbUrl);// 得到连接的对象// step3:从DriverManager驱动管理类对象中获取连接
  System.out.println("连接:" + conn);
  String sql = "insert into student (id,name,age,sex,classno) values(11,'rose2',20,'female',2);";
  // 从连接conn中获取一个传送器对象// step4:提供sql语句,并用传送器传递到数据库中
  Statement stmt = conn.createStatement();// 从conn中创建一个传送器对象,用于传送sql语句
  int flag = 0;// step5:执行sql语句
   flag = stmt.executeUpdate(sql);// stmt传送器,将sql语句,送入数据库,并执行
  if (flag > 0) {// step6:处理结果
   System.out.println("插入成功");
  } else {
   System.out.println("插入失败。。。");
  }
  if (stmt != null) { // step7:关闭资源
    stmt.close();
  }
  if (conn != null) {
    conn.close();
  }
 }
}
修改元素
package com.qianfeng.jdbc;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
public class Demo_02_JDBC_Select {
 public static void main(String[] args) throws Exception {
  // 查询
  // step1:加载驱动
  Class.forName("org.sqlite.JDBC");
  // step2:提供url参数
  String dbUrl = "jdbc:sqlite:/c:/pro/sql.db";
  // step3:获取连接
  Connection conn = DriverManager.getConnection(dbUrl);
  // step4:创建传送器对象,提供sql语句,年龄大于20岁的学生信息
  String sql = "select name ,id,age,sex,classno from student where age > ? and name like ?;";// sql语句不完整
  PreparedStatement pstmt = conn.prepareStatement(sql);// 传送器对象pstmt
  // step5:需要为?赋值,然后再执行
  pstmt.setString(2, "%o%");
  pstmt.setInt(1, 19);
  // resultSet:存储查询后的结果
  ResultSet resultSet = pstmt.executeQuery();
  // step6:处理结果,从resultSet获取数据
  System.out.println("ID\t姓名\t年龄\t性别\t班级编号");
  while (resultSet.next()) {
   // 获取数据
   int id = resultSet.getInt(2);
   String name = resultSet.getString("name");
   int age = resultSet.getInt("age");
   String sex = resultSet.getString(4);
   int classno = resultSet.getInt("classno");
   System.out.println(id + "\t" + name + "\t" + age + "\t" + sex
     + "\t" + classno);
  }
  // step7:关闭
  conn.close();
 }
}


posted @ 2015-12-03 22:05  陈旭缘  阅读(234)  评论(0编辑  收藏  举报
这是什么