IT数码 购物 网址 头条 软件 日历 阅读 图书馆
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
图片批量下载器
↓批量下载图片,美女图库↓
图片自动播放器
↓图片自动播放器↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁
 
   -> Java知识库 -> 在eclipse中实现基于Mysql8.0版本下java学生信息管理系统 -> 正文阅读

[Java知识库]在eclipse中实现基于Mysql8.0版本下java学生信息管理系统

在eclipse中实现基于Mysql8.0版本下java学生信息管理系统

?开发小型的学生信息管理系统。

(1)用JAVA开发该系统的前端

(2)连接数据库

(3)要求该系统有对数据库的增、删、改、查和测试的功能。

一、主界面

EX12_5_StudentManagement.java

package exp6;
import java.sql.*;
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import javax.swing.border.*;
import javax.swing.JOptionPane;


public class EX12_5_StudentManagement extends JFrame implements ActionListener{
?? ?JMenuBar bar = null;
?? ?JMenu menu1,menu2,menu3,menu4,menu5;// ? ?菜单栏
?? ?JMenuItem item1,item2,item3,item4,item5;// ?项目栏 ?
?? ?EX12_6_StudentAdd zengjia;
?? ?EX12_7_StudentQuery chaxun;
?? ?EX12_8_StudentUpdate gengxin; ?//.............................
?? ?EX12_9_StudentDelete shanchu; ?//.............................
?? ?//JDBCTest ceshi;//...........................................自己添加
?? ??
?? ?EX12_5_StudentManagement(){
?? ??? ?super("学 生 信 息 管 理 系 统");
?? ??? ?zengjia = new EX12_6_StudentAdd();
?? ??? ?chaxun = new EX12_7_StudentQuery();
?? ??? ?gengxin = new EX12_8_StudentUpdate(); ?// ....................
?? ??? ?shanchu = new EX12_9_StudentDelete(); ? //.....................
?? ??? ?//ceshi = new JDBCTest();// ? ? ? ? ? ? 自己添加
?? ??? ?bar = new JMenuBar();
?? ??? ?menu1 = new JMenu("信息录入");
?? ??? ?menu2 = new JMenu("信息查询");
?? ??? ?menu3 = new JMenu("信息更新");
?? ??? ?menu4 = new JMenu("信息删除");
?? ??? ?menu5 = new JMenu("退出系统");
?? ??? ?item1 = new JMenuItem("录 ?入");
?? ??? ?item2 = new JMenuItem("查 ?询");
?? ??? ?item3 = new JMenuItem("更 ?新");
?? ??? ?item4 = new JMenuItem("删 ?除");
?? ??? ?item5 = new JMenuItem("退 ?出");
?? ??? ?menu1.add(item1);
?? ??? ?menu2.add(item2);
?? ??? ?menu3.add(item3);
?? ??? ?menu4.add(item4);
?? ??? ?menu5.add(item5);
?? ??? ?bar.add(menu1);
?? ??? ?bar.add(menu2);
?? ??? ?bar.add(menu3);
?? ??? ?bar.add(menu4);
?? ??? ?bar.add(menu5);
?? ??? ?setJMenuBar(bar);
?? ??? ?item1.addActionListener(this);
?? ??? ?item2.addActionListener(this);
?? ??? ?item3.addActionListener(this);
?? ??? ?item4.addActionListener(this);
?? ??? ?item5.addActionListener(this);
?? ??? ??? ??? ??? ?
?? ??? ?JLabel label = new JLabel("学生信息管理系统",JLabel.CENTER);
?? ??? ?label.setFont(new Font("学生信息管理系统",0,35));
?? ??? ?String s = " ";
?? ??? ?Font f = new Font(s,Font.BOLD,60);
?? ??? ?label.setBackground(new Color(0,255,255));
?? ??? ?label.setForeground(new Color(255,128,255));
?? ??? ?add(label,"Center");
?? ??? ?setVisible(true);
?? ??? ?setSize(350,300);
?? ?}
?? ?public void actionPerformed(ActionEvent e){
?? ??? ?if(e.getSource() == item1){
?? ??? ??? ?this.getContentPane().removeAll();
?? ??? ??? ?this.getContentPane().add(zengjia,"Center");
?? ??? ??? ?this.getContentPane().repaint();
?? ??? ??? ?this.getContentPane().validate();
?? ??? ??? ?
?? ??? ?}
?? ??? ?if(e.getSource() == item2){
?? ??? ??? ?this.getContentPane().removeAll();
?? ??? ??? ?this.getContentPane().add(chaxun,"Center");
?? ??? ??? ?this.getContentPane().repaint();
?? ??? ??? ?this.getContentPane().validate();
?? ??? ??? ?
?? ??? ?}
?? ??? ?
?? ??? ?
?? ??? ?if(e.getSource() == item3){
?? ??? ??? ?this.getContentPane().removeAll();
?? ??? ??? ?this.getContentPane().add(gengxin,"Center");
?? ??? ??? ?this.getContentPane().repaint();
?? ??? ??? ?this.getContentPane().validate();
?? ??? ??? ?
?? ??? ?
?? ?}
?? ??? ?if(e.getSource() == item4){
?? ??? ??? ?this.getContentPane().removeAll();
?? ??? ??? ?this.getContentPane().add(shanchu,"Center");
?? ??? ??? ?this.getContentPane().repaint();
?? ??? ??? ?this.getContentPane().validate();
?? ??? ?}
?? ?if(e.getSource() == item5){
?? ??? ?
?? ??? ?System.exit(0);
?? ?}
?? ?}
?? ?public static void main(String args[]){
?? ??? ?EX12_5_StudentManagement stuM = new EX12_5_StudentManagement();
?? ??? ?stuM.setVisible(true);
?? ??? ?
?? ??? ?stuM.addWindowListener(new WindowAdapter(){
?? ??? ??? ?public void windowClosing(WindowEvent e){
?? ??? ??? ??? ?System.exit(0);
?? ??? ??? ?}
?? ??? ?});
?? ??? ?}
?? ?}

二、 信息录入界面

EX12_6_StudentAdd zengjia.java

EX12_6_StudentAdd zengjia.java

package exp6;

import java.sql.*;
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import javax.swing.border.*;
import javax.swing.JOptionPane;

public class EX12_6_StudentAdd extends JPanel implements ActionListener{
? Connection con; //与特定数据库的连接(会话)。
? Statement sql; //用于执行静态 SQL 语句并返回它所生成结果的对象。
? JButton b1,b2;
? JTextField tf1,tf2,tf3,tf4,tf5,tf6;
? Box baseBox,bv1,bv2;
? EX12_6_StudentAdd(){
? ?try{?? ??? ??? ??? ??? ??? ??? ??? ??? ??? ?//错误处理机制
? ? Class.forName("com.mysql.cj.jdbc.Driver");?? ?//通过 Class.forName为数据库管理系统加载一个JDBC驱动程序。
? ? }
? ?catch(ClassNotFoundException e){}?? ??? ?//如果加载驱动失败 控制台抛出异常
? ? try{?? ??? ??? ??? ??? ??? ??? ??? ??? ?//如果加载驱动成功, 调用驱动连接特定数据库
? ? ?con=DriverManager.getConnection("jdbc:mysql://localhost:3306/university?characterEncoding=utf8","root","123456");
? ? ?sql=con.createStatement(); ? ? ?//调取con成员方法获取Statement对象
? ? }
? ? catch(SQLException ee){}
? ??
? ? //设置面板布局 为边框布局
? ?setLayout(new BorderLayout());
? ?JPanel p1=new JPanel();
? ?JPanel p2=new JPanel();
? ?tf1=new JTextField(16);
? ?tf2=new JTextField(16);
? ?tf3=new JTextField(16);
? ?tf4=new JTextField(16);
? ?tf5=new JTextField(16);
? ?tf6=new JTextField(16);
? ?b1=new JButton("录入"); ?
? ?b2=new JButton("重置");
? ?b1.addActionListener(this);
? ?b2.addActionListener(this);
? ?p1.add(b1);
? ?p1.add(b2);
? ?bv1=Box.createVerticalBox();
? ?bv1.add(new JLabel("学号"));
? ?bv1.add(Box.createVerticalStrut(8));
? ?bv1.add(new JLabel("姓名"));
? ?bv1.add(Box.createVerticalStrut(8));
? ?bv1.add(new JLabel("性别"));
? ?bv1.add(Box.createVerticalStrut(8));
? ?bv1.add(new JLabel("地址"));
? ?bv1.add(Box.createVerticalStrut(8));
? ?bv1.add(new JLabel("电话"));
? ?bv1.add(Box.createVerticalStrut(8));
? ?bv1.add(new JLabel("专业"));
? ?bv1.add(Box.createVerticalStrut(8));
? ?bv2=Box.createVerticalBox();
? ?bv2.add(tf1);
? ?bv2.add(Box.createVerticalStrut(8));
? ?bv2.add(tf2);
? ?bv2.add(Box.createVerticalStrut(8));
? ?bv2.add(tf3);
? ?bv2.add(Box.createVerticalStrut(8));
? ?bv2.add(tf4);
? ?bv2.add(Box.createVerticalStrut(8));
? ?bv2.add(tf5);
? ?bv2.add(Box.createVerticalStrut(8));
? ?bv2.add(tf6);
? ?bv2.add(Box.createVerticalStrut(8));
? ?baseBox=Box.createHorizontalBox();
? ?baseBox.add(bv1);
? ?baseBox.add(Box.createHorizontalStrut(10));
? ?baseBox.add(bv2);
? ?p2.add(baseBox);
? ?add(p1,"South");
? ?add(p2,"Center");
? ?setSize(350,300);
? ?setBackground(Color.pink);
? }
? public void actionPerformed(ActionEvent e){
? ?if(e.getSource()==b1){
? ? try{ insert();}
? ? catch(SQLException ee){}
? ? JOptionPane.showMessageDialog(this,"数据已入库!","提示对话框",JOptionPane.INFORMATION_MESSAGE);
? ?}
? ?else if(e.getSource()==b2){
? ? ? ?tf1.setText(" ");
? ? ? ?tf2.setText(" ");
? ? ? ?tf3.setText(" ");
? ? ? ?tf4.setText(" ");
? ? ? ?tf5.setText(" ");
? ? ? ?tf6.setText(" ");
? ?}
? }
? public void insert() throws SQLException{
? ?String s1="'"+tf1.getText().trim()+"'";
? ?String s2="'"+tf2.getText().trim()+"'";
? ?String s3="'"+tf3.getText().trim()+"'";
? ?String s4="'"+tf4.getText().trim()+"'";
? ?String s5="'"+tf5.getText().trim()+"'";
? ?String s6="'"+tf6.getText().trim()+"'";
? ?
? ? con=DriverManager.getConnection("jdbc:mysql://localhost:3306/university?characterEncoding=utf8","root","123456");
? ?
? ? String temp="INSERT INTO student VALUES ("+s1+","+s2+","+s3+","+s4+","+s5+","+s6+")";
? ? sql.executeUpdate(temp);
? ?con.close();
? }
}


?

三、查询界面

EX12_7_StudentQuery chaxun.java

package exp6;

import java.sql.*;
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import javax.swing.border.*;

//query  查询
public class EX12_7_StudentQuery extends JPanel implements ActionListener{
	Connection con;
	Statement sql;
	JTextField t1,t2,t3,t4,t5,t6;
	JButton b;
	Box baseBox,bv1,bv2;
	int flag = 0;
	EX12_7_StudentQuery(){
		try{
			Class.forName("com.mysql.cj.jdbc.Driver");
		}
		catch(ClassNotFoundException e){}
		try{
			con = DriverManager.getConnection("jdbc:mysql://localhost:3306/university?characterEncoding=utf8","root","123456");
			sql = con.createStatement();
		}
		catch(SQLException ee){}
		
		setLayout (new BorderLayout());
		b = new JButton("查询");
		b.setBackground(Color.orange);
		b.addActionListener(this);
		t1 = new JTextField(8);
		t2 = new JTextField(16);
		t3 = new JTextField(16);
		t4= new JTextField(16);
		t5 = new JTextField(16);
		t6 = new JTextField(16);
		t2.setEditable(false);
		t3.setEditable(false);
		t4.setEditable(false);
		t5.setEditable(false);
		t6.setEditable(false);
		JPanel p1 = new JPanel(),p2 = new JPanel();
		p1.add(new JLabel("请输入学号:"));
		p1.add(t1);
		p1.add(b);
		bv1 = Box.createVerticalBox();
		bv1.add(new JLabel("姓名"));
		bv1.add(Box.createVerticalStrut(8));
		bv1.add(new JLabel("性别"));
		bv1.add(Box.createVerticalStrut(8));
		bv1.add(new JLabel("地址"));
		bv1.add(Box.createVerticalStrut(8));
		bv1.add(new JLabel("电话"));
		bv1.add(Box.createVerticalStrut(8));
		bv1.add(new JLabel("专业"));
		bv1.add(Box.createVerticalStrut(8));
		bv2 = Box.createVerticalBox();
		bv2.add(t2);
		bv2.add(Box.createVerticalStrut(8));
		bv2.add(t3);
		bv2.add(Box.createVerticalStrut(8));
		bv2.add(t4);
		bv2.add(Box.createVerticalStrut(8));
		bv2.add(t5);
		bv2.add(Box.createVerticalStrut(8));
		bv2.add(t6);
		bv2.add(Box.createVerticalStrut(8));
		baseBox = Box.createHorizontalBox();
		baseBox.add(bv1);
		baseBox.add(Box.createHorizontalStrut(10));
		baseBox.add(bv2);
		p2.add(baseBox);
		add(p1,"North");
		add(p2,"Center");
		setSize(350,300);
		setBackground(Color.white);
	}
	public void actionPerformed(ActionEvent e){
		flag = 0;
		try{query();}
		catch(SQLException ee){}
	}
	public void query() throws SQLException{
		String num,name,gender,address,phone,major;
		con = DriverManager.getConnection("jdbc:mysql://localhost:3306/university?characterEncoding=utf8","root","123456");
		num = t1.getText().trim();
		ResultSet rs = sql.executeQuery("SELECT* FROM student WHERE id = '"+ num +"'");		
	
		if(rs.next()){
			name = rs.getString("name");
			gender = rs.getString("gender");
			address = rs.getString("address");
			phone = rs.getString("phone");
			major = rs.getString("major");
			t2.setText(name);
			t3.setText(gender);
			t4.setText(address);
			t5.setText(phone);
			t6.setText(major);
			flag = 1;			
		}else{
			JOptionPane.showMessageDialog(this,"没有该学生!","提示对话框",JOptionPane.INFORMATION_MESSAGE);
		}
		con.close();
		if(flag == 0){t1.setText("没有该学生");}		
	}
}

四、更新界面

EX12_8_StudentUpdate gengxin.java

package exp6;

import java.awt.event.*;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.awt.*;

import javax.swing.Box;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;

public class EX12_8_StudentUpdate extends JPanel implements ActionListener{
	Connection con;
	Statement sql;
	JTextField t1,t2,t3,t4,t5,t6;
	JButton b1,b2,b3;
	Box baseBox,bv1,bv2;
	EX12_8_StudentUpdate(){
		try{
			Class.forName("com.mysql.cj.jdbc.Driver");
		}
		catch(ClassNotFoundException e){}
		try{
			con = DriverManager.getConnection("jdbc:mysql://localhost:3306/university?characterEncoding=utf8","root","123456");
			sql = con.createStatement();
		}
		catch(SQLException ee){}
		//设置面板布局 为边框布局
		   setLayout(new BorderLayout());
		   JPanel p1=new JPanel();
		   JPanel p2=new JPanel();
		   t1=new JTextField(16);
		   t2=new JTextField(16);
		   t3=new JTextField(16);
		   t4=new JTextField(16);
		   t5=new JTextField(16);
		   t6=new JTextField(16);
		   b1=new JButton("查询");
		   b2=new JButton("更新");
		   b3=new JButton("取消");
		   b1.addActionListener(this);
		   b2.addActionListener(this);
		   b3.addActionListener(this);
		   p1.add(b1);
		   p1.add(b2);
		   p1.add(b3);
		   bv1=Box.createVerticalBox();
		   bv1.add(new JLabel("学号"));
		   bv1.add(Box.createVerticalStrut(8));
		   bv1.add(new JLabel("姓名"));
		   bv1.add(Box.createVerticalStrut(8));
		   bv1.add(new JLabel("性别"));
		   bv1.add(Box.createVerticalStrut(8));
		   bv1.add(new JLabel("地址"));
		   bv1.add(Box.createVerticalStrut(8));
		   bv1.add(new JLabel("电话"));
		   bv1.add(Box.createVerticalStrut(8));
		   bv1.add(new JLabel("专业"));
		   bv1.add(Box.createVerticalStrut(8));
		   bv2=Box.createVerticalBox();
		   bv2.add(t1);
		   bv2.add(Box.createVerticalStrut(8));
		   bv2.add(t2);
		   bv2.add(Box.createVerticalStrut(8));
		   bv2.add(t3);
		   bv2.add(Box.createVerticalStrut(8));
		   bv2.add(t4);
		   bv2.add(Box.createVerticalStrut(8));
		   bv2.add(t5);
		   bv2.add(Box.createVerticalStrut(8));
		   bv2.add(t6);
		   bv2.add(Box.createVerticalStrut(8));
		   baseBox=Box.createHorizontalBox();
		   baseBox.add(bv1);
		   baseBox.add(Box.createHorizontalStrut(10));
		   baseBox.add(bv2);
		   p2.add(baseBox);
		   add(p1,"South");
		   add(p2,"Center");
		   setSize(350,300);
		   setBackground(Color.pink);
		  
	}

	
	@Override
	public void actionPerformed(ActionEvent e) {
		// TODO Auto-generated method stub
		if(e.getSource()==b1) {
			try {
				String num,name,gender,address,phone,major;
				con = DriverManager.getConnection("jdbc:mysql://localhost:3306/university?characterEncoding=utf8","root","123456");
				num=t1.getText().trim();
				ResultSet rs = sql.executeQuery("SELECT* FROM student WHERE id = '"+ num +"'");	
				if(rs.next()) {
					//num=rs.getString("id");
					name=rs.getString("name");
					gender=rs.getString("gender");
					address=rs.getString("address");
					phone=rs.getString("phone");
					major=rs.getString("major");
				//if(num.equals(t1.getText().trim())) {
					t2.setText(name);
					t3.setText(gender);
					t4.setText(address);
					t5.setText(phone);
					t6.setText(major);
					//break;
				//}
			}else {
				JOptionPane.showMessageDialog(this,"没有该学生!","提示对话框",JOptionPane.INFORMATION_MESSAGE);
			}
				con.close();
		}
			catch(SQLException ee) {}
	}
		if(e.getSource()==b2) {
			try {
				update();
			}catch(SQLException ee) {}
		}
		if(e.getSource()==b3) {
			t2.setText("");
			t3.setText("");
			t4.setText("");
			t5.setText("");
			t6.setText("");
		}
		
     }
	public void update() throws SQLException{
		String s1="'"+t1.getText().trim()+"'";
		String s2="'"+t2.getText().trim()+"'";
		String s3="'"+t3.getText().trim()+"'";
		String s4="'"+t4.getText().trim()+"'";
		String s5="'"+t5.getText().trim()+"'";
		String s6="'"+t6.getText().trim()+"'";
		String test ="UPDATE student SET name ="+s2+",gender="+s3+",address="+s4+",phone="+s5+",major="+s6+" WHERE id="+s1;
		con = DriverManager.getConnection("jdbc:mysql://localhost:3306/university?characterEncoding=utf8","root","123456");
		sql.executeUpdate(test);
		JOptionPane.showMessageDialog(this,"修改成功!","提示对话框",JOptionPane.INFORMATION_MESSAGE);
		con.close();
	}
}

?五、删除界面

EX12_9_StudentDelete shanchu.java

package exp6;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

import javax.swing.Box;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;

public class EX12_9_StudentDelete extends JPanel implements ActionListener{
	Connection con;
	Statement sql;
	JTextField t1,t2,t3,t4,t5,t6;
	JButton b,b1,b2;
	Box baseBox,bv1,bv2;
	EX12_9_StudentDelete(){
		try{
			Class.forName("com.mysql.cj.jdbc.Driver");
		}
		catch(ClassNotFoundException e){}
		try{
			con = DriverManager.getConnection("jdbc:mysql://localhost:3306/university?characterEncoding=utf8","root","123456");
			sql = con.createStatement();
		}
		catch(SQLException ee){}
		//设置面板布局 为边框布局
		   setLayout(new BorderLayout());
		   JPanel p1=new JPanel();
		   JPanel p2=new JPanel();
		   t1=new JTextField(16);
		   t2=new JTextField(16);
		   t3=new JTextField(16);
		   t4=new JTextField(16);
		   t5=new JTextField(16);
		   t6=new JTextField(16);
		   b=new JButton("删除");  
		   b.addActionListener(this);
		   p1.add(new JLabel("请输入要删除的学生学号:"));
		   p1.add(t1);
		   p1.add(b);
		   add(p1,"North");
		   add(p2,"Center");
		   setSize(350,300);
		   setBackground(Color.pink);
		  
	}
	@Override
	public void actionPerformed(ActionEvent e) {
		// TODO Auto-generated method stub
		if(e.getSource()==t1) {
			try {
				delete();
			}catch(SQLException ee) {}
		}else if(e.getSource()==b) {
			try {
				delete();
			}catch(SQLException ee) {}
	    int n=JOptionPane.showConfirmDialog(this, "确定要删除该学生的信息?","确定",JOptionPane.YES_NO_CANCEL_OPTION);	
	    if(n==JOptionPane.YES_OPTION) {
	    	try {
	    		String s1=""+t1.getText().trim()+"";
	    		String test="DELETE FROM student WHERE id="+s1;
	    		System.out.println(test);
	    		con = DriverManager.getConnection("jdbc:mysql://localhost:3306/university?characterEncoding=utf8","root","123456");
	    		sql.executeUpdate(test);
	    		JOptionPane.showMessageDialog(this,"已经删除成功!","提示对话框",JOptionPane.INFORMATION_MESSAGE);
	    		con.close();
	    	}catch(SQLException ee) {}
	    	
	        }else if(n==JOptionPane.NO_OPTION) {
	        	
	        }
		}
	}
	 
	public void delete() throws SQLException{
		String num,name,gender,address,phone,major;
		con = DriverManager.getConnection("jdbc:mysql://localhost:3306/university?characterEncoding=utf8","root","123456");
		num=t1.getText().trim();
		ResultSet rs = sql.executeQuery("SELECT* FROM student WHERE id = '"+ num +"'");	
		if(rs.next()) {
			num=rs.getString("id");
			name=rs.getString("name");
			gender=rs.getString("gender");
			address=rs.getString("address");
			phone=rs.getString("phone");
			major=rs.getString("major");
		if(num.equals(t1.getText().trim())) {
			t2.setText(name);
			t3.setText(gender);
			t4.setText(address);
			t5.setText(phone);
			t6.setText(major);
		}
	}else {
		JOptionPane.showMessageDialog(this,"没有该学生!","提示对话框",JOptionPane.INFORMATION_MESSAGE);
	}
		con.close();
      }
}

?六、JDBC连接测试类

package exp6;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

public class JDBCTest {

	public static Connection getConnection() {
		String driver = "com.mysql.cj.jdbc.Driver"; 
		String url = "jdbc:mysql://localhost:3306/university"; 
		String username = "root";
		String pwd = "123456";
		try {
			Class.forName(driver); 
			Connection conn = DriverManager.getConnection(url, username, pwd);
																			
			return conn;
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
			return null;
		} catch (SQLException e) {
			e.printStackTrace();
			return null;
		}
	}

	public static void closeAll(Connection conn, PreparedStatement ps, ResultSet rs) {
		try {
			if (rs != null) {
				rs.close();
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
		try {
			if (ps != null) {
				ps.close();
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
		try {
			if (conn != null) {
				conn.close();
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}

	public static void main(String[] args) throws SQLException {

		Connection cc = JDBCTest.getConnection();

		if (!cc.isClosed())

		System.out.println("Succeeded connecting to the Database!");
		Statement statement = cc.createStatement(); 
	

 		String sql = "select * from student";
		ResultSet rs = statement.executeQuery(sql); 
		while (rs.next()) { 
			System.out.println(rs.getString("id") + "");
			
		}

	}
}

?注意:在实现用java连接数据库前先创建好相关的数据库。

  Java知识库 最新文章
计算距离春节还有多长时间
系统开发系列 之WebService(spring框架+ma
springBoot+Cache(自定义有效时间配置)
SpringBoot整合mybatis实现增删改查、分页查
spring教程
SpringBoot+Vue实现美食交流网站的设计与实
虚拟机内存结构以及虚拟机中销毁和新建对象
SpringMVC---原理
小李同学: Java如何按多个字段分组
打印票据--java
上一篇文章      下一篇文章      查看所有文章
加:2021-12-14 15:48:10  更:2021-12-14 15:49:10 
 
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁

360图书馆 购物 三丰科技 阅读网 日历 万年历 2024年11日历 -2024/11/24 5:38:38-

图片自动播放器
↓图片自动播放器↓
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
图片批量下载器
↓批量下载图片,美女图库↓
  网站联系: qq:121756557 email:121756557@qq.com  IT数码