⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 jdbcapp.java

📁 这是一个SQL在JAVA 程序设计中怎么使用的例子
💻 JAVA
字号:
import java.awt.*;
import java.awt.event.*;
import java.sql.*;
import com.ms.jdbc.odbc.*;

public class JDBCApp extends Frame implements ActionListener 
{
		Connection con;
		Statement s;
		ResultSet R;
		
		Label l1,l2,l3;
		Button button1;
		Button button2;
		Button searchbutton;
		TextField searchtext;
		TextField idtext;
		TextField authortext;
		TextField yearborntext;
	private void ExecuteIt(int h)
	{
		try{
			// mine here refers to your DSN Name which include biblio.mdb
			con= DriverManager.getConnection("jdbc:odbc:mine");
			s= con.createStatement();
			if (h==1)
			//authors is a table inside biblio.mdb
			//we retrive all data by SQL statement	
			R = s.executeQuery("select * from authors");
			else
			//Query to search any author by he's name or part of he's name	
			R = s.executeQuery("select * from authors where author like '" +
							   searchtext.getText() + "%'");	
			R.next();
			GetIT();}catch(Exception o){}
			
	}
	private void GetIT()
	{
		try{
		idtext.setText(String.valueOf(R.getInt(1)));
		authortext.setText(R.getString(2));
		yearborntext.setText(String.valueOf(R.getInt(3)));}
		catch (Exception m){}
	}
	public static void main (String arg[])
	{
		new JDBCApp("Mine");
		
	}
	public JDBCApp(String title)
	{   super(title);
				
		init();
	}
	public void init()
	{
		
		new JdbcOdbcDriver();
		this.setLayout(null);
		this.setForeground(Color.blue);
		searchtext = new TextField();
		idtext = new TextField();
		authortext = new TextField();
		yearborntext = new TextField();
		idtext.setLocation(200,200);
		idtext.setSize(30,25);
		authortext.setLocation(200,250);
		authortext.setSize(100,25);
		yearborntext.setLocation(200,300);
		yearborntext.setSize(50,25);
		add(idtext);
		add(authortext);
		add(yearborntext);
		searchbutton = new Button("Search");
		button1 = new Button("Next");
		button2 = new Button("Exit");
		searchbutton.setLocation(300,120);
		searchtext.setLocation(120,120);
		searchtext.setSize(100,25);
		searchbutton.setSize(50,20);
		button1.setLocation(200,400);
		button2.setLocation(400,400);
		button1.setSize(50,20);
		button2.setSize(50,20);
		add(searchtext);
		add(searchbutton);
		add(button1);
		add(button2);
		
		button1.addActionListener(this);
		button2.addActionListener(this);
		searchbutton.addActionListener(this);
		this.setSize(600,460);
		this.setVisible(true);
		
	    ExecuteIt(1);
		
		
		ResultSetMetaData M;
		Graphics gh;
		gh = getGraphics();
				
		try {
		M = R.getMetaData();
		l1 = new Label(M.getColumnName(1));
		l2 = new Label(M.getColumnName(2));
		l3 = new Label(M.getColumnName(3));}
		
		catch(Exception k){}
		
		l1.setLocation(140,200);
		l2.setLocation(140,250);
		l3.setLocation(140,300);
		l1.setSize(50,30);
		l2.setSize(50,30);
		l3.setSize(50,30);
		
		add(l1); add(l2); add(l3);
	

	}
	public void actionPerformed(ActionEvent e)
	{
		if (e.getSource().equals(button1))
				try{
				R.next();
				
				GetIT();
			}catch(Exception x){}
				
		if (e.getSource().equals(button2))
				    System.exit(0);
		
				
		if (e.getSource().equals(searchbutton))
				ExecuteIt(0);
				
		}
			
				
						

		
			
	}
	

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -