java jdbc map_Java基础--JDBC-HashMap

Java基础--JDBC-HashMap

package com.JDBCTest;

import java.sql.Connection;

import java.sql.DriverManager;

import java.sql.PreparedStatement;

import java.sql.ResultSet;

import java.util.ArrayList;

import java.util.HashMap;

import java.util.List;

public class QuerySession {

private static final String url = "jdbc:mysql://119.254.106.50:3309/test?useUnicode=true&characterEncoding=utf8";

private static final String user = "rw_all_db";

private static final String password = "rw_all_db";

/**

* 获取连接

*/

public static Connection getConnection() {

try {

Class.forName("com.mysql.jdbc.Driver");//静态代码块实例化一个Driver对象

Connection connection = DriverManager.getConnection(url, user, password);

return connection;

} catch (Exception e) {

e.printStackTrace();

return null;

}

}

public static void closeConn(Connection connection,PreparedStatement statement,ResultSet rs) {

try {

if(rs!=null)rs.close();

if(statement!=null)statement.close();

if(connection!=null)connection.close();

} catch (Exception e) {

e.printStackTrace();

}

}

public static List> findData(String sql) {

Connection connection = null;

PreparedStatement statement = null;

ResultSet rs = null;

List> users = null;

try {

connection = getConnection();

statement = connection.prepareStatement(sql);

rs = statement.executeQuery();

users = new ArrayList<>();

HashMap hashMap = null;

while (rs.next()) {

hashMap = new HashMap<>();

hashMap.put("id", rs.getInt("id"));

hashMap.put("username", rs.getString("username"));

hashMap.put("address", rs.getString("address"));

hashMap.put("birthday", rs.getString("birthday"));

users.add(hashMap);

}

return users;

} catch (Exception e) {

e.printStackTrace();

return null;

} finally{

closeConn(connection, statement, rs);

}

}

public static List> searchData(String sql2,String address,String username) {

Connection connection = null;

PreparedStatement statement = null;

ResultSet rs = null;

List> users = null;

try {

connection = getConnection();

statement = connection.prepareStatement(sql2);

statement.setString(1, address);

statement.setString(2, "%"+username+"%");

rs = statement.executeQuery();

users = new ArrayList<>();

HashMap hashMap = null;

while (rs.next()) {

hashMap = new HashMap<>();

hashMap.put("id", rs.getInt("id"));

hashMap.put("username", rs.getString("username"));

hashMap.put("address", rs.getString("address"));

hashMap.put("birthday", rs.getString("birthday"));

users.add(hashMap);

}

return users;

} catch (Exception e) {

e.printStackTrace();

return null;

} finally{

closeConn(connection, statement, rs);

}

}

public static HashMap findDataPK(String sql1,Integer id) {

Connection connection = null;

PreparedStatement statement = null;

ResultSet rs = null;

try {

connection = getConnection();

statement = connection.prepareStatement(sql1);

statement.setInt(1, id);

rs = statement.executeQuery();

HashMap hashMap = null;

if (rs.next()) {

hashMap = new HashMap<>();

hashMap.put("id", rs.getInt("id"));

hashMap.put("username", rs.getString("username"));

hashMap.put("address", rs.getString("address"));

hashMap.put("birthday", rs.getString("birthday"));

}

return hashMap;

} catch (Exception e) {

e.printStackTrace();

return null;

} finally{

closeConn(connection, statement, rs);

}

}

public static void main(String[] args) {

//String sql = "SELECT id,username,address,birthday FROM user;";

//List> users = findData(sql);

//for (HashMap hashMap : users) {

//System.out.println(hashMap.get("id")+"=>"+hashMap.get("username")+"=>"+hashMap.get("address")+"=>"+hashMap.get("birthday"));

//}

//

//String sql1 = "SELECT id,username,address,birthday FROM user where id = ?";

//HashMap hashMap = findDataPK(sql1, 3);

//if (hashMap != null) {

//System.out.println(hashMap.get("id")+"=>"+hashMap.get("username")+"=>"+hashMap.get("address")+"=>"+hashMap.get("birthday"));

//}

String sql2 = "SELECT id,username,address,birthday FROM user where address = ? and username like ?";

List> users = searchData(sql2, "北京", "a");

for (HashMap hashMap : users) {

System.out.println(hashMap.get("id")+"=>"+hashMap.get("username")+"=>"+hashMap.get("address")+"=>"+hashMap.get("birthday"));

}

}

}

package com.MangoMap; //import android.app.Activity; import com.google.android.maps.MapActivity; import android.content.Intent; import android.content.pm.PackageManager; import android.content.pm.ResolveInfo; import android.os.Bundle; import android.view.View; import android.widget.ListView; import android.widget.SimpleAdapter; import java.text.Collator; import java.util.ArrayList; import java.util.Collections; import java.util.Comparator; import java.util.HashMap; import java.util.List; import java.util.Map; //MapView Com import com.google.android.maps.MapView; import com.google.android.maps.MapController; import com.google.android.maps.GeoPoint; //import com.google.android.maps. public class MangoMap extends MapActivity { /** Called when the activity is first created. */ private MapView mpv; private MapController mpc; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); //地图 mpv = (MapView) findViewById(R.id.map); mpv.setTraffic(true); mpv.setClickable(true); mpv.setScrollContainer(true); mpc = mpv.getController(); GeoPoint gp = new GeoPoint((int) (23.05320 * 1000000), (int) (113.155550 * 1000000)); //地理坐标 mpc.animateTo(gp); mpc.setZoom(15); //mpc. // } @Override protected boolean isRouteDisplayed() { return false; } } //----------------- <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" > <!-- <TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="@string/hello" /> --> <com.google.android.maps.MapView android:id="@+id/map" android:layout_width="fill_parent" android:layout_height="fill_parent" android:enabled="true" android:clickable="true" android:apiKey="YOUR API key" /> <Button android:text="@+id/Button01" android:id="@+id/Button01" android:layout_width="wrap_content" android:layout_height="wrap_content"></Button> </LinearLayout> //--------------------- <?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.MangoMap" android:versionCode="1" android:versionName="1.0"> <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" /> <uses-permission android:name="android.permission.INTERNET" /> <application android:icon="@drawable/icon" android:label="@string/app_name"> <uses-library android:name="com.google.android.maps" /> <activity android:name=".MangoMap" android:label="@string/app_name"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> </application> <uses-sdk android:minSdkVersion="3" /> </manifest>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值