2
java 클래스를 호출하는 작은 쉘 스크립트를 작성했습니다.이 스크립트는 mysql db를 호출하고 일부 결과 집합을 반환합니다.쉘 스크립트에서 mysql jar를로드하고 MYSQL DB에 연결하는 방법
내 쉘 Scipt :
CP="."
CP=$CP:/home/lib/mysql-connector-java-5.1.12-bin.jar
export CP
java -cp $CP com.util.Hello
내 자바 코드는 다음과 같다,
Hello.Java :
public static void main(String[] str) {
Connection conn = null;
try
{
String userName = "root";
String password = "root";
String url = "jdbc:mysql://localhost:3306/test";
Class.forName ("com.mysql.jdbc.Driver").newInstance();
conn = DriverManager.getConnection (url, userName, password);
System.out.println ("Database connection established");
Statement s = conn.createStatement();
s.executeQuery ("select * from users");
ResultSet rs = s.getResultSet();
int count = 0;
while (rs.next())
{
System.out.println (count + " rows were retrieved");
count++;
}
rs.close();
s.close();
}
catch (Exception e) {
System.err.println ("Cannot connect to database server")
}
}
나는 아래에 무엇입니까 리눅스 서버에서 쉘 스크립트를 실행할 때 오류,
내 Java 클래스에서 인쇄 한 데이터베이스 서버에 연결할 수 없습니다.
System.err.println ("Cannot connect to database server")
는 일부는 쉘 스크립트에서 MySQL의 항아리를로드하는 방법에 나를 도울 수 있습니까?
내 Java 클래스를 실행하는 동안 어떻게 사용합니까?
이 클래스를 war 파일에서 실행하면 작동합니다.
미리 감사드립니다.
예외를 포착하지 않고 버린 경우 문제가 무엇인지 쉽게 알 수 있습니다. 이 작업을 수행. try/catch 블록을 제거하고 "throws Exception"을 주 메서드 선언에 추가하고 다시 실행 한 다음 전체 스택 추적 (오류 메시지)을 게시합니다. – Keith
또한 'newInstance()'부분을 제거하십시오. 다음을 사용하여 클래스를로드하면됩니다. Class.forName ("com.mysql.jdbc.Driver"); – Aviram