import java.sql.Connection; import java.sql.DriverManager; import java.sql.Statement; import java.util.Scanner; public class input { public static void input() { System.out.println("データの入力をしてください。"); System.out.println("id"); int user_id = new Scanner(System.in).nextInt(); System.out.println("name"); String user_name = new java.util.Scanner(System.in).nextLine(); String msg = ""; try { // ドライバロード Class.forName("com.mysql.jdbc.Driver"); // MySQL に接続 Connection con = DriverManager.getConnection("jdbc:mysql://192.168.10.8:3306/testdb", "test11", ""); // ステートメント生成 Statement stmt = con.createStatement(); //SQL を実行 //String insert = "insert into test_table(user_id,user_name) values (" + user_id + "," + user_name + ")"; String insert = "insert into test_table(user_id,user_name) values (" + user_id + "," + user_name + ")"; int num = stmt.executeUpdate(insert); // 接続を閉じる stmt.close(); con.close(); }catch (ClassNotFoundException e){ msg = "ドライバのロードに失敗しました"; System.out.println(msg); }catch (Exception e){ msg = "ドライバのロードに失敗しました"; System.out.println(msg); } output a =new output(); a.output(); } }
このコードを入力すると
こうエラーが出ます。
データの入力をしてください。 id 1 name test ドライバのロードに失敗しました
どうコードを変更すればよいでしょうか
変更コード
import java.sql.Connection; import java.sql.DriverManager; import java.sql.Statement; public class input { private static String sqlStr1; public static void input() { String msg = ""; try { // ドライバロード Class.forName("com.mysql.jdbc.Driver"); System.out.println("データの入力をしてください。"); System.out.println("id"); int user_id =100; //= new Scanner(System.in).nextLine(); System.out.println("name"); int user_name =1; //= new Scanner(System.in).nextLine(); // MySQL に接続 Connection con = DriverManager.getConnection("jdbc:mysql://192.168.10.8:3306/testdb", "test11", ""); // ステートメント生成 Statement stmt = con.createStatement(); //SQL を実行 String insert = "insert into test_table(user_id,user_name) values (" + user_id + "," + user_name + ")"; //String sqlStr = "insert into test_table(user_id,user_name) values(2,'satou');"; int num = stmt.executeUpdate(insert); output a =new output(); a.output(); // 接続を閉じる stmt.close(); con.close(); }catch (Exception e){ msg = "ドライバのロードに失敗しました"; System.out.println(msg); } } }
int user_name =1;
を
String user_name = "1";
にするとエラーが出る。
int num = stmt.executeUpdate(insert);
ここが問題あるのではないかと思うがここを
String num = stmt.executeUpdate(insert);
にすとエラーが出る。
ヒントをもらえると嬉しいです。
また、再回答のほどよろしくお願いします。
import java.sql.Connection; import java.sql.DriverManager; import java.sql.Statement; public class input { private static String sqlStr1; public static void input() { String msg = ""; try { // ドライバロード Class.forName("com.mysql.jdbc.Driver"); System.out.println("データの入力をしてください。"); System.out.println("id"); int user_id =100; //= new Scanner(System.in).nextLine(); System.out.println("name"); String user_name ="test"; //= new Scanner(System.in).nextLine(); // MySQL に接続 Connection con = DriverManager.getConnection("jdbc:mysql://192.168.10.8:3306/testdb", "test11", ""); // ステートメント生成 Statement stmt = con.createStatement(); //SQL を実行 String insert = "insert into test_table(user_id,user_name) values (" + user_id + "," + user_name + ")"; //String sqlStr = "insert into test_table(user_id,user_name) values(2,'satou');"; int num = stmt.executeUpdate(insert); output a =new output(); a.output(); // 接続を閉じる stmt.close(); con.close(); }catch (Exception e){ e.printStackTrace() ; } } }
さらにコードを変更しました。
com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: Unknown column 'test' in 'field list' at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method) at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62) at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45) at java.lang.reflect.Constructor.newInstance(Constructor.java:422) at com.mysql.jdbc.Util.handleNewInstance(Util.java:425) at com.mysql.jdbc.Util.getInstance(Util.java:408) at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:943) at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3973) at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3909) at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:2527) at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:2680) at com.mysql.jdbc.ConnectionImpl.execSQL(ConnectionImpl.java:2483) at com.mysql.jdbc.StatementImpl.executeUpdateInternal(StatementImpl.java:1552) at com.mysql.jdbc.StatementImpl.executeLargeUpdate(StatementImpl.java:2607) at com.mysql.jdbc.StatementImpl.executeUpdate(StatementImpl.java:1480) at input.input(input.java:57)
このようなエラーが出力されました。
int num = stmt.executeUpdate(insert);
やはりこのコードではないかと思っています。
回答よろしくお願いいたします。
回答3件
あなたの回答
tips
プレビュー