1、工具准备
常用的java环境,即eclipse,jdk等
mysql 5.6 数据库
当网上下载一个MySQL连接驱动:mysql-connector-java-5.1.27.jar
地址为:https://dev.mysql.com/downloads/connector/j/
2、下载后,包为zip压缩包,解压到任意位置,新建个java工程
3、右键点击javasql,选择build path,再选择add external Archives,将驱动包导入进来
4、随机创建个数据表,我这里用的是公司的测试库
username root
password test123
ip 10.1.60.23
port 3306
package sqldemo;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
public class sqldemo {
public String getusername()
{
Connection con;
String driver="com.mysql.jdbc.Driver";
String url="jdbc:mysql://10.1.60.62:3306/s61";
String username="root";
String password="test123";
String loginname=null;
try{
Class.forName(driver);
con=DriverManager.getConnection(url,username,password);
if (!con.isClosed())
System.out.println("Succeed conect database");
Statement statement=con.createStatement();
//String sql="select * from s61.t6110 where f01 in (select user_id from s61.t_user_capital_account where status="+"'YWC')";
//System.out.println(sql);
String sql="select * from s61.t6110 where f01=293418 ";
//String sql="select f02 from s61.t6110 where f01 in (select f02 from s61.t6101 where f03="+"'WLZH'"+ "and f06>100000 and f02 in (select user_id from s61.t_user_capital_account where status="+"'YWC'))";
ResultSet sr=statement.executeQuery(sql);
System.out.println("执行结果如下所示");
while (sr.next())
{
loginname=sr.getString("f02");
}
sr.close();
con.close();
}catch(ClassNotFoundException e){
System.out.println("sorry,not found sqlDriver");;
e.printStackTrace();
}catch(SQLException e){
e.printStackTrace();
}catch(Exception e){
e.printStackTrace();
}finally{
System.out.println("数据查询成功");
}
return loginname;
}
}
查询结果为 :