问题如下package yinshua;
import javax.swing.*;
import java.sql.*;
import java.sql.Connection;
import java.util.Calendar;
import java.lang.String;import java.awt.event.*;import java.awt.*;
public class Yl_ruku extends Sqlconn implements ActionListener{
private JFrame f;
private JLabel l1,l2,l3,l4,l5,l6,l7,l8,l9;
private JTextField t1,t2,t3,t4,t5,t6,t7,t8,t9,t10,t11,t12,t13,t14;
private JComboBox c1,c2,c3,c4,c5,c6,c7,c8,c9;
private JButton b1,b2,b3,b4,b5,b6;
private JPanel p1,p2,p3,p4,p5,p6,p7;
public static void main(String args[]) throws SQLException{
Yl_ruku ruku=new Yl_ruku();//定义对象 ruku.go();
}
public void go() throws SQLException{
Sqlconn b=new Sqlconn();
f=new JFrame("原料入库管理");
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setLayout(new GridLayout(4,1));
f.setSize(700, 500);
f.validate();
Statement sql=dbConn.createStatement();
String name,name1,name2,name3,name4,name5;
String ci1[] =new String[100];
String ci2[] =new String[100];
String ci3[] =new String[100];
String ci4[] =new String[100];
String ci5[] =new String[100];
String ci6[] =new String[100];
ResultSet rs = sql.executeQuery("SELECT yl_name FROM yl_shuxing");
int i=0;
while(rs.next()){
name=rs.getString(1);
ci1[i]=name;
i++;
}
上边这是部分代码,Eclipse内提示错误语句为:Statement sql=dbConn.createStatement();
错误提示为Exception in thread "main" java.lang.Error: Unresolved compilation problems:
The method createStatement() is undefined for the type String
Sqlconn是数据库连接的代码,没错误。
我想问的是,我怎么错了,我该怎么修改啊,谢谢!!
import javax.swing.*;
import java.sql.*;
import java.sql.Connection;
import java.util.Calendar;
import java.lang.String;import java.awt.event.*;import java.awt.*;
public class Yl_ruku extends Sqlconn implements ActionListener{
private JFrame f;
private JLabel l1,l2,l3,l4,l5,l6,l7,l8,l9;
private JTextField t1,t2,t3,t4,t5,t6,t7,t8,t9,t10,t11,t12,t13,t14;
private JComboBox c1,c2,c3,c4,c5,c6,c7,c8,c9;
private JButton b1,b2,b3,b4,b5,b6;
private JPanel p1,p2,p3,p4,p5,p6,p7;
public static void main(String args[]) throws SQLException{
Yl_ruku ruku=new Yl_ruku();//定义对象 ruku.go();
}
public void go() throws SQLException{
Sqlconn b=new Sqlconn();
f=new JFrame("原料入库管理");
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setLayout(new GridLayout(4,1));
f.setSize(700, 500);
f.validate();
Statement sql=dbConn.createStatement();
String name,name1,name2,name3,name4,name5;
String ci1[] =new String[100];
String ci2[] =new String[100];
String ci3[] =new String[100];
String ci4[] =new String[100];
String ci5[] =new String[100];
String ci6[] =new String[100];
ResultSet rs = sql.executeQuery("SELECT yl_name FROM yl_shuxing");
int i=0;
while(rs.next()){
name=rs.getString(1);
ci1[i]=name;
i++;
}
上边这是部分代码,Eclipse内提示错误语句为:Statement sql=dbConn.createStatement();
错误提示为Exception in thread "main" java.lang.Error: Unresolved compilation problems:
The method createStatement() is undefined for the type String
Sqlconn是数据库连接的代码,没错误。
我想问的是,我怎么错了,我该怎么修改啊,谢谢!!
你的dbConn是String?
你在用String.createStatement(),当然不行
应该用Connection的createStatement(sql)这样
先去获取个Connection的实例吧
具体去看jdbc吧,这个蛮基础的
String ci3[] =new String[100];
String ci4[] =new String[100];
String ci5[] =new String[100];
String ci6[] =new String[100]; ?
p2=new JPanel();
p3=new JPanel(new GridLayout(4,6));
p4=new JPanel();
l1=new JLabel("原料入库管理");//p1面板的内容
l2=new JLabel("序号");//p2面板的内容
t1=new JTextField(10);
l3=new JLabel("入库时间");
t2=new JTextField(10);
//p3 面板的内容
l4=new JLabel("入库编号");//第一行的控件
l5=new JLabel("原料名称");
l6=new JLabel("原料规格");
l7=new JLabel("原料数量");
l8=new JLabel("原料单价");
l9=new JLabel("原料总金额");
t3=new JTextField("10011");//第二行的控件
c1=new JComboBox(ci1);
c1.setEditable(true);
Statement sql1=dbConn.createStatement();
ResultSet rs1 = sql1.executeQuery("SELECT yl_guige FROM yl_shuxing where yl_name='"+c1.getSelectedItem().toString().trim()+"'" );
int i1=0;
while(rs1.next()){
name1=rs1.getString(1);
ci2[i1]=name1;
i1++;
}
c2=new JComboBox(ci2);
c2.setEditable(true);
p5=new JPanel(new GridLayout(1,2));
t10=new JTextField("0");
c3=new JComboBox();
c3.setEditable(true);
t4=new JTextField("0",10);
t5=new JTextField();
t6=new JTextField();//第三行的控件
Statement sql2=dbConn.createStatement();
ResultSet rs2 = sql2.executeQuery("SELECT yl_name FROM yl_shuxing ");
int i2=0;
while(rs2.next()){
name2=rs2.getString(1);
ci3[i2]=name2;
i2++;
}
c4=new JComboBox(ci3);
c4.setEditable(true);
Statement sql3=dbConn.createStatement();
ResultSet rs3 = sql3.executeQuery("SELECT yl_guige FROM yl_shuxing where yl_name='"+c1.getSelectedItem().toString().trim()+"'" );
int i3=0;
while(rs3.next()){
name3=rs3.getString(1);
ci4[i3]=name3;
i3++;
}
c5=new JComboBox(ci4);
c5.setEditable(true);
p6=new JPanel(new GridLayout(1,2));
t11=new JTextField("0");
c8=new JComboBox();
c8.setEditable(true);
t7=new JTextField("0");
t8=new JTextField();
t9=new JTextField();//第四行的控件
Statement sql4=dbConn.createStatement();
ResultSet rs4 = sql4.executeQuery("SELECT yl_name FROM yl_shuxing ");
int i4=0;
while(rs4.next()){
name4=rs4.getString(1);
ci5[i4]=name4;
i4++;
}
c6=new JComboBox(ci5);
c6.setEditable(true);
Statement sql5=dbConn.createStatement();
ResultSet rs5 = sql5.executeQuery("SELECT yl_guige FROM yl_shuxing where yl_name='"+c1.getSelectedItem().toString().trim()+"'" );
int i5=0;
while(rs5.next()){
name5=rs5.getString(1);
ci6[i5]=name5;
i5++;
}
这是后边的代码。
数据库连接代码是:
import java.sql.Connection;
import java.sql.DriverManager;
public class Sqlconn{
public static final String dbConn = null;
public Sqlconn(){
String driverName = "com.microsoft.sqlserver.jdbc.SQLServerDriver"; //加载JDBC驱动
String dbURL = "jdbc:sqlserver://192.168.1.234:1433; DatabaseName=test1"; //连接服务器和数据库sample
String userName = "sa"; //默认用户名
String userPwd = "1234"; //密码
Connection dbConn=null; try {
Class.forName(driverName).newInstance();
dbConn = DriverManager.getConnection(dbURL, userName, userPwd);
//如果连接成功 控制台输出Connection Successful!
} catch (Exception e) {
e.printStackTrace();
}
}
}
你是不是应该先获得一个Collection的对象如conn,然后在
conn.createStatement();
dbConn = b.getConn();
Statement sql=dbConn.createStatement(); public Connection getConn(){
return dbConn;
}
public static final String dbConn = null;
public Sqlconn(){
String driverName = "com.microsoft.sqlserver.jdbc.SQLServerDriver"; //加载JDBC驱动
String dbURL = "jdbc:sqlserver://192.168.1.234:1433; DatabaseName=test1"; //连接服务器和数据库sample
String userName = "sa"; //默认用户名
String userPwd = "1234"; //密码
Connection dbConn=null; try {
Class.forName(driverName).newInstance();
dbConn = DriverManager.getConnection(dbURL, userName, userPwd);
//如果连接成功 控制台输出Connection Successful!
} catch (Exception e) {
e.printStackTrace();
}
}你开头定义了一个static dbConn, 构造函数就不要定义了. 可以Sqlconn.dbConn调用.
Statement sql=Sqlconn.dbConn.createStatement();