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();
}
}