Class.forName("com.mysql.cj.jdbc.Driver");
此处地址获取如图位置:
+ 右键点击Driver
URL:jdbc:mysql://ip:端口号/数据库名?时区(serverTimezone=Asia/Shanghai)
User:root
password:root
方式一:本地数据库连接直接使用localhost作为ip
Connection conn= DriverManager.getConnection("jdbc:mysql://localhost:3306/test3?serverTimezone=Asia/Shanghai", "root", "521521");
方式二:利用ip远程操纵数据库,连接其他电脑需要ip,user,password,所连电脑关闭防火墙。
测试连接是否成功:
System.out.println(conn2.isClosed()==false? "连接成功":"连接失败");
String sql2="delete from student where sname='回'";
//基本增删改查语句
/* String sql="insert into student values('1','回','男','1997-04-22','950')"; //增
String sql1="update student set sname='回' where sno='950'"; //改
String sql2="delete from student where sname='嘿嘿'"; //删
String sql3="select * from student"; //查
*/
Statement st=conn2.createStatement();
int i=st.executeUpdate(sql2);
System.out.println(i);
//查询需要使用ResultSet 接收返回结果集
/*ResultSet rs=st.executeQuery(sql3);
while(rs.next())
{
String sno=rs.getString(1);
String sname=rs.getString(2);
String ssex=rs.getString(3);
Date sbirthday=rs.getDate(4);
String sclass=rs.getString(5);
System.out.println(sno+"\t"+sname+"\t"+ssex+"\t"+sbirthday+"\t"+sclass);
}
*/
// rs.close();
st.close();
conn2.close();
数据库连接完成!常用sql查询语句移步
常用查询语句
package com.zhiyou.jdbcTest;
import java.sql.Connection;
import java.sql.Date;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.sql.Timestamp;
public class PreperTest {
public static void main(String[] args) {
try {
Connection conn=DriverManager.getConnection("jdbc:mysql://localhost:3306/test3?serverTimezone=Asia/Shanghai", "root", "521521");
String sql="insert into student values(?,?,?,?,?)"; //?占位
PreparedStatement ps=conn.prepareStatement(sql);
ps.setString(1, "20"); //向所占位置添加数据
ps.setString(2, "哈哈哈");
ps.setString(3, "男");
//输入时间格式3种:
//1.可获取时分秒
Timestamp ts=new Timestamp(new java.util.Date().getTime());
ps.setObject(4, ts);
//2.直接输入,必须用-连接
//ps.setDate(4,Date.valueOf("2019-04-22"));
//3.字符串类型输入
//ps.setString(4, "2019.04.22");
ps.setString(5, "95320");
int i=ps.executeUpdate(); //返回修改记录条数
System.out.println(i);
ps.close();
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
package com.zhiyou.jdbcTest;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
public class MysqlTool {
//定义数据库参数
private static String URL="jdbc:mysql://localhost:3306/test3?serverTimezone=Asia/Shanghai";
private static String USER="root";
private static String PASSWORD="521521";
//创建连接对象,载体,结果集对象
private static Connection conn=null;
private static PreparedStatement ps=null;
private static ResultSet rs=null;
//创建连接对象,可以直接在实例化前加载,使用静态方法
static{
try {
conn=DriverManager.getConnection(URL, USER, PASSWORD);
System.out.println(conn.isClosed()==false? "数据库连接成功...":"数据库连接失败...");
} catch (SQLException e) {
e.printStackTrace();
}
}
/**
* 增删改方法update
* @param sql 需要执行的sql语句
* @param objects 封装了数据的数组,需要和sql语句中的占位符一一对应
* @return 返回影响条数,反之为0
*/
public int update(String sql,Object[] objects){
int a=0;
try {
//创建sql载体
ps=conn.prepareStatement(sql);
//给占位符赋值
for(int i=0;i
package com.zhiyou.jdbcTest;
import java.sql.Date;
import java.sql.ResultSet;
import java.sql.SQLException;
public class MysqlTest {
public static void main(String[] args) {
MysqlTool mt=new MysqlTool();
String sql="insert into student values(?,?,?,?,?)";
Object[] objects={"2","嘿嘿","男","1995.02.03","5645"};
/*String sql1="update student set sname='hhh' where sno=?";
String sql3 = "delete from student where sno=?";
Object[] objects1={2};*/
int i=mt.update(sql, objects);
System.out.println("成功操作"+i+"条数据...");
String sql4="select * from student LIMIT 1,2";
Object[] objects2={};
ResultSet rs=mt.query(sql4, objects2);
try {
while(rs.next()){
Date date = rs.getDate("sbirthday");
int sid = rs.getInt("sno");
String sname = rs.getString("sname");
System.out.println(sid+"\t"+sname+"\t"+date);
}
} catch (SQLException e) {
e.printStackTrace();
}
mt.close();
}
}