JAVA中,怎么提取数据库里的数据放到JCombobox中去?
这个是连接数据库Class.forName("com.mysql.jdbc.Driver");Connectionconn=DriverManager.getConnec...
这个是连接数据库
Class.forName("com.mysql.jdbc.Driver");
Connection conn=DriverManager.getConnection("jdbc:mysql://localhost:3306/mydangan","root","123456");
Statement st=conn.createStatement();
ResultSet rs=st.executeQuery("SELECT*from dangan");
下拉列表的名是 combobox 要怎么写? 展开
Class.forName("com.mysql.jdbc.Driver");
Connection conn=DriverManager.getConnection("jdbc:mysql://localhost:3306/mydangan","root","123456");
Statement st=conn.createStatement();
ResultSet rs=st.executeQuery("SELECT*from dangan");
下拉列表的名是 combobox 要怎么写? 展开
2个回答
展开全部
比如:
Student bean=new Student();
StudentDAO sdao=new StudentDAO();
List<Student> slist=(List<Student>)sdao.selectStudent(bean);//查询学生的方法
if(slist.size()>0)
{
for(int i=0;i<slist.size();i++)
{
Student student=(Student)slist.get(i);
c1.addItem(student.getKemu());
c2.addItem(student.getNianji());
c3.addItem(student.getBanji());
c4.addItem(sutdent.getFenshu());
}
}
//
就行了!
展开全部
import java.awt.FlowLayout;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Vector;
import javax.swing.JComboBox;
import javax.swing.JFrame;
public class Test extends JFrame {
private static final longserialVersionUID= -6628351148857075490L;
public Test() {
Vector<String> items = new Vector<String>();
try {
Class.forName("com.mysql.jdbc.Driver");
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
Connection conn = null;
Statement st = null;
ResultSet rs = null;
try {
conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/test", "root", "");
st = conn.createStatement();
rs = st.executeQuery("SELECT * from user limit 5");
while (rs.next()) {
items.add(rs.getString("username"));
}
} catch (SQLException e) {
e.printStackTrace();
} finally {
try {
rs.close();
st.close();
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
JComboBox comboBox = new JComboBox(items);
this.setLayout(new FlowLayout());
this.add(comboBox);
}
public static void main(String[] args) {
Test t = new Test();
t.setSize(200, 200);
t.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
t.setVisible(true);
}
}
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Vector;
import javax.swing.JComboBox;
import javax.swing.JFrame;
public class Test extends JFrame {
private static final longserialVersionUID= -6628351148857075490L;
public Test() {
Vector<String> items = new Vector<String>();
try {
Class.forName("com.mysql.jdbc.Driver");
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
Connection conn = null;
Statement st = null;
ResultSet rs = null;
try {
conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/test", "root", "");
st = conn.createStatement();
rs = st.executeQuery("SELECT * from user limit 5");
while (rs.next()) {
items.add(rs.getString("username"));
}
} catch (SQLException e) {
e.printStackTrace();
} finally {
try {
rs.close();
st.close();
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
JComboBox comboBox = new JComboBox(items);
this.setLayout(new FlowLayout());
this.add(comboBox);
}
public static void main(String[] args) {
Test t = new Test();
t.setSize(200, 200);
t.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
t.setVisible(true);
}
}
更多追问追答
追问
我是新手,不要写太复杂了
追答
这个是最简单的,你稍微修改就能运行
如果这些还看不懂就不要做swing,还是做点简单的吧。
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询
广告 您可能关注的内容 |