安卓jDBC连接SQL

 

package com.sql.url;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;

import android.util.Log;

import com.mysql.jdbc.Connection;
import com.user.com.canshu;
public class url {
	  public static String  jdbDrver="com.mysql.jdbc.Driver";
	  public static String  jdbsqlname="";//数据库的名称
	  public static String jdbcurl="jdbc:mysql:" +
	"//120.78.100.103:3300/"+jdbsqlname+"?characterEncoding=UTF-8";
	  public static String  jdbname="root";
	  public static String  jdbpwd="123456";
	  Connection conn=null;
	  ResultSet rs =null;
	  private ArrayList<String> liname;
	  private ArrayList<String> lipwd;
	  static Statement st=null;
	 
	public void onClickQuery()  
	  {  
	 //在android中操作数据库最好在子线程中执行,否则可能会报异常  
	    new Thread()  
	    {  
	      public void run() {  
	        try {  
	       //注册驱动  
	           Class.forName(jdbDrver);  
	            conn = (Connection) DriverManager.getConnection(jdbcurl, jdbname, jdbpwd);  
	            st = (Statement) conn.createStatement();  
	         //   查询所有
	            String sql = "select * from user";  
	            rs = st.executeQuery(sql);  
	          while (rs.next()) {  
	            Log.v("yzy", "第一项-->"+rs.getString(1)+"  第二项-->"+rs.getString(2));  
	          }
	          st.close();  
	  		conn.close();
	  		rs.close();
	          Log.v("yzy", "链接成功!");  
	        }catch(ClassNotFoundException e)  
	        {  
	          Log.v("yzy", "链接失败!"+"  "+e.getMessage());  
	        } catch (SQLException e)  
	        {  
	          Log.v("yzy", "链接失败2!"+"  "+e.getMessage());  
	        }  
	      };  
	    }.start();  
	  }  
	public void clossA(Statement st,Connection conn,ResultSet rt){
	 try {
		st.close();  
		conn.close();
		rs.close();
	} catch (SQLException e) {
		// TODO Auto-generated catch block
		Log.v("yzy——close", "关闭数据库失败!");
		e.printStackTrace();
	}  
	}
	  /**
	   * 链接数据库
	   */
	  public void sqljdbc()
	  {
			try {
				Class.forName(jdbDrver);//链接
				conn=(Connection) DriverManager.getConnection(jdbcurl,jdbname,jdbpwd);
				System.out.println("链接"+jdbsqlname+"数据库成功!");
			} catch (Exception e) {
				System.out.println("链接jdbsqlname失败");
				e.printStackTrace();
			}
		 
	  }
	  /**
	     * 查询记录是否存在
	     * 
	     * @param name
	     *           存在 0,错误或 不存在1
	     */
	  private static Connection getSQLConnection()
		{
			Connection con = null;
			try
			{
				Class.forName("net.sourceforge.jtds.jdbc.Driver");  
				con = (Connection) DriverManager.getConnection(jdbcurl, jdbname, jdbpwd);
			} catch (ClassNotFoundException e)
			{
				e.printStackTrace();
			} catch (SQLException e)
			{
				e.printStackTrace();
			}
			return con;
		}
	  public int ChaXun(final String name,final String upwd){
		  int abc=1;
		  Connection conn = getSQLConnection();
		  String sql="SELECT  * FROM user WHERE uname='"+name+"'"+"and upwd="+"'"+upwd+"'";
		  Statement stmt;
		try {
			stmt = conn.createStatement();
			ResultSet rs = stmt.executeQuery(sql);
			if (rs.next())
			{
				abc=0;
				Log.v("yzy——cha","查询成功》》》不存在!"+abc);	
			}else{
				abc=1;
				Log.v("yzy——cha","查询成功》》》不存在!"+abc);	
				}
			rs.close();
			stmt.close();
			conn.close();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			 abc=1;
				Log.v("yzy——cha", "查询错误!"+abc);
			e.printStackTrace();
		}
		  return abc;
	  }
	  /**
	   * 查询所有教练
	   * */
		public List<canshu> Chaxunjl_jl() throws InterruptedException
		   {
			final List<canshu> ALL_jl=new ArrayList<canshu>();
			   Thread th=new Thread(){
				   public void run(){
					   try {
					    Class.forName(jdbDrver);
					    conn = (Connection) DriverManager.getConnection(jdbcurl, jdbname, jdbpwd);
				 		String sql="SELECT  * FROM tchaer ";
				 		st=conn.createStatement();
				        rs= st.executeQuery(sql);
				 		Log.i("查询语句", sql+"<<<<<<<<");
				 		 while (rs.next()) {
				 		 canshu jl_jl=new canshu();
				 		 jl_jl.setJl_name(rs.getString("tname"));
				 		 jl_jl.setJl_map(rs.getString("tmap"));
				 		 jl_jl.setJl_tph(rs.getString("tph"));
				 		 jl_jl.setJl_img(rs.getString("timg"));
				 		 ALL_jl.add(jl_jl);
				 		 }
				 		 Log.v("所有教练查询》》》》》","查询成功》》》》存在!"+ALL_jl);
				 	    st.close();  
				  		conn.close();
				  		rs.close();
					   }catch (SQLException e){ 
						   Log.v("所有教练查询》》》》查询错误!查询错误!", "查询错误!"+ALL_jl);
					        } catch (ClassNotFoundException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}  
					      }
			   };
			th.start();
			th.run();
			th.sleep(2000);
			return  ALL_jl;
			  }
		 
	  /**
	     * 查询记录是否存在
	     * 
	     * @param name
	     *           存在 0,错误或 不存在1
	     */
	 int ab;
	public int Chaxunlog(final String name,final String upwd) throws InterruptedException
	   {
		   Thread th=new Thread(){
			   public void run(){
				   try {
				    Class.forName(jdbDrver);
				    conn = (Connection) DriverManager.getConnection(jdbcurl, jdbname, jdbpwd);
			 		String sql="SELECT  * FROM user WHERE uph='"+name+"'"+"and upwd="+"'"+upwd+"'";
			 		st=conn.createStatement();
			        rs= st.executeQuery(sql);
			 		Log.i("查询语句", sql+"<<<<<<<<");
			 	  if(!rs.next())
			 	  {
			 		 ab=1;
			 		 Log.v("yzy——cha","查询成功》》》不存在!"+ab);
			 	  }else
			 	  { 
			 		 ab=0;
			 		 Log.v("yzy——cha","查询成功》》》》存在!"+ab);
			 	  }
			 	    st.close();  
			  		conn.close();
			  		rs.close();
				   }catch (SQLException e){ 
				        	     ab=1;
								Log.v("yzy——cha", "查询错误!"+ab);
				        } catch (ClassNotFoundException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}  
				      }
		   };
		th.start();
		th.run();
		th.sleep(2000);
		int bb=ab;
		return bb;
		  }
	 
	/**
		   * 注册添加数据
		   * @param id
		   * @return
		   */
			public void loganddate(final String uph,final String uname,final String upwd,final int eag,final int unameimg)  
			  {  
			 //在android中操作数据库最好在子线程中执行,否则可能会报异常  
			    new Thread()  
			    {  
			      public void run() {  
			        try {  
			       //注册驱动  
			           Class.forName(jdbDrver);  
			            conn = (Connection) DriverManager.getConnection(jdbcurl, jdbname, jdbpwd);  
			            st=conn.createStatement(); 
			         //   添加数据
			            String sql = "insert into user(uph,uname,upwd,eag,unameimg) values('"+uph+"','"+uname+"','"+upwd+"'"+","+eag+","+unameimg+")";  
			            st.executeUpdate(sql);
			            st.close();  
			    		conn.close();
			          Log.v("yzy——and", "添加成功!");  
			        }catch(ClassNotFoundException e)  
			        {  
			          Log.v("yzy——and", "添加失败!"+"  "+e.getMessage());  
			        } catch (SQLException e)  
			        {  
			          Log.v("yzy——and", "添加失败2!"+"  "+e.getMessage());  
			        }  
			      }; 
			    }.start();  
			  }
			 
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

加金开发

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值