Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- JDBC Connector Issues
- import java.sql.Connection;
- import java.sql.DriverManager;
- import java.sql.ResultSet;
- import java.sql.SQLException;
- import java.sql.Statement;
- public class Main {
- public static void main(String args[]) {
- // Database credentials
- Connection conn = null;
- String url = "jdbc:mysql://localhost/";
- String db = "db";
- String driver = "com.mysql.jdbc.Driver";
- // Connect to database
- try {
- Class.forName(driver);
- System.out.println("HELLO");
- conn = DriverManager.getConnection(url+db,"root","");
- System.out.println("Success");
- } catch(Exception e) {
- return;
- }
- }
- }
- >java -cp .:./lib/driver.jar Main
- public class DBDiary {
- Connection conn;
- public DBDiary(){
- this.getConn();
- }
- public Connection getConn(){
- try {
- conn = getConnection();
- } catch (SQLException e) {
- System.out.println("Connection Couldnt be Obtained");
- }
- return conn;
- }
- public static Connection getConnection() throws SQLException {
- String drivers = "com.mysql.jdbc.Driver";
- String url = "jdbc:mysql://localhost:3306/test";
- String username = "root";
- String password = "root";
- System.setProperty(drivers,"");
- return DriverManager.getConnection(url,username,password);
- }
- }
Add Comment
Please, Sign In to add comment