



全文预览已结束
下载本文档
版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
Ex4.8(数据库连接)import java.sql.Connection;import java.sql.DriverManager;public class ConnectionTestpublic static void main(String args )throws Exception Class.forName(com.mysql.jdbc.Driver); /加载数据库驱动 String url=jdbc:mysql:/localhost:3306/xsgl; Connection con=DriverManager.getConnection(url,root,1234);EX:P254(登陆界面-1)import java. sql. * ;public class DBConnectionConnection con=null; /数据库连接对象public DBConnection( )tryClass.forName(com.mysql.jdbc.Driver); /加载数据库驱动con=DriverManager.getConnection(jdbc:mysql:/localhost:3306/xsgl,root,1234);catch(Exception e)System.out.print(数据库访问失败+e.getMessage( );EX:P255(登陆界面-2)import java.awt.event. * ;import java.awt. * ;import java.sql.Connection;import java.sql.ResultSet;import java.sql.Statement;import javax.swing. * ;public class Login implements ActionListenerJFrame f;JLabel l1;JLabel l2;JTextField jt1;JTextField jt2;JButton an1;JButton an2;public Login()f=new JFrame();f.setTitle(学生登陆);f.setLayout(new FlowLayout() );f.setLocation(400,300);l1=new JLabel(请输入学号:);l2=new JLabel(请输入姓名:);jt1=new JTextField(15);jt2=new JTextField(15);an1=new JButton(登陆);an2=new JButton(取消);an1.addActionListener(this);f.add(l1);f.add(jt1);f.add(l2);f.add(jt2);f.add(an1);f.add(an2);f.setSize(300,200);f.setResizable(false);f.setVisible(true);public static void main(String args )new Login();public void actionPerformed(ActionEvent e)String xuehao=jt1.getText( );String xingming=jt2.getText( );DBConnection db=new DBConnection( );Connection conn=db.con;tryStatement stmt= conn.createStatement( );ResultSet rs=stmt.executeQuery(select*from student where stuno= +xuehao+and name=+xingming+);if(rs.next() ) JOptionPane.showConfirmDialog(null,登陆成功!,提示信息,JOptionPane.DEFAULT_OPTION,JOptionPane.INFORMATION_MESSAGE);elseJOptionPane.showConfirmDialog(null,登陆失败!,提示信息,JOptionPane.DEFAULT_OPTION,JOptionPane.INFORMATION_MESSAGE);catch(Exception el) System.out.print(查询不成功+el.getMessage( ) );Ex4.10(插入操作)import java. sql.Connection;import java. sql.DriverManager;import java. sql.Statement;public class DataInsertTestpublic static void main(String args )throws ExceptionClass.forName(com.mysql.jdbc.Driver);String url=jdbc:mysql:/localhost:3306/xsgl;Connection con=DriverManager.getConnection(url,root,1234);Statement st=con.createStatement( );String sql=insert into student(stuno,name)values(lnjd12303,angle);st.executeUpdate(sql);st.close( );con.close( ); Ex4.11(插入操作-Statement类)import java.sql.Connection;import java.sql.DriverManager;import java.sql.Statement;public class DataInsertTestpublic static void main(String args )throws ExceptionString stuno=;String name=;Class.forName(com.mysql.jdbc.Driver);String url=jdbc:mysql:/localhost:3306/xsgl;Connection con=DriverManager.getConnection(url,root,1234);Statement st=con.createStatement( );String sql=insert into student(stuno,name)values(+stuno+,+name+);st.executeUpdate(sql);st.close( );con.close( );Ex4.13(删除操作)import java.sql.Connection;import java.sql.DriverManager;import java.sql.Statement;public class DataDeleteTestpublic static void main(String args )throws ExceptionClass.forName(com.mysql.jdbc.Driver);String url=jdbc:mysql:/localhost:3306/xsgl;Connection con=DriverManager.getConnection(url,root,1234);Statement st=con.createStatement( );String sql=delete from student where stuno=lnjd12303;st.executeUpdate(sql);st.close( );con.close( );Ex4.14(修改操作)import java.sql.Connection;import java.sql.DriverManager;import java.sql.Statement;public class DataUpdateTestpublic static void main(String args )throws ExceptionClass.forName(com.mysql.jdbc.Driver);String url=jdbc:mysql:/localhost:3306/xsgl;Connection
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 2025年上海市公寓买卖合同
- 2025笔记本电脑采购协议范本
- 巧克力成型工技能操作考核试卷及答案
- 重金属煅烧工特殊工艺考核试卷及答案
- 第12课 封建制度说课稿-2025-2026学年高中历史华东师大版上海第一分册-华东师大版上海2007
- 预包装食品知识培训
- 2025企业间的借款合同范本
- 家电生产科室人才培养计划
- 会计从业考试录指纹及答案解析
- 氧化钨制备工数字化技能考核试卷及答案
- 专职安全生产管理人员综合类C证习题(含答案)
- 2025年市场监管总局机关公开遴选公务员面试模拟题及答案
- 注册安全工程师考试道路运输和其他安全安全生产实务(初级)试卷与参考答案
- 构建餐饮油烟共治格局的策略及实施路径
- 《MySQL数据库教程》课件第一章数据库概述
- 2025年“中央八项规定”精神学习知识竞赛测试题库及答案
- 铝单板幕墙专项方案
- GB/T 42062-2022医疗器械风险管理对医疗器械的应用
- 第六节-食品新资源的开发-课件
- 复变函数-西安交大版课件
- 南方医大护理学导论教案
评论
0/150
提交评论