Access SQL Server 2000 database with Java

zhaozj2021-02-16  67

Abstract: This article mainly introduces the use of JDBC, the ODBC interface enables connectivity to the SQL Server2000 database, and uses the Java application to access it, and simply realize a simple query function through the graphical user interface. Foreword database technology and network technology are two hot topics in the current computer field. Since the production of database technology, it has been developed in technology. The development tools and the environment as front-end access is continuously improved and developed. In addition to the ASP, PHP, JSP used on the network, which is commonly used on small systems to access database technology on small systems with Delphi, Visual Basic, Powerbuilder. And VC , etc., while Java is also one of them.

The Java language is one of the outstanding languages ​​of writing database applications, which provides technology for accessing data. Using JDBC technology in Java language, users can easily develop web-based database access procedures to expand network application functions. JDBC (Java Database Connectivity, Java Database Connection) is a Java API for executing a SQL statement that provides a unified access interface for a variety of relational databases. JDBC consists of a set of classes and interfaces written in Java language. By calling these classes and interfaces, users can connect a variety of different database systems (such as Access, SQL Server 2000, Oracle, Sybase, etc.) in consistent ways. ), In turn, you can use standard SQL languages ​​to access data in the database without having to write different Java program code for each database system. SQL Server2000 is a new generation of database products in Microsoft, which is generated on the basis of SQL Server 7.0, and has made a lot of extensions for SQL Server 7.0. SQL Server2000 provides scalability and high reliability for maximum Web site and enterprise applications through high-end hardware platforms and the latest network and storage technology. Java Access Database 1 JDBC, ODBC Interface Access Database 1 JDBC, ODBC Interface Java Application Connects through JDBC API (Java.sql), and actual action is by JDBC Driver Manager through JDBC drivers and The database system is connected. ODBC (Open Database Connectivity) open interface, provides users with a standard interface for accessing relational databases, which provides a unified API for different databases; allows applications to access any ODBC drivers via API The database's database, and all the current relational databases provide an ODBC driver, so ODBC has become the industry standard for database access and has been widely used. The JDBC-ODBC bridge is a JDBC driver that converts the JDBC operation to an ODBC operation. Using the JDBC-ODBC bridge allows program developers to write JDBC applications without learning more knowledge, and can make full use of existing ODBC data sources. The JDBC-ODBC bridge driver allows JDBC to access almost all types of databases. 2 Use the Java application to access the SQL Server2000 database (1) to establish a database to start "Microsoft SQL Server2000", open the "Enterprise Manager" to create a database named "MyData" in the "Database", and the name "wuzi "The data sheet, as shown in Figure 1.

Figure 1 (2) Establishing (ODBC) Data Sources and Drivers Open the ODBC Data Source Manager dialog box by "Management Tool" on the control panel, click System DSN tab , Then click the "Add" button to get the "Create Data Source" dialog box, select "SQL Server" and click the "Finish" button to "create the data source to the SQL Server" dialog box "data Source Name "Fill in" WZGL "and select" Server Name ", then click the" Next "button, select" Windows NT Verification "item using the Network Login ID, click Next button to change the default database To "MyData", click Next, click the "Finish" button, then click "Test Data Source", after success, click the "OK" button to complete the (ODBC) data source and driver Establishment. 3 Write the code to save the code in the jdbc.java file:

Import java.awt. *; import java.awt.event. *; import java.sql. *; public class jdbc // Define primary class {public static void main (string args []) {GUI GUI = New GUI () ; // Create a class GUI object gui.pack (); // Load execution of GUI class}} class gUI extends frame import; panel sextfield sno; button btn; gui () // Structure { Super ("Material Inquiry"); setLayout (New BorderLayout ()); setBackground (color.cyan); setvisible (true); text = new textarea (); btn = new button ("query)); SNO = New TextField (16); panel = new panel (); Panel.Add (New Label ("Enter the query supplied:")); panel.add (sno); panel.add (btn); add ("north", panel); add (text, "Center"); text.setEditable (false); btn.addActionListener (this); addWindowListener (new WindowAdapter () {public void windowClosing (WindowEvent e) {setVisible (false); System.exit ( 0);}});} public void actionperformed (ActionEvent E) {if (E.GetSource () == btn) // {text.setText ("Query results" '/ n' when the user presses the query button ); // Display prompt information try {liststudent ();} catch (sqlexception ee) {}}} public void li StStudent () THROWS SQLEXCEPTION / / Operation for the database {String BH, MC, XH, LB, DW, SJ; Int Sl; Float Dj, Je; Try {Class.Forname ("Sun.jdbc.odbc.jdbCodbcdriver"); } catch (ClassNotFoundException e) {} Connection con = DriverManager.getConnection ( "jdbc: odbc: wzgl"); Statement sql = con.createStatement (); // create Statement object ResultSet rs = sql.executeQuery ( "select * from wuzi "); While (rs.next ()) // Output The case of the query {BH = rs.getstring (" Material No. "); MC = rs.getstring (" Material Name "); XH = rs.getstring (" Specification model "); lb = rs.getstring (" Category "); dw = rs.getstring (" Metering Unit)); SL ​​= rs.getint ("Quantity); DJ =

rs.getfloat; JE = rs.getfloat ("amount"); sj = rs.getdate ("time") .tostring (); if (bh.trim (). Equals (sno.getText () .trim ())) {text.append ('/ n' "material number" "" "" "" "specification model" " " " " " " unit unit " " " " Quantity " " " " single price " " " " " " " " " " TEXT.APPpend ('/ n' bh " mc " XH " LB "" DW "" " " " " "" "" '/ n');}}}} 4 run

First, add: Javac JDBC.JAVA

After successful compilation: Java JDBC

After execution, enter the material number you want to query the supplies in the text box, click the Query button, display all the information of the query supplies in the text box below. as shown in picture 2.

figure 2

Conclude

This article only involves query operations accessed by SQL Server2000 database, or insert, delete, modify, etc. With the rapid development of database technology and the continuous upgrade of Java versions, access to database access is getting simpler, and the scope of the application will become wider and wider, of course, there will be many new features waiting to be developed.

Related Resources: Java connection SQLServer2000 and database operations

转载请注明原文地址:https://www.9cbs.com/read-17056.html

New Post(0)