增刪改查sql語言
阿新 • • 發佈:2021-10-17
Add.java
package jdbc;
import java.util.Scanner;
import java.sql.*;
public class Add {
public static void main(String[] args) throws Exception {
// Class.forName("com.mysql.jdbc.Driver");//載入驅動
Scanner sc = new Scanner(System.in);
String jdbc="jdbc:mysql://127.0.0.1:3306/mydb?characterEncoding=GBK";
Connection conn =DriverManager.getConnection("jdbc:mysql://localhost:3306/world", "root", "1767737316.");//連結到資料庫
Statement state=conn.createStatement();
String stunumber;
String stuname;
int stuscore;
System.out.println("請輸入學號");
stunumber = sc.nextLine();
System.out.println( "姓名");
stuname = sc.nextLine();
System.out.println("成績");
stuscore = sc.nextInt();
String sql = "insert into 博哥戰神(number,name,score) values(?,?,?)"; //SQL語句
PreparedStatement presta = conn.prepareStatement(sql);
// 設定sql語句中的values值
presta.setString(1,stunumber);
presta.setString( 2,stuname);
presta.setInt(3,stuscore);
int count = presta.executeUpdate(sql); System.out.println(count);
conn.close();//關閉通道
}
}