A ramble on visualization prefuse (i)---reading data from a SQL Server database

Source: Internet
Author: User
Tags how to connect to sql server

The "Visual Tools solo Show-----Prefuse example Graphview" introduces the features of the entire Prefuse toolset, the operating flow of the framework, and analyzes and demonstrates the official example Graphview.java.

These days have seen the Prefuse.data, Prefuse.data.expression the specific interface of the package, a general understanding of how the Prefuse framework completes the mapping between the external data and the Prefuse data, and how to accomplish the logical expression through the various classes in the Prefuse.data.expression package , string expressions, common functions (such as subtraction sine cosine budget), and so on.

When I saw Prefuse.data.io.sql, I found a few major classes ConnectionFactory, Databasedatasource, Datasourceworker, and looked at it, and found a connection with Java SQL The way the server database should be similar, so be prepared to start with that piece to do a application. Search on the Internet, found that the predecessors have tasted fresh, see here, but the connection is the MySQL database. You can see from the Prefuse API that this project writer is also very emotional about MySQL. So here's how to connect to SQL Server, which is divided into the following steps:

1. Download the driver package for the SQL server2005 that needs to be connected, including Sqljdbc.jar and Mssqlserver.jar two jars, to be imported into the project.

2. Create tables nodes and edges in SQL server2005 and populate them with values, as shown in:

Nodes table:

  

Edges table:

  

Nodes table data:

  

Edges table Data:

  

3. Use the classes in the Prefuse.data.io.sql package to write program code, as follows:

 Public classExample1121_1 {/**     * @paramargs *@throwsdataioexception*/     Public Static voidMain (string[] args)throwsdataioexception {//------------1------------String Driver= "Com.microsoft.sqlserver.jdbc.SQLServerDriver"; String URL= "Jdbc:sqlserver://localhost:1433;databasename=test"; String username= "SA"; String Password= "123456"; Databasedatasource Dbds=NULL; Try{Dbds=connectionfactory.getdatabaseconnection (Driver, URL, username, password); } Catch(SQLException E1) {e1.printstacktrace (); } Catch(ClassNotFoundException E1) {e1.printstacktrace (); } Table Nodes= Dbds.getdata ("SELECT * FROM Nodes"); Table edges= Dbds.getdata ("select * from Edges"); Visualization Vis=Newvisualization (); Graph Graph=NewGraph (nodes, edges,false, "id", "Sid", "Tid"); Vis.add ("Graph", graph); Labelrenderer label=NewLabelrenderer ("name"); Label.setroundedcorner (10, 10); Vis.setrendererfactory (Newdefaultrendererfactory (label)); int[] palette =New int[]{colorlib.rgb (255, 190, 190, 255)}; Datacoloraction Fill=NewDatacoloraction ("Graph.nodes", "gender", Constants.nominal, Visualitem.fillcolor,palette); Coloraction text=NewColoraction ("Graph.nodes", Visualitem.textcolor, Colorlib.gray (0)); Coloraction edges1=NewColoraction ("Graph.edges", Visualitem.strokecolor, Colorlib.gray (200)); ActionList Color=Newactionlist ();        Color.add (fill);        Color.add (text);                Color.add (EDGES1); ActionList Layout=Newactionlist (activity.infinity); Layout.add (NewForcedirectedlayout ("graph")); Layout.add (Newrepaintaction ()); Vis.putaction ("Color", color); Vis.putaction ("Layout", layout); Display Display=NewDisplay (VIS); Display.setsize (700, 600); Display.addcontrollistener (NewDragcontrol ()); Display.addcontrollistener (NewPancontrol ()); Display.addcontrollistener (NewZoomcontrol ()); Display.addcontrollistener (NewWheelzoomcontrol ()); Display.addcontrollistener (NewFocuscontrol (1)); Display.addcontrollistener (NewZoomtofitcontrol ()); JFrame JF=NewJFrame ();        Jf.setdefaultcloseoperation (Jframe.exit_on_close);        Jf.add (display);        Jf.pack (); Jf.setvisible (true); Vis.run ("Color"); Vis.run ("Layout"); }}

The above code mainly implements the function when reading the information of the node from the nodes table, reading the edge information from the edges table, and passing the statement

labelrenderer label = new Labelrenderer ("name") reads the properties in the nodes table to assign to each node According to the gender attribute in table nodes, there are different colors for male and female sex; add some controllers such as drag, zoom, pan, etc.

  3. The results of the code run are shown below:

 

Through the above steps, the connection between Prefuse and database SQL server2005 is completed, and the information of the points and edges required to read the graph is displayed graphically. So as long as the idea of Prefuse connected database, connected to other database products is the same reason, Prefuse also support JDBC/ODBC database connection.

Original link: http://www.cnblogs.com/bigdataZJ/p/VisualizationPrefuse1.html

A ramble on visualization prefuse (i)---reading data from a SQL Server database

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.