阅读453 返回首页    go 阿里云 go 技术社区[云栖]


天气预报,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

  上一篇:go mysql 临时表 创建和插入
  下一篇:go 使用StreamTokenizer统计文件的字符数