Java 数据库查询
来源:互联网 发布:河北经济网络直播 编辑:程序博客网 时间:2024/06/05 14:19
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.util.*;
import java.sql.*;
public class test extends JFrame implements ActionListener{
public static Connection connectByJdbcOdbc(String url, String username,String password){
Connection con = null;
try{
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver"); //加载ODBC驱动程序
}
catch (Exception e){
e.printStackTrace();
return null; //加载失败,连接不成功
}
try{
con = DriverManager.getConnection(url, username, password);
}
catch (SQLException e){
e.printStackTrace();
return null; //连接失败
}
return con; //连接成功
}
String title[] ={"学号", "姓名", "学分", "地址", "简历"};
JTextField txtNo = new JTextField(8);
JTextField txtName = new JTextField(10);
JTextField txtScore = new JTextField(3);
JTextField txtAddr = new JTextField(30);
JTextArea txtresume = new JTextArea();
JButton prev = new JButton("前一个");
JButton next = new JButton("后一个");
JButton first = new JButton("第一个");
JButton last = new JButton("最后一个");
Statement sql; //SQL语句对象
ResultSet rs; //存放查询结果对象
test(Connection connect){
super("考生信息查看窗口");
setSize(450, 350);
try{
sql = connect.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_READ_ONLY);
rs = sql.executeQuery("SELECT * FROM ksInfo");
Container con = getContentPane();
con.setLayout(new BorderLayout(0,6)); JPanel p[] = new JPanel[4];
for (int i = 0; i < 4; i++){
p[i] = new JPanel(new FlowLayout(FlowLayout.LEFT, 8, 0));
p[i].add(new JLabel(title[i]));
}
p[0].add(txtNo);
p[1].add(txtName);
p[2].add(txtScore);
p[3].add(txtAddr);
JPanel p1 = new JPanel(new GridLayout(4, 1, 0, 8));
JScrollPane jsp = new JScrollPane(txtresume,
JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,
JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
jsp.setPreferredSize(new Dimension(300, 60));
for (int i = 0; i < 4; i++){
p1.add(p[i]);
}
JPanel p2 = new JPanel(new FlowLayout(FlowLayout.LEFT, 10, 0));
p2.add(new JLabel(title[4]));
p2.add(jsp);
JPanel p3 = new JPanel();
p3.add(prev);
p3.add(next);
p3.add(first);
p3.add(last);
prev.addActionListener(this);
next.addActionListener(this);
first.addActionListener(this);
last.addActionListener(this);
rs.first();
readRecord();
con.add(p1,"North");
con.add(p2,"Center");
con.add(p3,"South");
}
catch (Exception e){
e.printStackTrace();
}
// setVisible(true);
}
public void modifyRecord(Connection connect){
String stuNo = (String)JOptionPane.showInputDialog(null,
"请输入考生考号", "输入考号对话框", JOptionPane.PLAIN_MESSAGE, null,
null, "");
try {
sql = connect.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_READ_ONLY);
String SQLStr="SELECT * FROM ksInfo where txtNo='"+stuNo+"'";
rs = sql.executeQuery(SQLStr);
Container con = getContentPane();
con.setLayout(new BorderLayout(0, 6));
JPanel p[] = new JPanel[4];
for (int i = 0; i <4 ; i++){
p[i] = new JPanel(new FlowLayout(FlowLayout.LEFT, 8, 0));
p[i].add(new JLabel(title[i]));
}
p[0].add(txtNo);
p[1].add(txtName);
p[2].add(txtScore);
p[3].add(txtAddr);
JPanel p1 = new JPanel(new GridLayout(4, 1, 0, 8));
JScrollPane jsp = new JScrollPane(txtresume,
JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,
JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
jsp.setPreferredSize (new Dimension(300, 60));
for (int i = 0; i < 4; i++){
p1.add(p[i]);
}
JPanel p2 = new JPanel(new FlowLayout(FlowLayout.LEFT, 10, 0));
p2.add(new JLabel(title[4]));
p2.add(jsp);
JPanel p3 = new JPanel();
p3.add(prev);
p3.add(next);
p3.add(first);
p3.add(last);
prev.addActionListener(this);
next.addActionListener(this);
first.addActionListener(this);
last.addActionListener(this);
rs.first();
readRecord();
con.add(p1,"North");
con.add(p2,"Center");
con.add(p3,"South");
}
catch (Exception e){
e.printStackTrace();
}
setVisible(true);
}
boolean readRecord(){
try{
txtNo.setText(rs.getString("txtNo")); txtName.setText(rs.getString("txtName")); txtScore.setText(rs.getString("txtScore"));
txtAddr.setText(rs.getString("txtAddr")); txtresume.setText(rs.getString("txtresume"));
}
catch (SQLException e){
e.printStackTrace(); return false;
}
return true;
}
public void actionPerformed(ActionEvent e){
try{
if (e.getSource() == prev && (!rs.isFirst()))rs.previous();
else if (e.getSource() == next && (!rs.isLast()))rs.next();
else if (e.getSource() == first)rs.first();
else if (e.getSource() == last)rs.last(); readRecord();
}
catch (Exception e2){}
}
public static void main(String args[]){
Connection connect = null;
JFrame.setDefaultLookAndFeelDecorated(true);
Font font = new Font("JFrame", Font.PLAIN, 14);
if ((connect =connectByJdbcOdbc("jdbc:sqlserver://localhost:1433;Databasename=master", "sa", "dbn.cn123")) == null){
JOptionPane.showMessageDialog(null, "数据库连接失败!");
System.exit ( - 1);
}
test a=new test(connect); //创建对象
a.modifyRecord(connect);
}
}
import java.awt.event.*;
import javax.swing.*;
import java.util.*;
import java.sql.*;
public class test extends JFrame implements ActionListener{
public static Connection connectByJdbcOdbc(String url, String username,String password){
Connection con = null;
try{
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver"); //加载ODBC驱动程序
}
catch (Exception e){
e.printStackTrace();
return null; //加载失败,连接不成功
}
try{
con = DriverManager.getConnection(url, username, password);
}
catch (SQLException e){
e.printStackTrace();
return null; //连接失败
}
return con; //连接成功
}
String title[] ={"学号", "姓名", "学分", "地址", "简历"};
JTextField txtNo = new JTextField(8);
JTextField txtName = new JTextField(10);
JTextField txtScore = new JTextField(3);
JTextField txtAddr = new JTextField(30);
JTextArea txtresume = new JTextArea();
JButton prev = new JButton("前一个");
JButton next = new JButton("后一个");
JButton first = new JButton("第一个");
JButton last = new JButton("最后一个");
Statement sql; //SQL语句对象
ResultSet rs; //存放查询结果对象
test(Connection connect){
super("考生信息查看窗口");
setSize(450, 350);
try{
sql = connect.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_READ_ONLY);
rs = sql.executeQuery("SELECT * FROM ksInfo");
Container con = getContentPane();
con.setLayout(new BorderLayout(0,6)); JPanel p[] = new JPanel[4];
for (int i = 0; i < 4; i++){
p[i] = new JPanel(new FlowLayout(FlowLayout.LEFT, 8, 0));
p[i].add(new JLabel(title[i]));
}
p[0].add(txtNo);
p[1].add(txtName);
p[2].add(txtScore);
p[3].add(txtAddr);
JPanel p1 = new JPanel(new GridLayout(4, 1, 0, 8));
JScrollPane jsp = new JScrollPane(txtresume,
JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,
JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
jsp.setPreferredSize(new Dimension(300, 60));
for (int i = 0; i < 4; i++){
p1.add(p[i]);
}
JPanel p2 = new JPanel(new FlowLayout(FlowLayout.LEFT, 10, 0));
p2.add(new JLabel(title[4]));
p2.add(jsp);
JPanel p3 = new JPanel();
p3.add(prev);
p3.add(next);
p3.add(first);
p3.add(last);
prev.addActionListener(this);
next.addActionListener(this);
first.addActionListener(this);
last.addActionListener(this);
rs.first();
readRecord();
con.add(p1,"North");
con.add(p2,"Center");
con.add(p3,"South");
}
catch (Exception e){
e.printStackTrace();
}
// setVisible(true);
}
public void modifyRecord(Connection connect){
String stuNo = (String)JOptionPane.showInputDialog(null,
"请输入考生考号", "输入考号对话框", JOptionPane.PLAIN_MESSAGE, null,
null, "");
try {
sql = connect.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_READ_ONLY);
String SQLStr="SELECT * FROM ksInfo where txtNo='"+stuNo+"'";
rs = sql.executeQuery(SQLStr);
Container con = getContentPane();
con.setLayout(new BorderLayout(0, 6));
JPanel p[] = new JPanel[4];
for (int i = 0; i <4 ; i++){
p[i] = new JPanel(new FlowLayout(FlowLayout.LEFT, 8, 0));
p[i].add(new JLabel(title[i]));
}
p[0].add(txtNo);
p[1].add(txtName);
p[2].add(txtScore);
p[3].add(txtAddr);
JPanel p1 = new JPanel(new GridLayout(4, 1, 0, 8));
JScrollPane jsp = new JScrollPane(txtresume,
JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,
JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
jsp.setPreferredSize (new Dimension(300, 60));
for (int i = 0; i < 4; i++){
p1.add(p[i]);
}
JPanel p2 = new JPanel(new FlowLayout(FlowLayout.LEFT, 10, 0));
p2.add(new JLabel(title[4]));
p2.add(jsp);
JPanel p3 = new JPanel();
p3.add(prev);
p3.add(next);
p3.add(first);
p3.add(last);
prev.addActionListener(this);
next.addActionListener(this);
first.addActionListener(this);
last.addActionListener(this);
rs.first();
readRecord();
con.add(p1,"North");
con.add(p2,"Center");
con.add(p3,"South");
}
catch (Exception e){
e.printStackTrace();
}
setVisible(true);
}
boolean readRecord(){
try{
txtNo.setText(rs.getString("txtNo")); txtName.setText(rs.getString("txtName")); txtScore.setText(rs.getString("txtScore"));
txtAddr.setText(rs.getString("txtAddr")); txtresume.setText(rs.getString("txtresume"));
}
catch (SQLException e){
e.printStackTrace(); return false;
}
return true;
}
public void actionPerformed(ActionEvent e){
try{
if (e.getSource() == prev && (!rs.isFirst()))rs.previous();
else if (e.getSource() == next && (!rs.isLast()))rs.next();
else if (e.getSource() == first)rs.first();
else if (e.getSource() == last)rs.last(); readRecord();
}
catch (Exception e2){}
}
public static void main(String args[]){
Connection connect = null;
JFrame.setDefaultLookAndFeelDecorated(true);
Font font = new Font("JFrame", Font.PLAIN, 14);
if ((connect =connectByJdbcOdbc("jdbc:sqlserver://localhost:1433;Databasename=master", "sa", "dbn.cn123")) == null){
JOptionPane.showMessageDialog(null, "数据库连接失败!");
System.exit ( - 1);
}
test a=new test(connect); //创建对象
a.modifyRecord(connect);
}
}
0 0
- Java 数据库查询
- Java数据库查询简介
- 用JAVA实现数据库查询
- Java查询MySQL数据库示例
- JAVA数据库查询操作(JDBC)
- java连接数据库(查询语句)
- java查询数据库表卡死,解决方法
- java入门教程-12.6Java数据库查询简介
- 【Java】如何用Java实现数据库查询
- Java数据库查询结果的输出
- 通过java查询数据库表结构
- 通过java查询数据库表结构
- 通过java查询数据库表结构
- java查询数据库表的相关信息
- Java数据库通用查询及封装
- java连接mysql数据库查询员工信息
- Java连接MySQL数据库并查询结果
- java 结束掉一个查询数据库进程
- HDU1394 Minimum Inversion Number 求逆序数+树状数组
- Android核心基础
- android核心技术之性能分析工具Systrace
- css3
- sql中 truncate 、delete与drop区别
- Java 数据库查询
- Java与Swift对比
- 解析elasticsearch的config下的配置文件
- jdk动态代理示例以及代码原理分析
- nginx启动异常
- qmake和tmake的区别
- cocos Creator[框架封装之一] 打印打印 protocol buffer 协议
- [!] Unable to find a pod with name, author, summary, or ...
- 在配置无密码登录的时候,ssh老是提示输入密码