Answer the question
In order to leave comments, you need to log in
Is the application making queries to the database correctly?
Hello!
There is a java program that connects to a MySQL database via JDBC.
Creating a connection is hardly any different from 98% of implementations:
private static Connection createConnection() {
try {
Class.forName(controllerDB);
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
Connection connection = null;
try {
connection = DriverManager
.getConnection(dbURL,db_user, db_password);
} catch (SQLException e) {
e.printStackTrace();
}
if (connection != null) {
return connection;
} else {
return null;
}
}
public static String[] one() throws SQLException {
Statement statement;
ResultSet result;
Random random = new Random();
int id = ((random.nextInt(max_number) + 1));
String sId = Integer.toString(id);
Connection conn = createConnection();
String sql = "SELECT * FROM table WHERE ID=" + sId;
String[] arr = new String[10];
statement = conn.createStatement();
result = statement.executeQuery(sql);
while (result.next()){
arr[0] = result.getString("string");
arr[1] = result.getString("string");
arr[2] = result.getString("string");
arr[3] = result.getString("string");
arr[4] = result.getString("string");
arr[5] = result.getString("string");
arr[6] = result.getString("string");
arr[7] = result.getString("string");
arr[8] = result.getString("string");
arr[9] = result.getString("string");
}
statement.close();
conn.close();
return arr;
}
static void two(int num1, String str1, String str2) throws SQLException {
String insertTableSQL = "INSERT ... " +
"VALUES (?, ?, ?)"
+ "ON DUPLICATE KEY UPDATE"
+ ".. .";
Connection conn = createConnection();
PreparedStatement insert = conn.prepareStatement(insertTableSQL);
insert.setString(1, str1);
insert.setInt(2, num1);
insert.setString(3, str2);
try {
insert.executeUpdate();
} catch (SQLException e) {
e.printStackTrace();
}
insert.close();
conn.close();
}
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question