done this the next step is to configure NetBeans to end our connection to the database data:
I. - Conditions |
- have set the Microsoft SQL Server 2005, but did you check out this entry :
- NetBeans IDE
- SQL Server Connector for Java, which can be downloaded here .
- An application in Java, but here you'll create a simple.
- The Pubs database, if not please add to your database You can do this form.
II .- CREATION PROJECT |
- Open the NetBeans and create our project in which we will put a JPanel (PanelPrincipal) and JFrame (Main), go to PanelPrincipal to which we give Border Layout, in the north and place a button in the center of a Text Area:
| NOTE: Before positioning Txt Area is recommended to add a JScrollPane to display content that exceeds the limits of textArea |
- Now we go to the JFrame called Home where we add the previously created panel:
public Main () {
initComponents ();
add (new PanelPrincipal ());
setSize (500.400);}
III .- ADDING TO PROJECT JAR |
- Unzip the connector had downloaded previously.
- And we will find the following folders:
- add the libraries sqljdbc4 sqljdbc and our project for this right-click the Add JAR Libraries →
& # 160;
IV .- ESTABLISHING THE CONNECTION |
- We go to the panel Panel pricipalmente name and import the following libraries: import java
. sql.Connection;
java.sql.DriverManager import;
import java.sql.ResultSet;
java.sql.ResultSetMetaData import;
import java.sql.SQLException;
import java.sql.Statement;
import java.util . logging.Level;
java.util.logging.Logger import;
- We will design graph and we double-click the Browse button to set your action and we add the following code: private void
btnConsultaActionPerformed (java.awt.event.ActionEvent evt) {String
connectionUrl = "jdbc: sqlserver: / / localhost: 1433; "+
" databaseName = pubs; user = sa, password = adminadmin; "
/ / Declare the objects sioguientes
Connection con = null;
Statement stmt = null;
ResultSet rs = null;
try {/ / establish the connection
Class.forName ("com.microsoft.sqlserver.jdbc.SQLServerDriver");
con = DriverManager.getConnection(connectionUrl);
// Create and execute an SQL statement that returns some data.
String SQL = "SELECT * FROM AUTHORS";
stmt = con.createStatement();
rs = stmt.executeQuery(SQL);
this.muestraData(rs);
}
catch (Exception e)
{
e.printStackTrace();
}
finally {
if (rs != null) try { rs.close(); } catch(Exception e) {}
if (stmt != null) try { stmt.close(); } catch(Exception e) {}
if (con != null) try { con.close ();} catch (Exception e) {}}
}
- Finally we create our method public void
muestraData muestraData (ResultSet r) throws Exception {
ResultSetMetaData
rmeta = r . getMetaData ();
();// int numColumnas = rmeta.getColumnCount many columns
txtSalida.setText
(""); for (int i = 1; i \u0026lt;= numColumnas; + + i)
/ / get column name
txtSalida.append (rmeta.getColumnName (i) + "\\ t");
txtSalida.append ("\\ n");
while (r.next ())
{/ / until EOF
for (int i = 1; i \u0026lt;= numColumnas; + + i) {
txtSalida.append (r.getString (i) + " \\ t ");}
txtSalida.append (" \\ n ");
}}
- done this run the project and we will leave as follows:
- Example you can download it here:
V. - ERRORS |
- needs updating to SQL Server 2005 Service Pack 3 for this to work.
0 comments:
Post a Comment