0
ثبت داده در جدول پایگاه داده در جاوا
*چگونه می توان اطلاعات را در زبان جاوا در جدول پایگاه داده ثبت نمود لطفا با مثال کد آن توضیح دهید ، من از پایگاه داده MYSQL استفاده می نمایم با تشکر از راهنمایی شما
1 پاسخ
0
سلام به شما Itpro ای عزیز
کد زیر نحوه insert کردن داده ها را نشان می دهد.
import java.sql.*; public class JavaMysqlPreparedStatementInsertExample { public static void main(String[] args) { try { // create a mysql database connection String myDriver = "org.gjt.mm.mysql.Driver"; String myUrl = "jdbc:mysql://localhost/test"; Class.forName(myDriver); Connection conn = DriverManager.getConnection(myUrl, "root", ""); // the mysql insert statement String query = " insert into users (first_name, last_name, is_admin, num_points)" + " values (?, ?, ?, ?, ?)"; // create the mysql insert preparedstatement PreparedStatement preparedStmt = conn.prepareStatement(query); preparedStmt.setString (1, "Barney"); preparedStmt.setString (2, "Rubble"); preparedStmt.setBoolean(4, false); preparedStmt.setInt (5, 5000); // execute the preparedstatement preparedStmt.execute(); conn.close(); } catch (Exception e) { System.err.println("Got an exception!"); System.err.println(e.getMessage()); } } }