MySQL database written in Java jdbc connection

At this moment I am more exciting, because the connection database afternoon, finally succeeded, although it takes a long time, but ultimately successful, that feeling, you know. So I want to share my experience to everyone, I hope this can help you, take some detours.

This is a decent jdbc code haha

Attention, attention, attention, important things to say three times:

(1) // here you need to change is the name of the database, T2 is about to change your database name
(2) // insert into your table, the table is the author shen, the name change
(3) / / here is inserted, if the other statements is to change, otherwise it will error

jdbc to connect MySQL database

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

public class JDBC_Main {
	public static void main(String[] args) {
		try { 
			Class.forName("com.mysql.jdbc.Driver");
			String url = "jdbc:mysql://localhost:3306/T2?useUnicode=true&characterEncoding=utf-8&useSSL=false";
			**//需要更改的是此处你的数据库名,即将T2改为你的数据库名字**
			String user = "root";
			String password = "123qwe";
			Connection conn = DriverManager.getConnection(url, user, password);
			String sql = "insert into shen (name,age,birthday) values(?,?,?)";
			**//插入到你的表中,笔者的表是shen,将名字进行更改**
			PreparedStatement ps = conn.prepareStatement(sql);
			ps.setString(1, "李三");
			ps.setInt(2, 22);
			ps.setString(3, "男");
			ps.executeUpdate();
			**//此处是进行插入,如果是其他语句,是要更改的,否则就会报错**
			conn.close();
			System.out.println("MySQL插入完毕!!!");
		}
		catch (ClassNotFoundException e) {
			e.printStackTrace();
		}
		catch (SQLException e) {
			 e.printStackTrace();
		}
 }
}

database:
Here Insert Picture Description

Thank you, please criticism!

Guess you like

Origin blog.csdn.net/qq_41026809/article/details/90447921