链接方法:
1、安装java(配置环境变量)
2、拷贝jar包
import java.sql.*;
class dbconnect
{
//链接db2
public static Connection connect(String url,String user,String pass)
{
Connection conn =null;
try
{
conn = DriverManager.getConnection(url,user,pass);
}catch(Exception e){
System.out.println(e.getMessage());}
return conn;
}
//关闭链接
public static void allclose(Connection conn,ResultSet rs,PreparedStatement ps)
{
if(conn != null) {
try {
conn.close();
} catch(Exception e) { e.printStackTrace();
}
try {
rs.close();
} catch(Exception e) { e.printStackTrace();
}
try {
ps.close();
} catch(Exception e) { e.printStackTrace();
}
conn = null;
}
}
public static void main(String[] args)
{
try
{
Class.forName("com.ibm.db2.jcc.DB2Driver").newInstance();
String url="jdbc:db2://127.0.0.1:50000/DBFLOW";
String user="db2admin";
String password="password";
Connection conn=null;
conn = connect(url,user,password);
if(conn !=null)
{
System.out.println("恭喜你链接成功!");
}
// your code
PreparedStatement ps=conn.prepareStatement("select * from OA_FLOWDAILYDATA");
ResultSet rs=ps.executeQuery();
while(rs.next())
{
System.out.println( "user_no="+rs.getString("FORMID"));
}
//end your code
allclose(conn,rs,ps);
System.out.println("恭喜你关闭成功!");
}catch(Exception sqle)
{
System.out.print(sqle);
}
}
}