[java]代码库
package JDBC;
import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.sql.Connection;
import java.sql.Date;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Vector;
import javax.swing.*;
import javax.swing.table.DefaultTableModel;
public class JDBC extends JFrame{
//用户名 账号登入 private static final String URL = "jdbc:sqlserver://挚爱:1433;user=sa;password=1234;DatabaseName=HuffmanTree";
//服务器自动登入 private static final String URL = "jdbc:sqlserver://挚爱:1433;integrated Security=true;DatabaseName=HuffmanTree";
private static final String URL = "jdbc:sqlserver://挚爱:1433;DatabaseName=HuffmanTree";
private static final String USERNAME = "sa";
private static final String PASSWORD = "1234";
static {
try {
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
JDBC frame = new JDBC();
frame.setVisible(true);
}
public JDBC(){
super();
setTitle("Huffman编码译码展示");
setBounds(100,100,480,420);
getContentPane().setLayout(null);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
try {
Connection conn = DriverManager.getConnection(URL,USERNAME,PASSWORD);
Statement statement = conn.createStatement(); //静态
//----------------------------------------------------------------------------------------
JPanel panel1 = new JPanel(); //创建label“请输入编码内容 ”
//panel1.setLayout(new FlowLayout());
final JLabel label = new JLabel();
label.setText("请输入编码内容:");
panel1.add(label);
JTextField textField = new JTextField();
textField.setColumns(20);
panel1.add(textField);
JButton button = new JButton();
button.setText("转换");
panel1.add(button);
panel1.setBounds(0, 0, 450, 35);
getContentPane().add(panel1);
//-----------------------------------------------------------------------------------------
JPanel panel2 = new JPanel(); //创建数据库表
BorderLayout borderLayout2 = new BorderLayout();
panel2.setLayout(borderLayout2);
final JLabel label1 = new JLabel();
label1.setText("Huffman树的结点数组:\n");
panel2.add(label1,BorderLayout.NORTH);
JScrollPane jScrollPane1 = new JScrollPane();
panel2.add(jScrollPane1);
panel2.add(jScrollPane1,BorderLayout.CENTER);
Object[][] rowData = {
{"1001", "李汉", "软件部", new Double(3000)},
{"1002", "朱泽", "软件部", new Double(3100)},
{"1003", "刘宇", "经理部", new Double(3000)},
{"1004", "刘宇", "经理部", new Double(3000)},
{"1005", "刘宇", "经理部", new Double(3000)},
{"1006", "刘宇", "经理部", new Double(3000)},
{"1007", "刘宇", "经理部", new Double(3000)},
{"1008", "刘宇", "经理部", new Double(3000)},
};
Object[] columnsName = {"data", "parent", "left", "right"};
DefaultTableModel dtm = new DefaultTableModel(rowData, columnsName);
JTable jTable1 = new JTable(dtm);
jScrollPane1.getViewport().add(jTable1);
panel2.setBounds(2,50,460,190);
getContentPane().add(panel2);
//----------------------------------------------------------------------------------------------
//String sql = "select * from Table_1";
//ResultSet rs = statement.executeQuery(sql); //
//---------------------------------------------------------------------------------------------------
statement.close();
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}