String url = "你的url地址";
在海南州等地區(qū),都構(gòu)建了全面的區(qū)域性戰(zhàn)略布局,加強(qiáng)發(fā)展的系統(tǒng)性、市場(chǎng)前瞻性、產(chǎn)品創(chuàng)新能力,以專注、極致的服務(wù)理念,為客戶提供做網(wǎng)站、網(wǎng)站制作 網(wǎng)站設(shè)計(jì)制作按需定制網(wǎng)站,公司網(wǎng)站建設(shè),企業(yè)網(wǎng)站建設(shè),品牌網(wǎng)站設(shè)計(jì),成都營(yíng)銷網(wǎng)站建設(shè),成都外貿(mào)網(wǎng)站建設(shè),海南州網(wǎng)站建設(shè)費(fèi)用合理。
URL uri = new URL(url);
HttpURLConnection conn = (HttpURLConnection) uri.openConnection();
conn.setRequestProperty("Accept-Charset", "utf-8");
conn.setRequestMethod("POST");
conn.setDoOutput(true);
conn.setDoInput(true);
conn.setUseCaches(false);
conn.setConnectTimeout(90000);
conn.setReadTimeout(180000);
conn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
OutputStreamWriter osw = new OutputStreamWriter(conn.getOutputStream(), "UTF-8");
這個(gè)過(guò)程就是請(qǐng)求url跳轉(zhuǎn)的,一般都是post方式,當(dāng)然,這個(gè)可以自己選擇get也可以,osw就是請(qǐng)求返回的數(shù)據(jù)了
如果是在局域網(wǎng)中測(cè)試,本地的話就是127.0.0.1或localhost,或改為局域網(wǎng)中的其它IP地址,如果是廣域網(wǎng)的話,那就得搭建服務(wù)器,就是不搭建也得有一個(gè)固定的外網(wǎng)IP才行。
用這個(gè)類吧.好的話,給我加加分.
import java.sql.*;
/**
* @功能: 一個(gè)JDBC的本地化API連接類,封裝了數(shù)據(jù)操作方法,只用傳一個(gè)SQL語(yǔ)句即可
* @作者: 李開(kāi)歡
* @日期: 2007/
*/
public class ConnectionDemo {
/*
* 這里可以將常量全部放入另一個(gè)類中,以方便修改
*/
private static Connection conn;
private static Statement ps;
private static ResultSet rs;
private static final String DRIVER = "com.microsoft.jdbc.sqlserver.SQLServerDriver";
private static final String URL = "jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=mydb";
private static final String USER ="sa";
private static final String PASS = "sa";
public ConnectionDemo() {
// TODO Auto-generated constructor stub
ConnectionDemo.getConnection();
}
public static Connection getConnection(){
System.out.println("連接中...");
try {
Class.forName(ConnectionDemo.DRIVER);
conn = DriverManager.getConnection(ConnectionDemo.URL, ConnectionDemo.USER, ConnectionDemo.PASS);
System.out.println("成功連接");
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return conn;
}
public static Statement getStatement(String sql){
System.out.println("執(zhí)行SQL語(yǔ)句中...");
try {
ps = conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_UPDATABLE);
if(sql.substring(0, 6).equals("select")){
rs = ps.executeQuery(sql);
System.out.println("執(zhí)行完查詢操作,結(jié)果已返回ResultSet集合");
}else if(sql.substring(0, 6).equals("delete")){
ps.executeUpdate(sql);
System.out.println("已執(zhí)行完畢刪除操作");
}else if(sql.substring(0, 6).equals("insert")){
ps.executeUpdate(sql);
System.out.println("已執(zhí)行完畢增加操作");
}else{
ps.executeUpdate(sql);
System.out.println("已執(zhí)行完畢更新操作");
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return ps;
}
public static ResultSet getResultSet(){
System.out.println("查詢結(jié)果為:");
return rs;
}
public static void closeConnection(){
System.out.println("關(guān)閉連接中...");
try {
if (rs != null) {
rs.close();
System.out.println("已關(guān)閉ResultSet");
}
if (ps != null) {
ps.close();
System.out.println("已關(guān)閉Statement");
}
if (conn != null) {
conn.close();
System.out.println("已關(guān)閉Connection");
}
} catch (Exception e) {
// TODO: handle exception
}
}
public static void main(String[] args) {
// TODO Auto-generated method stub
ConnectionDemo.getConnection();
String sql = "delete from type where id = 1";
ConnectionDemo.getStatement(sql);
String sql2 = "insert into type values(1,'教學(xué)設(shè)備')";
ConnectionDemo.getStatement(sql2);
String sql1 = "select * from type";
ConnectionDemo.getStatement(sql1);
ResultSet rs = ConnectionDemo.getResultSet();
System.out.println("編號(hào) "+"類 型");
try {
while(rs.next()){
System.out.print(" "+rs.getInt(1)+" ");
System.out.println(rs.getString(2));
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
ConnectionDemo.closeConnection();
}
}