数据库
增 insert
删 delete
改 updata
查 select
事物 commit
DATA BASE数据库
TABLE 表
字段 属性 主键(一个TABLE中不能重复,能够使用这个值确定唯一一条数据) 外键(一个表中的数据持有别的表的主键)
mySQL忽略大小写
auto_increment自动增加
java中对数据库的操作
增加
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
public class SQLTest {
public static void main(String[] args) {
//链接数据库的驱动
String dri = "com.mysql.jdbc.Driver";
//URL指向要访问的数据库名称
String url = "jdbc:mysql://localhost:3306/clazz";
//MYSQL配置的用户名
String name = "root";
//密码
String password = "19940310";
try {
//加载驱动
Class.forName(dri);
//连接数据库
Connection conn = DriverManager.getConnection(url,name,password);
if(!conn.isClosed()){
Statement statement = conn.createStatement();//创建项目
String insert = "insert into student (stu_name,sex,age) values('赵六','男',22)";
statement.execute(insert);
System.out.println("运行成功");
}else{
System.out.println("请开启数据库");
}
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
结果:
改
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
public class SQLTest {
public static void main(String[] args) {
//链接数据库的驱动
String dri = "com.mysql.jdbc.Driver";
//URL指向要访问的数据库名称
String url = "jdbc:mysql://localhost:3306/clazz";
//MYSQL配置的用户名
String name = "root";
//密码
String password = "19940310";
try {
//加载驱动
Class.forName(dri);
//连接数据库
Connection conn = DriverManager.getConnection(url,name,password);
if(!conn.isClosed()){
Statement statement = conn.createStatement();//创建项目
String update = "update student set age = 21 where stu_name = '赵六'";
statement.execute(update);
System.out.println("运行成功");
}else{
System.out.println("请开启数据库");
}
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
结果:
删
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
public class SQLTest {
public static void main(String[] args) {
//链接数据库的驱动
String dri = "com.mysql.jdbc.Driver";
//URL指向要访问的数据库名称
String url = "jdbc:mysql://localhost:3306/clazz";
//MYSQL配置的用户名
String name = "root";
//密码
String password = "19940310";
try {
//加载驱动
Class.forName(dri);
//连接数据库
Connection conn = DriverManager.getConnection(url,name,password);
if(!conn.isClosed()){
Statement statement = conn.createStatement();//创建项目
String delete = "delete from student where stu_name = '赵六'";
statement.execute(delete);
System.out.println("运行成功");
}else{
System.out.println("请开启数据库");
}
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
运行结果:
查找
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
public class SQLTest {
public static void main(String[] args) {
//链接数据库的驱动
String dri = "com.mysql.jdbc.Driver";
//URL指向要访问的数据库名称
String url = "jdbc:mysql://localhost:3306/clazz";
//MYSQL配置的用户名
String name = "root";
//密码
String password = "19940310";
try {
//加载驱动
Class.forName(dri);
//连接数据库
Connection conn = DriverManager.getConnection(url,name,password);
if(!conn.isClosed()){
Statement statement = conn.createStatement();//创建项目
String select = "select * from student";
ResultSet set = statement.executeQuery(select);
set.first();//游标放到第一位
while(!set.isAfterLast()){//判断游标是否在最后
String str = set.getString("stu_name");//获取一次select到的名字
System.out.println(str);
set.next();//游标往后一位
}
System.out.println("运行成功");
}else{
System.out.println("请开启数据库");
}
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
结果:
注册和登录界面
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
public class SQLManager {
//单例设计模式
private Statement statement ;
private static SQLManager manager;
public static synchronized SQLManager Instance(){
if(manager==null){
manager = new SQLManager();
}
return manager;
}
private SQLManager(){
//链接数据库的驱动
String dri = "com.mysql.jdbc.Driver";
//URL指向要访问的数据库名称
String url = "jdbc:mysql://localhost:3306/clazz";
//MYSQL配置的用户名
String name = "root";
//密码
String password = "19940310";
try {
//加载驱动
Class.forName(dri);
//连接数据库
Connection conn = DriverManager.getConnection(url,name,password);
if(!conn.isClosed()){
statement = conn.createStatement();//创建项目
String s = "create table if not exists user(num int not null primary "
+ "key auto_increment,user_name "
+ "varchar(30) not null,password varchar(30) not null) ";
statement.execute(s);
}
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public Statement getStatement() {
return statement;
}
public void setStatement(Statement statement) {
this.statement = statement;
}
}
import java.awt.BorderLayout;
import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JTextField;
import javax.swing.JPasswordField;
import javax.swing.JButton;
import javax.swing.JLabel;
import java.awt.event.ActionListener;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.awt.event.ActionEvent;
public class ZhuCeFRame extends JFrame {
private JPanel contentPane;
private JTextField textField;
private JPasswordField passwordField;
private JButton btnNewButton;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
ZhuCeFRame frame = new ZhuCeFRame();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the frame.
*/
public ZhuCeFRame() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 450, 300);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
contentPane.setLayout(null);
textField = new JTextField();
textField.setBounds(146, 48, 191, 21);
contentPane.add(textField);
textField.setColumns(10);
passwordField = new JPasswordField();
passwordField.setBounds(146, 124, 191, 21);
contentPane.add(passwordField);
btnNewButton = new JButton("完成注册");
btnNewButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
//获取用户名
String userName = textField.getText();
String nameRule = "^\\w{5,15}$";
if(userName.matches(nameRule)){
}else{
System.out.println("输入用户名不符合格式");
}
//获取密码
char[] passwordarr = passwordField.getPassword();
String password = null;
for (int i = 0; i < passwordarr.length; i++) {
password = password+passwordarr[i];
}
String passwordRule = "^\\w{5,15}$";
if(password.matches(passwordRule)){
}else{
System.out.println("输入密码不符合格式");
}
//判断用户是否存在
Statement statement = SQLManager.Instance().getStatement();
String sql = "select * from user where user_name='"+userName+"'";
try {
ResultSet set = statement.executeQuery(sql);
set.last();
int num = set.getRow();
if(num>0){
System.out.println("该用户名已经存在");
}else{
String insert = "insert into user(user_name,password)"
+ " values('"+userName+"','"+password+"')";
statement.execute(insert);
System.out.println("注册成功");
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
});
btnNewButton.setBounds(146, 204, 93, 23);
contentPane.add(btnNewButton);
JLabel label = new JLabel("用户名");
label.setBounds(82, 51, 54, 15);
contentPane.add(label);
JLabel label_1 = new JLabel("密码");
label_1.setBounds(82, 127, 54, 15);
contentPane.add(label_1);
}
}
import java.awt.BorderLayout;
import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JTextField;
import javax.swing.JPasswordField;
import javax.swing.JLabel;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.awt.event.ActionEvent;
public class LogInFrame extends JFrame {
private JPanel contentPane;
private JTextField textFieldUserName;
private JPasswordField passwordField;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
LogInFrame frame = new LogInFrame();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the frame.
*/
public LogInFrame() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 450, 300);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
contentPane.setLayout(null);
textFieldUserName = new JTextField();
textFieldUserName.setBounds(184, 50, 125, 21);
contentPane.add(textFieldUserName);
textFieldUserName.setColumns(10);
passwordField = new JPasswordField();
passwordField.setBounds(184, 117, 125, 21);
contentPane.add(passwordField);
JLabel lblNewLabel = new JLabel("用户名");
lblNewLabel.setBounds(120, 53, 54, 15);
contentPane.add(lblNewLabel);
JLabel lblNewLabel_1 = new JLabel("密码");
lblNewLabel_1.setBounds(120, 120, 54, 15);
contentPane.add(lblNewLabel_1);
JButton btnNewButton = new JButton("登录");
btnNewButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
String userName = textFieldUserName.getText();
char[] passwordarr = passwordField.getPassword();
String password = "";
for (int i = 0; i < passwordarr.length; i++) {
password = passwordarr[i]+password;
}
Statement state = SQLManager.Instance().getStatement();
String sql = "select * from user where user_name = '"+userName+"' "
+ "and password = '"+password+"'";
try {
ResultSet set = state.executeQuery(sql);
set.last();
int num = set.getRow();
System.out.println(num);
if(num==0){
System.out.println("登录成功");
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
});
btnNewButton.setBounds(184, 191, 93, 23);
contentPane.add(btnNewButton);
}
}
运行结果: