简体   繁体   English

将Netbeans(JAVA)连接到MYSQL

[英]Connecting netbeans (JAVA) to MYSQL

The problem is when I run this code, I always get answer COL12 instead of getting all the columns with data like I am getting in MYSQL workbench. 问题是,当我运行此代码时,我总是得到答案COL12,而不是像在MYSQL工作台中那样获得所有包含数据的列。 I need to get same values. 我需要获得相同的值。 Moreover when I use this query "DELETE FROM test WHERE col1 = 2;" 而且,当我使用此查询“ DELETE FROM test WHERE col1 = 2;”时 in netbeans I get Error Exception. 在netbeans中,我收到错误异常。

Here is my Code I wrote in Netbeans. 这是我在Netbeans中编写的代码。
package sams; 包装袋

import java.sql.*;  
import java.sql.Statement;
import java.sql.DriverManager;




public class Temp_Class {



public static void main(String[] args){

    try {

String query="SELECT * FROM test";
        Class.forName("com.mysql.jdbc.Driver");
        Connection con=      DriverManager.getConnection("jdbc:mysql://localhost/JavaProject", "root",  "19881990");
        Statement st=con.createStatement();
        ResultSet rs=st.executeQuery(query);
        rs.next();
        String sname= rs.getString(2);
 System.out.println(sname);
con.close();
    } catch (Exception e) {

            System.out.println("Error");

      }


    }



}

I have made table test in MYSQL Workbench, with columns="col1, col2, col3" with values="1, COL11, COL12" and "2, COL21, COL22"; 我已经在MYSQL Workbench中进行了表测试,其中column =“ col1,col2,col3”的值=“ 1,COL11,COL12”和“ 2,COL21,COL22”;

This is the code I use to delete a value in DB. 这是我用来删除数据库中值的代码。 . . but getting Error Exception. 但出现错误异常。

public static void main(String[] args){

    try {


        String deletesql="DELETE FROM test WHERE col1 = 2";

        Class.forName("com.mysql.jdbc.Driver");
        Connection con= DriverManager.getConnection("jdbc:mysql://localhost/JavaProject", "root", "19881990");
        Statement st=con.createStatement();
        ResultSet rs=st.executeQuery(select);

        Statement ds=con.createStatement();
        ResultSet dsrow=ds.executeQuery(deletesql);

        dsrow.next();
        String ds1= dsrow.getString(1);
        String ds2= dsrow.getString(2);
        String ds3= dsrow.getString(3);
        System.out.println(ds1+" "+ ds2+" "+ds3);

        con.close();
    } catch (Exception e) {

        System.out.println("Error");

    }


}

The reason you are getting one column is because you are fetching only one column 获得一列的原因是因为您仅获取一列

rs.next();
String sname= rs.getString(2);   //will give you one column

instead do 代替做

while(rs.next){                     //get all rows

 String col1= rs.getString(0);      //get column 1 of respective row
 String col2= rs.getString(1);      //get column 2 of respective row

 System.out.println(col1+" "+col2);

}

String sname= rs.getString(2); returns one column from the resultSet. 从resultSet返回一列。 If you want to have all columns you have to use: 如果要具有所有列,则必须使用:

String sname0= rs.getString(0);
String sname1= rs.getString(1);
String sname2= rs.getString(2);

I think the problem is from rs.getString(2) 我认为问题出在rs.getString(2)
Here you have just tried to get the third column. 在这里,您刚刚尝试获取第三列。 You can use rs.getString(0) and rs.getString(1) to get other values. 您可以使用rs.getString(0)和rs.getString(1)来获取其他值。

test table 测试

col1 col2 col3 col1 col2 col3

col1 1 2 col1 1 2
col2 col11 col21 col2 col11 col21
col3 col12 col22 col3 col12 col22

you are queries for col1 (ie SELECT * FROM test) 您是对col1的查询(即SELECT * FROM test)

String sname= rs.getString(2); 字符串sname = rs.getString(2);
System.out.println(sname); 的System.out.println(SNAME);

rs.getString(2) ie we are passing column index ie second column col2 and it prints the first value in column 2 ie 1 rs.getString(2),即我们正在传递列索引,即第二列col2,并在列2中打印第一个值,即1
here 1 should be printed in reality 这里1应该是现实印刷

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM