天天看點

android 連接配接資料庫(sql server)

最近做了一個項目,客戶有個需求,沒有背景服務的情況下直接連接配接資料庫;

多的不說了,直接上代碼

public static Connection getSQLConnection(String ip, String user, String pwd, String db)
	    {
	        Connection con = null;
	        String portsNum=StaticParams.dataBasePortNumber;
	        try
	        {
	            Class.forName("net.sourceforge.jtds.jdbc.Driver");
	            con = DriverManager.getConnection("jdbc:jtds:sqlserver://" + ip + ":"+portsNum+"/" + db, user, pwd);
	        } catch (ClassNotFoundException e)
	        {
	            e.printStackTrace();
	           
	            con = null;
	        } catch (SQLException e)
	        {
	        	con = null;
	            e.printStackTrace();
	            
	        }
	        return con;
	    }
           

插入

public static String insertAppNumberCodeData(String OutCheckNote){
						 String result = "";
						Connection conn = null;
						PreparedStatement ps = null;
						try{
							conn = DBUtil.getSQLConnection(StaticParams.serviceIp, StaticParams.loginName, StaticParams.dataBasePwd, StaticParams.dataBaseName);
					      
							String sql = "insert into Result (ID ,Item,Note)values(103 ,0,?)";
					        ps = conn.prepareStatement(sql);//
					        ps.setString(1, Note);
					        int low = ps.executeUpdate();
					        String strMessage="";
					        int intStatus=0;
					        if(low>0){
					        	strMessage="插入成功!";
					        	intStatus=200;
					        }else{
					        	strMessage="插入失敗!";
					        	intStatus=500;
					        }
					      
				           ps.close();
				           conn.close();
						}catch(Exception e){
							e.printStackTrace();
						
//							flage = 0;
						}finally{
							
//				           try {
//								ps.close();
//							} catch (SQLException e) {
//								// TODO Auto-generated catch block
//								e.printStackTrace();
//							}
//				           try {
//								conn.close();
//							} catch (SQLException e) {
//								// TODO Auto-generated catch block
//								e.printStackTrace();
//							}
//							
						}
						
						return result;
						
					}
           

修改

public static String updateWholeStationStates(int OutCheckItemID){
			 String result = "";
			Connection conn = null;
			PreparedStatement ps = null;
			try{
				conn = DBUtil.getSQLConnection(StaticParams.serviceIp, StaticParams.loginName, StaticParams.dataBasePwd, StaticParams.dataBaseName);
				
				String sql = "update Result set ItemID=? where SID=101";
		        ps = conn.prepareStatement(sql);//
		        ps.setInt(1, ItemID);
		        int low = ps.executeUpdate();
		        String strMessage="";
		       
		        if(low>0){
		        	strMessage="修改成功!";
		        	intStatus=200;
		        }else{
		        	strMessage="修改失敗!";
		        	intStatus=500;
		        }
		        StationNumBean bean=new StationNumBean();
				bean.setNum(OutCheckItemID);
				bean.setStatus(intStatus);
				bean.setMessage(strMessage);
				result = GsonUtil.createGsonString(bean);

	           ps.close();
	           conn.close();
			}catch(Exception e){
				e.printStackTrace();
			
//				flage = 0;
		           
			}finally{
				
//	           try {
//					ps.close();
//				} catch (SQLException e) {
//					// TODO Auto-generated catch block
//					e.printStackTrace();
//				}
//	           try {
//					conn.close();
//				} catch (SQLException e) {
//					// TODO Auto-generated catch block
//					e.printStackTrace();
//				}
				
			}
			
			return result;
			
		}
	
           

查詢

public static String queryCarInfoDaoData(){
			 String result = "";
			Connection conn = null;
			Statement stmt = null ;
			ResultSet rs = null ;
			try{
				conn = DBUtil.getSQLConnection(StaticParams.serviceIp, StaticParams.loginName, StaticParams.dataBasePwd, StaticParams.dataBaseName);
				
				String sql = "select * from Result where SID=100 order by ID desc";
		        stmt = conn.createStatement();//
		        rs = stmt.executeQuery(sql);
		        OutCheckNoteDomBean IBean=new OutCheckNoteDomBean();
	           List<OutCheckNoteDomBean.OutCheckNoteBean> listBean=new ArrayList<>();
		        while (rs.next())
	            {
	            	OutCheckNoteDomBean.OutCheckNoteBean noteBean=new OutCheckNoteDomBean.OutCheckNoteBean();
	            	noteBean.setID(rs.getInt("ID"));
	            	noteBean.setFID(rs.getInt("SID"));
	            	noteBean.setOutCheckItemID(rs.getInt("ItemID"));
	            	noteBean.setOutCheckNote(rs.getString("Note"));
//	            	IBean.getResultBean().add(noteBean);
	            	listBean.add(noteBean);
//	            	result = GsonUtil.createGsonString(noteBean);
	            }
		        IBean.setResult(listBean);
		        IBean.setStatus(200);
		        result = GsonUtil.createGsonString(IBean);
//				result = GsonUtil.createGsonString(IBean);
		        rs.close();
	            stmt.close();
	            conn.close();
			}catch(Exception e){
				e.printStackTrace();
				
//				flage = 0;
			}finally{
//				try {
//					rs.close();
//				} catch (SQLException e) {
//					// TODO Auto-generated catch block
//					e.printStackTrace();
//				}
//	            try {
//					stmt.close();
//				} catch (SQLException e) {
//					// TODO Auto-generated catch block
//					e.printStackTrace();
//				}
//	            try {
//					conn.close();
//				} catch (SQLException e) {
//					// TODO Auto-generated catch block
//					e.printStackTrace();
//				}
				
			}
			
			return result;
			
		}