java连接mysql/mariadb
0、前期准备
mariadb java驱动下载:
mysql java驱动下载:
1、导入sql java驱动包到项目(eclipse)
1)右键单击项目名称—>
Build path
—>condigure Build Path
2)点击condigure Build Path
后进入如下页面 找到Libraries
点击Add JARs...
找到你下载好,需要添加的java mysql或者java mariadb驱动包点击Apply
添加就可以了
2、码代码
代码一般分为七个部分,先来说说java中mysql的增、删、改、查
,java中mysql的数据操作需要用到两个方法:executeUpdate()
和executeQuery()
因此我们分开来讲。
本段内容来源
Statement 接口提供了三种执行 SQL 语句的方法:executeQuery、executeUpdate 和 execute。使用哪一个方法由 SQL 语句所产生的内容决定。
方法executeQuery :
- 用于产生单个结果集的语句,例如 SELECT 语句。 被使用最多的执行 SQL 语句的方法是 executeQuery。这个方法被用来执行 SELECT 语句,它几乎是使用最多的 SQL 语句。
方法executeUpdate
- 用于执行 INSERT、UPDATE 或 DELETE 语句以及 SQL DDL(数据定义语言)语句,例如 CREATE TABLE 和 DROP TABLE。INSERT、UPDATE 或 DELETE 语句的效果是修改表中零行或多行中的一列或多列。executeUpdate 的返回值是一个整数,指示受影响的行数(即更新计数)。对于 CREATE TABLE 或 DROP TABLE 等不操作行的语句,executeUpdate 的返回值总为零。
executeUpdate方法
- 使用executeUpdate方法是因为在 createTableCoffees 中的 SQL 语句是 DDL (数据定义语言)语句。创建表,改变表,删除表都是 DDL 语句的例子,要用 executeUpdate 方法来执行。你也可以从它的名字里看出,方法 executeUpdate 也被用于执行更新表 SQL 语句。实际上,相对于创建表来说,executeUpdate 用于更新表的时间更多,因为表只需要创建一次,但经常被更新。
1)增、删、改
:mariadb与mysql略微有点差别或者说可以不计
差别:加粗部分
mysql:Class.forName(“come.mysql.jdbc.Driver”);//可以通用,若出错可以尝试使用下一条
mariadb:Class.forName(“org.mariadb.jdbc.Driver”);
我们连接一个叫studentInfo
的数据库并对student
表进行操作。
java代码如下:
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;
public class JDBCDome {
public static void main(String[] args) throws SQLException {
String url = "jdbc:mysql://localhost:3306/studentInfo";
String user = "root";
String password = "root";
//1.加载驱动
try{
Class.forName("com.mysql.jdbc.Driver");//mysql
//若导入的驱动包是mariadb的可以使用下面这个
//mariadb:Class.forName("org.mariadb.jdbc.Driver");//mariadb
} catch(ClassNotFoundException e) {
System.out.println("数据库驱动加载失败!");
e.printStackTrace();
}
//2.创建连接
Connection connection = DriverMannager.getConnection(url,user,password);
//3.sql语句拼凑
//对student表进行插入数据student(studentId,studentName,studentAge,gender,classId)
Srting sql = "insert into student(studentId,studentName,studentAge,gender,classId) values (12000,'孙悟空',1000,'男',1)";
//4.获取prepareStatement
PrepareStatement ps = connection.prepareStatement(sql);
//5.发送并执行sql语句
int i = executeUpdate();
//6.打印执行结果
if(i>0){
System.out.println("执行成功!");
}else{
System.out.println("插入失败!");
}
//7.关闭资源(先打开的后关闭)
ps.close();
connection.close();
}
}
2)查询:
查询时连接数据库的代码和上面 **1)**的是一样的,不同的是执行时使用的是executeQuery()
方法,同时返回的是一个结果集ResultSet
.
代码如下:
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
public class QueryDemo {
public static void main(String[] args) throws SQLException {
String url = "jdbc:mysql://localhost:3306/studentInfo";
String user="root";
String password = "root";
//1.加载驱动
try {
Class.forName("com.mysql.jdbc.Driver");
} catch (ClassNotFoundException e) {
System.out.println("驱动加载失败");
e.printStackTrace();
}
//2.创建连接
Connection connection = DriverManager.getConnection(url, user, password);
//3.拼写sql语句
String sql = "select * from student";
//4.获取preparestatement
PrepareStatement ps = connection.prepareStarement(sql);
//5.发送并执行sql语句
ResultSet rs = ps.executeQuery();
while(rs.next()){
int id = rs.getInt("studentId");
String name = rs.getString("studentname");
int age = rs.getInt("studentage");
String gender = rs.getString("gender");
int classId = rs.getInt("classid");
System.out.print(id+" ");
System.out.print(name+" ");
System.out.print(age+" ");
System.out.print(gender+" ");
System.out.print(classId+"");
System.out.println();
}
//7.关闭资源(先打开的后关闭)
rs.close();
ps.close();
connection.close();
}
}
常见异常总结
java.lang.ClassNotFoundException: com.mysql.jdbc.Driver...
No suitable driver found for jdbc:mysql://localhost...
解决办法:
1).连接URL格式出现了问题 Connection conn=DriverManager.getConnection(url,user,password);
2).驱动字符串出错(com.mysql.jdbc.Driver)
可以尝试重新导入驱动包方法如:步骤1;若还是失败,可以试着做下面的操作:
将驱动包mysql-connector-java-XXX-bin.jar的jar包加入java jak安装目录下的jre\lib\ext文件夹下或者jir-x-x.x\lib\ext文件下。其中xxx
代表版本号。若jre下没有ext文件夹,直接放在jre\lib文件夹也是可以的。