package cn;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;
import com.mysql.jdbc.Driver;
public class jdbc {
public static void main(String[] args) throws Exception {
Class.forName("com.mysql.jdbc.Driver");
System.out.println("数据库驱动加载成功");
Connection conn = DriverManager.getConnection("jdbc:mysql//localhost:3306/girls", "root", "123456");
System.out.println("创建连接成功");
String sql="update admin set username= 9999 where id=1";
Statement stmt = conn.createStatement();
int count = stmt.executeUpdate(sql);
System.out.println( count );
stmt.close();
conn.close();
}
}
url写错了。mysql后面少写了冒号:
package cn;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;
import com.mysql.jdbc.Driver;
public class jdbc {
public static void main(String[] args) throws Exception {
Class.forName("com.mysql.jdbc.Driver");
System.out.println("数据库驱动加载成功");
Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/girls", "root", "123456");
System.out.println("创建连接成功");
String sql="update admin set username= 9999 where id=1";
Statement stmt = conn.createStatement();
int count = stmt.executeUpdate(sql);
System.out.println( count );
stmt.close();
conn.close();
}
}
static final String JDBC_DRIVER = "com.mysql.cj.jdbc.Driver";
No sutiable driver是因为版本吗,我记得不同版本的这个语句不一样,差一个cj,你可以试一下
出现这些说明几个问题
1:代码写的问题
2:你有没有改过host文件,如果不确定就把localhost改为127.0.0.1
3:你的数据库中创建了对应的库和表了吗?
4:也是最重要的,你的数据库服务启动了吗?相关判断数据库是否启动可以去自己百度一下
5:你用navicat之类的工具能否连接上数据库,账号密码是否对
不告诉你了嘛,你对比网上百度的url地址,看看你这有没有问题
com.mysql.jdbc.Driver
改成
com.mysql.cj.jdbc.Driver