It is convenient to connect to the MySql server using Jdbc. First, import jdbc to the project or put jdbc into ClassPath. Here, I use Eclipse to directly import the jdbcjar File
It is convenient to connect to the MySql server using Jdbc. First, import jdbc to the project or put jdbc into ClassPath. Here, I use Eclipse to directly import the jdbc jar File
It is convenient to connect to the MySql server using Jdbc.
First, import jdbc to the project or put jdbc into ClassPath. Here, I use Eclipse to directly import the jdbc jar file.
Then, the DriverManager is developed, And the froName of the Class is directly completed using the simplest method. The Code is as follows:
Class. forName ("com. mysql. jdbc. Driver"). newInstance ();
Then, instantiate a Connection. Pay attention to the user name and password. There are several methods to choose from. Here I use the getConnection (String url, String user, String password) method of the DirverManager class. Specific Use: DriverManager
For example: Connection conn = DriverManager. getConnection ("jdbc: mysql: // localhost/mydatabase", "root", "1234 ");
Next, create a Statement for executing the SQL Statement. This is easy to do with one line of code:
Statement stat = conn. createStatement ();
Finally, you can use the stat instance to execute SQL statements. For details, refer to Statement.
Sample Code:
The created mydatabase contains a mytable table that contains an integer id and a text content.
Use the following code to view the content of the first 20 rows in mytable.