天氣預報,MYSQL之JDBC查詢模塊實現(代碼)和簡易插入模塊實現(代碼)以及PREPAREDSTATEMENT插入模塊實現(代碼)
package com.zzk.cn;
import java.sql.*;
public class Insert {
public static void main(String[] args) {
System.out.println("HelloWorld");
insertMysql();
}
public static void insertMysql() {
ResultSet rs = null;
Statement stmt = null;
Connection conn = null;
try {
System.out.println("HelloWorld!");
Class.forName("com.mysql.jdbc.Driver");
// new oracle.jdbc.driver.OracleDriver;
conn=DriverManager.getConnection("jdbc:mysql://10.1.101.223:3306/weather", "appuser", "opzoon123!");
stmt = conn.createStatement();
rs = stmt.executeQuery("select * from weather_info");
while (rs.next()) {
System.out.println(rs.getString("weather_date"));
System.out.println(rs.getInt("city_id"));
}
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
} finally {
try {
if (rs != null) {
rs.close();
rs = null;
}
} catch (SQLException e) {
e.printStackTrace();
}
try {
if (stmt != null) {
stmt.close();
stmt = null;
}
} catch (SQLException e) {
e.printStackTrace();
}
try {
if (conn != null) {
conn.close();
conn = null;
}
} catch (SQLException e) {
e.printStackTrace();
}
}
}
}
package com.zzk.cn;
import java.sql.*;
public class Insert {
public static void main(String[] args) {
System.out.println("HelloWorld");
insertMysql();
}
public static void insertMysql() {
ResultSet rs = null;
Statement stmt = null;
Connection conn = null;
try {
System.out.println("HelloWorld!");
Class.forName("com.mysql.jdbc.Driver");
// new oracle.jdbc.driver.OracleDriver;
conn=DriverManager.getConnection("jdbc:mysql://10.1.101.223:3306/weather", "appuser", "opzoon123!");
stmt = conn.createStatement();
String sql="insert into weather_info(city_id,weather_date) values (4,'1911.11.1')";
stmt.executeUpdate(sql);
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
} finally {
try {
if (rs != null) {
rs.close();
rs = null;
}
} catch (SQLException e) {
e.printStackTrace();
}
try {
if (stmt != null) {
stmt.close();
stmt = null;
}
} catch (SQLException e) {
e.printStackTrace();
}
try {
if (conn != null) {
conn.close();
conn = null;
}
} catch (SQLException e) {
e.printStackTrace();
}
}
}
}
package com.zzk.cn;
import java.sql.*;
public class Insert {
public static void main(String[] args) {
System.out.println("HelloWorld");
insertMysql();
}
public static void insertMysql() {
//ResultSet rs = null;
//Statement stmt = null;
Connection conn = null;
PreparedStatement pstmt=null;
try {
System.out.println("HelloWorld!");
Class.forName("com.mysql.jdbc.Driver");
// new oracle.jdbc.driver.OracleDriver;
conn=DriverManager.getConnection("jdbc:mysql://10.1.101.223:3306/weather", "appuser", "opzoon123!");
pstmt=conn.prepareStatement("insert into weather_info(city_id,weather_date,weather_description) values (?,?,?)");
pstmt.setInt(1, 3);
pstmt.setString(2, "你好嗎");
pstmt.setString(3, "我很好");
pstmt.executeUpdate();
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
} finally {
try {
if (pstmt !=null) {
pstmt.close();
pstmt=null;
}
}catch (SQLException e) {
e.printStackTrace();
}
try {
if (conn != null) {
conn.close();
conn = null;
}
} catch (SQLException e) {
e.printStackTrace();
}
}
}
}
最後更新:2017-04-02 06:52:12