# download and extract JDK
URL=http://download.oracle.com/otn-pub/java/jdk/8u25-b17/jdk-8u25-linux-x64.tar.gz
wget --continue --no-check-certificate --header "Cookie: oraclelicense=a" ${URL} -O /tmp/${URL##*/}
tar -C /var/lib/jenkins/tools/hudson.model.JDK/ -xzf /tmp/jdk-8u25-linux-x64.tar.gz
# configure JDK
http://192.168.1.111:8080/configure
JDK
JDK installations
Add JDK
Install automatically: check out
JAVA_HOME: /var/lib/jenkins/tools/hudson.model.JDK/jdk1.8.0_25
Save
# create New Item
Item name: JDBCTest
check "Freestyle project"
JDK: JDK8
Build > Execute shell > Command
javac JDBCTest.java && java JDBCTest
Save
Build Now
Upload / Create file on Jenkins machine:
# /var/lib/jenkins/jobs/JDBCTest/workspace/JDBCTest.java
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
public class JDBCTest {
public static void main(String[] args) {
String HOST = "db_host";
String DB = "db_name";
String USER = "user_name";
String PASSWORD = "user_pass";
System.out.println("java.version:" + System.getProperty("java.version"));
System.out.println("java.class.path:" + System.getProperty("java.class.path"));
Connection con = null;
Statement stmt = null;
ResultSet rs = null;
try{
Class.forName("com.mysql.jdbc.Driver");
con = DriverManager.getConnection("jdbc:mysql://" + HOST + ":3306/" + DB, USER, PASSWORD);
stmt = con.createStatement();
rs = stmt.executeQuery("SELECT table_name FROM INFORMATION_SCHEMA.TABLES");
while (rs.next()) {
System.out.println(rs.getString(1));
}
}catch(SQLException e){
e.printStackTrace();
}catch(ClassNotFoundException e){
e.printStackTrace();
}finally{
try{rs.close();}catch(Exception e){}
try{stmt.close();}catch(Exception e){}
try{con.close();}catch(Exception e){}
}
}
}