Mysql java 变量_在mysql查询中传递java字符串变量

如何在sql查询中传递

java字符串变量.我已经完成了所有的JDBC连接.

我的sql数据库查询是

sql = "Select *

from production AS cust

INNER JOIN location AS comp

ON cust.location_id = comp.location_id

where comp.name = locationnames AND crop_id =1";

它不起作用.但是,如果我执行以下代码,它的工作

sql = "Select *

from production AS cust

INNER JOIN location AS comp

ON cust.location_id = comp.location_id

where comp.name = "\taplejung"\

AND crop_id =1";

现在告诉我如何将变量名称传递给sql查询来执行此操作. Jst告诉我如何将变量locationnames传递给comp.name.

我的完整java函数如下所示:locationCombo表示在combobox中选择的项目. CropCombo也表示相同……

public void displayYearwise() throws SQLException, ClassNotFoundException{

//jComboBox4.setSelectedItem("Crops");

//DefaultCategoryDataset dataset = new DefaultCategoryDataset();

XYSeriesCollection dataset = new XYSeriesCollection();

XYSeries series = new XYSeries("production");

XYSeries series1 = new XYSeries("scat");

String JDBC_DRIVER="com.mysql.jdbc.Driver";

String DB_URL="jdbc:mysql://localhost/data2";

Connection conn;

Statement stmt;

String USER = "root";

String PASS = "";

Object cropname = CropCombo.getSelectedItem();

String cropnames = cropname.toString();

Object locationname = locationCombo.getSelectedItem();

// String locationnames = locationname.toString();

String locationnames = "taplejung";

String pd="paddy ";

System.out.println(cropnames.length()+" "+pd.length());

System.out.println(cropsList);

String sql=null;

if(cropnames.equals("paddy"))

{

//System.out.println();

sql="Select *

from production AS cust

INNER JOIN location AS comp

ON cust.location_id = comp.location_id

WHERE comp.name = "+locationnames+"

AND crop_id =1";

}

else{

sql="SELECT *

FROM `production`

WHERE crop_id = 4

AND location_id = 10";

}

try{

Class.forName(JDBC_DRIVER);

conn=DriverManager.getConnection(DB_URL,USER,PASS);

System.out.println("Creating statement...");

stmt = conn.createStatement();

System.out.println(sql);

ResultSet rs=stmt.executeQuery(sql);

while (rs.next()){

//String student = rs.getString("studentname");

String yeartext = rs.getString("year_of_production");

//double value = Double.parseDouble(text);

String productiontext = rs.getString("production_amount");

Double yield = rs.getDouble("yield_amount");

double production = Double.parseDouble(productiontext);

double year = Double.parseDouble(yeartext);

series.add(year,production) ;

series1.add(year,yield) ;

//dataset.addSeries(series);

}

dataset.addSeries(series);

dataset.addSeries(series1);

chartArea.removeAll();

JFreeChart chart = ChartFactory.createScatterPlot("Scatter Plot","Year","Paddy Production", dataset);

// JFreeChart chart = ChartFactory.createScatterPlot("Scatter Plot","Year","Paddy Production", dataset, PlotOrientation.HORIZONTAL, rootPaneCheckingEnabled, rootPaneCheckingEnabled, rootPaneCheckingEnabled);

// CategoryPlot p = chart.getCategoryPlot();

//XYPlot xyplot = (XYPlot)jfreechart.getPlot();

//https://stackoverflow.com/questions/12417732/jfreechart-with-scroller

ChartPanel chartPanel = new ChartPanel(chart, false);

chartArea.setLayout(new BorderLayout());

chartArea.add(chartPanel, BorderLayout.EAST);

chartArea.add(chartPanel);

SwingUtilities.updateComponentTreeUI(this);

// p.setRangeGridlinePaint(blue);

chartArea.updateUI();

System.out.println("Database created successfully...");

}

catch(SQLException se)

{

//Handle errors for JDBC

System.out.println("Connect failed ! ");

se.printStackTrace();

// JOptionPane.showMessageDialog(MajorUI.this, err.getMessage());

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值