您的位置:首页 > 数据库 > MySQL

天气预报,MYSQL之JDBC查询模块实现(代码)和简易插入模块实现(代码)以及PREPAREDSTATEMENT插入模块实现(代码)

2011-11-29 09:38 691 查看
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();
}
}

}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐