package com.jia.jdbc;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.util.Scanner;
public class Demo01 {
public static void main(String[] args) {
try {
Class.forName("com.mysql.jdbc.Driver");
Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/dvdsys","root","1");
if(conn!=null) {
System.out.println("java连接mysql成功!");
}else {
System.out.println("java连接mysql失败!");
}
Scanner sc=new Scanner(System.in);
System.out.println("请输入dvd的名字");
String name=sc.next();
System.out.println("请输入dvd的价格:");
double price=sc.nextDouble();
System.out.println("请输入dvd的出版社:");
String publish=sc.next();
PreparedStatement ps = conn.prepareStatement("insert into dvd2 values(null,?,?,?,0,'','',0)");
ps.setString(1, name);
ps.setDouble(2, price);
ps.setString(3, publish);
int row = ps.executeUpdate();
if(row==1) {
System.out.println("新增成功!");
}else {
System.out.println("新增失败!");
}
ps.close();
conn.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
转载请注明原文地址:https://ipadbbs.8miu.com/read-2651.html