Answer the question
In order to leave comments, you need to log in
Answer the question
In order to leave comments, you need to log in
an example of how to work with the database:
package news_parser_connection;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
public class LocalhostDBConnection {
public static void main(String args[]) {
Connection connection;
try {
// Название драйвера
String driverName = "com.mysql.jdbc.Driver";
Class.forName(driverName);
// Create a connection to the database
String serverName = "localhost";
String mydatabase = "db_name";
String url = "jdbc:mysql://" + serverName + "/" + mydatabase;
String username = "root";
String password = "root";
connection = DriverManager.getConnection(url, username, password);
System.out.println("is connect to DB" + connection);
String query = "Select * FROM news";
Statement stmt = connection.createStatement();
ResultSet rs = stmt.execute(query);
String dbtime;
while (rs.next()) {
dbtime = rs.getString(1);
System.out.println(dbtime);
} // end while
connection.close();
} // end try
catch (ClassNotFoundException e) {
e.printStackTrace();
// Could not find the database driver
} catch (SQLException e) {
e.printStackTrace();
// Could not connect to the database
}
}
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question