package mycom;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;
import com.mysql.jdbc.ResultSet;
public class testConnection {
public static void main(String[] args){
Connection conn = null;
try{
Class.forName("com.mysql.jdbc.Driver").newInstance();
conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/test","root","root");
if(conn==null)
System.out.println("Connection is null");
else
System.out.println("Connection = "+conn.toString());
java.sql.ResultSet rs = null;
Statement stmt = conn.createStatement();
rs = stmt.executeQuery("select * from country order by 1");
while(rs.next()){
System.out.print(rs.getString(1)+" | ");
System.out.println(rs.getString(2));
System.out.println("-----------------------------------");
}
}catch(SQLException e){
e.printStackTrace();
}catch (Exception e) {
e.printStackTrace();
}finally{
try {
conn.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
No comments:
Post a Comment