我正在尝试将sql结果显示到JLabel中。在数据库中有一个名为courses的表。在该表中有4列(id,name,img_path,description)我目前正在尝试仅显示课程名称。当我将结果打印到命令行时,它工作。但是当我试图在JLabel中显示结果时。什么都没发生。
这是用于连接数据库的db.java文件
package my.welcomescreen;
import java.sql.*;
public class db {
public static Connection dbconnect(){
Connection con=null;
try {
Class.forName("com.mysql.jdbc.Driver");
con = DriverManager.getConnection("jdbc:mysql://localhost/st2", "root", "");
}
catch (ClassNotFoundException | SQLException e){
System.out.println(e);
}
return con;
}
}
这是包含主要方法
的Welcome_screen.java文件package my.welcomescreen;
import java.sql.*;
import javax.swing.*;
public class Welcome_screen extends javax.swing.JFrame {
public Welcome_screen() {
initComponents();
}
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {
jPanel1 = new javax.swing.JPanel();
jLabel1 = new javax.swing.JLabel();
signin_btn = new javax.swing.JButton();
signup_btn = new javax.swing.JButton();
jLabel2 = new javax.swing.JLabel();
jLabel3 = new javax.swing.JLabel();
jLabel4 = new javax.swing.JLabel();
jLabel5 = new javax.swing.JLabel();
jLabel6 = new javax.swing.JLabel();
jLabel7 = new javax.swing.JLabel();
cname = new javax.swing.JLabel();
jLabel8 = new javax.swing.JLabel();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
setBackground(new java.awt.Color(102, 255, 102));
jPanel1.setBackground(new java.awt.Color(102, 255, 102));
jPanel1.addInputMethodListener(new java.awt.event.InputMethodListener() {
public void caretPositionChanged(java.awt.event.InputMethodEvent evt) {
}
public void inputMethodTextChanged(java.awt.event.InputMethodEvent evt) {
jPanel1InputMethodTextChanged(evt);
}
});
jLabel1.setFont(new java.awt.Font("SansSerif", 0, 58)); // NOI18N
jLabel1.setForeground(new java.awt.Color(255, 255, 255));
jLabel1.setText("WELCOME");
signin_btn.setText("Sign in");
signin_btn.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
signin_btnActionPerformed(evt);
}
});
signup_btn.setText("Sign up");
signup_btn.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
signup_btnActionPerformed(evt);
}
});
jLabel2.setFont(new java.awt.Font("SansSerif", 1, 58)); // NOI18N
jLabel2.setForeground(new java.awt.Color(255, 255, 255));
jLabel2.setText("to");
jLabel3.setFont(new java.awt.Font("SansSerif", 0, 58)); // NOI18N
jLabel3.setForeground(new java.awt.Color(255, 255, 255));
jLabel3.setText("E-Tutes !");
jLabel4.setFont(new java.awt.Font("SansSerif", 0, 14)); // NOI18N
jLabel4.setForeground(new java.awt.Color(255, 255, 255));
jLabel4.setText("E-tutes is a great place for learn new programming languages.");
jLabel5.setFont(new java.awt.Font("SansSerif", 0, 14)); // NOI18N
jLabel5.setForeground(new java.awt.Color(255, 255, 255));
jLabel5.setText(" You can find learn all the popular programming languges and frameworks in one place !");
jLabel6.setFont(new java.awt.Font("SansSerif", 0, 14)); // NOI18N
jLabel6.setForeground(new java.awt.Color(255, 255, 255));
jLabel6.setText("It's totally free ! You can follow any of our courses for free with no hidden costs !");
jLabel7.setFont(new java.awt.Font("SansSerif", 0, 14)); // NOI18N
jLabel7.setForeground(new java.awt.Color(255, 255, 255));
jLabel7.setText("Don't miss this opportunity ! ");
jLabel8.setBackground(new java.awt.Color(153, 255, 255));
javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
jPanel1.setLayout(jPanel1Layout);
jPanel1Layout.setHorizontalGroup(
jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
.addGap(0, 0, Short.MAX_VALUE)
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
.addComponent(jLabel4, javax.swing.GroupLayout.PREFERRED_SIZE, 403, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(157, 157, 157))
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
.addComponent(jLabel6)
.addGap(90, 90, 90))))
.addGroup(jPanel1Layout.createSequentialGroup()
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
.addComponent(jLabel5, javax.swing.GroupLayout.PREFERRED_SIZE, 564, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGroup(javax.swing.GroupLayout.Alignment.LEADING, jPanel1Layout.createSequentialGroup()
.addGap(54, 54, 54)
.addComponent(jLabel1)
.addGap(18, 18, 18)
.addComponent(jLabel2)
.addGap(10, 10, 10)
.addComponent(jLabel3))
.addGroup(jPanel1Layout.createSequentialGroup()
.addComponent(jLabel7)
.addGap(197, 197, 197))
.addGroup(javax.swing.GroupLayout.Alignment.LEADING, jPanel1Layout.createSequentialGroup()
.addGap(184, 184, 184)
.addComponent(jLabel8, javax.swing.GroupLayout.PREFERRED_SIZE, 122, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(cname)
.addGroup(jPanel1Layout.createSequentialGroup()
.addComponent(signin_btn, javax.swing.GroupLayout.PREFERRED_SIZE, 113, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(18, 18, 18)
.addComponent(signup_btn, javax.swing.GroupLayout.PREFERRED_SIZE, 113, javax.swing.GroupLayout.PREFERRED_SIZE)))))
.addContainerGap(69, Short.MAX_VALUE))
);
jPanel1Layout.setVerticalGroup(
jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel1Layout.createSequentialGroup()
.addGap(18, 18, 18)
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 83, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(jLabel2)
.addComponent(jLabel3, javax.swing.GroupLayout.PREFERRED_SIZE, 83, javax.swing.GroupLayout.PREFERRED_SIZE))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addComponent(jLabel4)
.addGap(1, 1, 1)
.addComponent(jLabel5, javax.swing.GroupLayout.PREFERRED_SIZE, 22, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(1, 1, 1)
.addComponent(jLabel6)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jLabel7)
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel1Layout.createSequentialGroup()
.addGap(49, 49, 49)
.addComponent(cname)
.addGap(170, 170, 170))
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jLabel8, javax.swing.GroupLayout.PREFERRED_SIZE, 40, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(150, 150, 150)))
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(signin_btn, javax.swing.GroupLayout.PREFERRED_SIZE, 38, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(signup_btn, javax.swing.GroupLayout.PREFERRED_SIZE, 38, javax.swing.GroupLayout.PREFERRED_SIZE))
.addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
);
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jPanel1, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
);
pack();
}// </editor-fold>
private void signin_btnActionPerformed(java.awt.event.ActionEvent evt) {
new Sign_in().setVisible(true);
}
private void signup_btnActionPerformed(java.awt.event.ActionEvent evt) {
new Sign_up().setVisible(true);
}
private void jPanel1InputMethodTextChanged(java.awt.event.InputMethodEvent evt) {
}
public static void main(String args[]) {
Welcome_screen courses = new Welcome_screen();
courses.getCourses();
/* Create and display the form */
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new Welcome_screen().setVisible(true);
}
});
}
public void getCourses(){
db obj = new db();
obj.dbconnect();
String Query = "select * from courses";
try{
Connection con = obj.dbconnect();
Statement st = con.createStatement();
ResultSet rs = st.executeQuery(Query);
while(rs.next()){
String value = rs.getString("name");
jLabel8.setText(value); //this is the part that I set sql result to the JLabel
}
}catch(Exception e){
System.out.println(e);
}
}
// Variables declaration - do not modify
private javax.swing.JLabel cname;
private javax.swing.JLabel jLabel1;
private javax.swing.JLabel jLabel2;
private javax.swing.JLabel jLabel3;
private javax.swing.JLabel jLabel4;
private javax.swing.JLabel jLabel5;
private javax.swing.JLabel jLabel6;
private javax.swing.JLabel jLabel7;
private javax.swing.JLabel jLabel8;
private javax.swing.JPanel jPanel1;
private javax.swing.JButton signin_btn;
private javax.swing.JButton signup_btn;
// End of variables declaration
}