How to connect to local host using JDBC?

前端 未结 2 1327
挽巷
挽巷 2021-01-06 07:30

I installed MySql on my own machine. I created database, create table, ... using MySql CommandLine Client. When working on a project in school, I connected to school\'s data

相关标签:
2条回答
  • 2021-01-06 08:09

    Simple Connection:

    import java.sql.Connection;
    import java.sql.DriverManager;
    
    public class Main {
      public static void main(String[] argv) throws Exception {
        String driverName = "org.gjt.mm.mysql.Driver";
        Class.forName(driverName);
    
        String serverName = "localhost";
        String mydatabase = "mydatabase";
        String url = "jdbc:mysql://" + serverName + "/" + mydatabase; 
    
        String username = "username";
        String password = "password";
        Connection connection = DriverManager.getConnection(url, username, password);
      }
    }
    
    0 讨论(0)
  • 2021-01-06 08:21

    It looks like you left your username and password in the source you posted.

    I don't see why you can't just do

    DriverManager.getConnection("jdbc:mysql://localhost/chandb", "user, "pass");
    
    0 讨论(0)
提交回复
热议问题