1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79
| import java.sql.*;
public class MySQLClient {
public static void main(String[] args) {
int i;
Connection conn = null;
String url = "jdbc:mysql://localhost:3306/";
String db = "mabase";
String driver = "com.mysql.jdbc.Driver";
String user = "monuser";
String pass = "monpass";
try {
Class.forName(driver).newInstance();
} catch (Exception e) {
System.out.println(e.getMessage());
}
try{
System.out.println(url+db);
System.out.println(user);
System.out.println(pass);
conn = DriverManager.getConnection(url+db,user, pass);
String query = "SELECT * FROM maTable;";
Statement select = null;
try {
select = conn.createStatement();
try {
ResultSet result = select.executeQuery(query);
result.last();
int rowCount = result.getRow();
result.beforeFirst();
System.out.println("Retrieved " + rowCount + " row(s).\n");
i = 0;
while (result.next()) {
String name = result.getString("monChamp");
System.out.println(name);
i++;
}
String s = Integer.toString(i);
System.out.println(s);
// clean up
result.close();
conn.close();
} catch(Exception e) {
System.err.println("Mysql Statement Error: " + query);
e.printStackTrace();
}
} catch (Exception e1) {
System.out.println(e1.getMessage());
e1.printStackTrace();
}
}
catch(Exception sqle){
System.out.println("getConnectionError");
System.out.println(sqle.getMessage());
}
} // main()
} // MySQLclient |
Partager