0
connect to database
package exam; import java.sql.Connection; import java.sql.Driver; import java.sql.DriverManager; import java.sql.SQLException; public class test { private static final String URL = "jdbc:mysql:// localhost:3306/test2"; private static final String USERNAME = "root"; private static final String PASSWORD = ""; public static void main(String[] args) { Connection connection; try { connection = DriverManager.getConnection(URL,USERNAME,PASSWORD); System.out.println("connected"); } catch (SQLException e) { System.out.println("not connected"); e.printStackTrace(); } } } What is wrong???
1 ответ
0
Check for all the prerequisite before writing this code. SQL query here is just an example.
package exam;
import java.sql.Connection;
import java.sql.Driver;
import java.sql.DriverManager;
import java.sql.SQLException;
public class test {
public static void main(String[] args) {
String url = "jdbc:mysql:// localhost:3306/test2";
try {
Connection con= DriverManager.getConnection(url,"root","******");
Statement st = con.createStatement();
ResultSet rs = st.executeQuery("Select * from Employee");
} catch (SQLException e) {
System.out.println("not connected");
e.printStackTrace();
}
}
}