流程:
1.加载驱动
(Class.forName(DBDRIVER);//加载驱动)
2.连接(conn=DriverManager.getConnection(DBURL,DBUSER,DBPASS);//连接)
3.操作sql(更新,查询)
【statement,PreparedStatement 】
stmt = conn.createStatement();//操作
stmt.executeUpdate(sql);//执行操作
4.关闭连接(stmt,conn,rs,pstmt)
Connection conn = null;//连接
Class.forName(DBDRIVER);//加载驱动
conn=DriverManager.getConnection(DBURL,DBUSER,DBPASS);//连接
没有报错=成功
Connection conn = null;//连接
Statement stmt = null;//操作sql
String sql = "INSERT INTO USER ( name, password, age, sex, birthday ) VALUES( '李兴华', 'www.mldn.cn', 30, '男', '2008-08-27' )";
Class.forName(DBDRIVER);//加载驱动
conn=DriverManager.getConnection(DBURL,DBUSER,DBPASS);//连接
stmt = conn.createStatement();//操作
stmt.executeUpdate(sql);//执行操作
String sql = "INSERT INTO USER ( name, password, age, sex, birthday ) VALUES( '李兴华', 'www.mldn.cn', 30, '男', '2008-08-27' )";
package file;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.Statement;
public class demo
{
public static final String DBDRIVER = "com.mysql.cj.jdbc.Driver";
public static final String DBURL = "jdbc:mysql://localhost:3306/java_mysql";
public static final String DBUSER = "root";
public static final String DBPASS = "dyh20011022";
public static void main(String[] args) throws Exception
{
Connection conn = null;//连接
Statement stmt = null;//操作sql
String sql = "INSERT INTO USER ( name, password, age, sex, birthday ) VALUES( '李兴华', 'www.mldn.cn', 30, '男', '2008-08-27' )";
Class.forName(DBDRIVER);//加载驱动
conn = DriverManager.getConnection(DBURL,DBUSER,DBPASS);//连接
stmt = conn.createStatement();//操作
stmt.executeUpdate(sql);//执行操作
stmt.close();//操作关闭
conn.close();//连接关闭
}
}
String sql = "UPDATE user SET name ='"+name+"' , password = '"+password+"',age = "+age+" , sex = '"+sex+"' , birthday = '"+birthday+"'WHERE id = "+id;
package file;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.Statement;
public class demo
{
public static final String DBDRIVER = "com.mysql.cj.jdbc.Driver";
public static final String DBURL = "jdbc:mysql://localhost:3306/java_mysql";
public static final String DBUSER = "root";
public static final String DBPASS = "dyh20011022";
public static void main(String[] args) throws Exception
{
int id = 3;
String name ="name3";
String password = "name_password";
int age =10;
String sex ="女";
String birthday = "2001-10-03";
Connection conn = null;//连接
Statement stmt = null;//操作sql
String sql = "UPDATE user SET name ='"+name+"' , password = '"+password+"',age = "+age+" , sex = '"+sex+"' , birthday = '"+birthday+"'WHERE id = "+id;
Class.forName(DBDRIVER);//加载驱动
conn = DriverManager.getConnection(DBURL,DBUSER,DBPASS);//连接
stmt = conn.createStatement();//操作
stmt.executeUpdate(sql);//执行操作
stmt.close();//操作关闭
conn.close();//连接关闭
}
}
String sql = "DELETE FROM user WHERE id =" + id;
package file;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.Statement;
public class demo
{
public static final String DBDRIVER = "com.mysql.cj.jdbc.Driver";
public static final String DBURL = "jdbc:mysql://localhost:3306/java_mysql";
public static final String DBUSER = "root";
public static final String DBPASS = "dyh20011022";
public static void main(String[] args) throws Exception
{
int id = 3;
Connection conn = null;//连接
Statement stmt = null;//操作sql
String sql = "DELETE FROM user WHERE id =" + id;
Class.forName(DBDRIVER);//加载驱动
conn = DriverManager.getConnection(DBURL,DBUSER,DBPASS);//连接
stmt = conn.createStatement();//操作
stmt.executeUpdate(sql);//执行操作
stmt.close();//操作关闭
conn.close();//连接关闭
}
}
ResultSet rs = stmt.executeQuery(sql);//执行操作
while(rs.next())
{
int id = rs.getInt("id");
String name = rs.getString("name");
。。。。。。。。。
}
package file;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
public class demo
{
public static final String DBDRIVER = "com.mysql.cj.jdbc.Driver";
public static final String DBURL = "jdbc:mysql://localhost:3306/java_mysql";
public static final String DBUSER = "root";
public static final String DBPASS = "dyh20011022";
public static void main(String[] args) throws Exception
{
Connection conn = null;//连接
Statement stmt = null;//操作sql
String sql = "select id,name,password,age,sex,birthday from user";
Class.forName(DBDRIVER);//加载驱动
conn = DriverManager.getConnection(DBURL,DBUSER,DBPASS);//连接
stmt = conn.createStatement();//操作
ResultSet rs = stmt.executeQuery(sql);//执行操作
while(rs.next())
{
int id = rs.getInt("id");
String name = rs.getString("name");
String password = rs.getString("password");
String sex = rs.getString("sex");
String age = rs.getString("age");
String birthday = rs.getString("birthday");
System.out.println("--------------------------------------------------------");
System.out.println(id+"\t"+name+"\t"+password+"\t"+age+"\t"+sex+"\t"+birthday);
}
rs.close();//结果集关闭
stmt.close();//操作关闭
conn.close();//连接关闭
}
}
while(rs.next())
{
int id = rs.getInt(1);
String name = rs.getString(2);
String password = rs.getString(3);
int age = rs.getInt(4);
String sex = rs.getString(5);
String birthday = rs.getString(6);
System.out.println("--------------------------------------------------------");
System.out.println(id+"\t"+name+"\t"+password+"\t"+age+"\t"+sex+"\t"+birthday);
}
package file;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.Statement;
import java.text.SimpleDateFormat;
import java.util.Date;
public class demo
{
public static final String DBDRIVER = "com.mysql.cj.jdbc.Driver";
public static final String DBURL = "jdbc:mysql://localhost:3306/java_mysql";
public static final String DBUSER = "root";
public static final String DBPASS = "dyh20011022";
public static void main(String[] args) throws Exception
{
Class.forName(DBDRIVER);//加载驱动
String name = "读研贺";
String password="duyanhe";
String sex="男";
String birthday="2001-10-06";
int age = 22;
java.util.Date temp = new SimpleDateFormat("yyyy-MM-dd").parse(birthday);
java.sql.Date bir = new java.sql.Date(temp.getTime());
String sql = "INSERT INTO user(name,password,age,sex,birthday) VALUES(?,?,?,?,?)";
Connection conn = DriverManager.getConnection(DBURL,DBUSER,DBPASS);
PreparedStatement pstmt = conn.prepareStatement(sql);
pstmt.setString(1, name);
pstmt.setString(2, password);
pstmt.setInt(3, age);
pstmt.setString(4,sex);
pstmt.setDate(5,bir);
pstmt.executeUpdate();
pstmt.close();
conn.close();
}
}
package file;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.Statement;
import java.text.SimpleDateFormat;
import java.util.Date;
public class demo
{
public static final String DBDRIVER = "com.mysql.cj.jdbc.Driver";
public static final String DBURL = "jdbc:mysql://localhost:3306/java_mysql";
public static final String DBUSER = "root";
public static final String DBPASS = "dyh20011022";
public static void main(String[] args) throws Exception
{
Class.forName(DBDRIVER);//加载驱动
String sql = "select id,name,password,age,sex,birthday from user where name like ? or password like ? or sex like ?";
Connection conn = DriverManager.getConnection(DBURL,DBUSER,DBPASS);//连接
PreparedStatement pstmt = conn.prepareStatement(sql);//操作
pstmt.setString(1, "%李%");
pstmt.setString(2, "%李%");
pstmt.setString(3, "%李%");
ResultSet rs = pstmt.executeQuery();//执行操作
while(rs.next())
{
int id = rs.getInt(1);
String name = rs.getString(2);
String password = rs.getString(3);
int age = rs.getInt(4);
String sex = rs.getString(5);
java.util.Date d =rs.getDate(6);
System.out.println("---------------------------------------------");
System.out.println(id+"\t"+name+"\t"+password+"\t"+age+"\t"+sex+"\t"+d);
}
rs.close();
pstmt.close();
conn.close();
}
}