安装MySQL,版本为5.7
ConnectionDemo.java
import static java.lang.System.out;
import java.sql.*;
public class ConnectionDemo {
public static void main(String[] args)
throws ClassNotFoundException, SQLException {
Class.forName("com.mysql.jdbc.Driver");
String jdbcUrl = "jdbc:mysql://localhost:3306/demo";
String user = "root";
String passwd = "";
try(Connection conn =
DriverManager.getConnection(jdbcUrl, user, passwd)) {
out.printf("已%s数据库连接%n",
conn.isClosed() ? "关闭" : "打开");
}
}
}
mysqld--initialize-insecure
mysqladmin -u root -p password
try{ Class.forName("com.mysql.jdbc.Driver");
}
catch(Exception e){}
String uri = "jdbc:mysql://localhost/students?useSSL=true&characterEncoding=utf-8";
con = DriverManager.getConnection(uri, "root",""); //连接代码
try{Statement sql = con.createStatement();
}
catch(SQLException e){}
Statement stmt = con.createStatement(int type ,int concurrency);
update 表 set 字段 = 新值 where <条件子句>
insert into 表(字段列表) values (对应的具体的记录)
insert into 表 values (对应的具体的记录)
delete from 表名 where <条件子句>
ResultSet executeQuery()
boolean execute()
int executeUpdate()
try { Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
}
catch(Exception e){
}
try{
String uri= "jdbc:sqlserver://192.168.100.1:1433;DatabaseName=warehouse";
String user="sa";
String password="dog123456";
con=DriverManager.getConnection(uri,user,password);
}
catch(SQLException e){
System.out.println(e);
}
Class.forName("org.apache.derby.jdbc.EmbeddedDriver");
Connection con =
DriverManager.getConnection("jdbc:derby:students;create=true");
原文:https://www.cnblogs.com/zyh5322/p/10780928.html