📄 testartist.java
字号:
package sql99;
import java.sql.*;
import connections.ConnectionFactory;
import java.util.Map;
public class TestArtist {
public static void main(String[] args) {
Connection connection = null;
PreparedStatement ps = null;
ResultSet resultSet = null;
try {
connection = ConnectionFactory.getConnection();
Map map = connection.getTypeMap();
map.put("ARTIST", Artist.class);
String sql = "INSERT INTO artists (id, performer) VALUES (?, ?)";
ps = connection.prepareStatement(sql);
Artist artist = new Artist("ARTIST", "John Hiatt", "link to picture",
"Singer-songwriter");
ps.setInt(1, 1);
ps.setObject(2, artist);
int result = ps.executeUpdate();
if (result != 1) {
System.out.println("Insert failed!");
return;
}
artist = new Artist("ARTIST", "", "", "");
System.out.println("Created new artist object, name is [" +
artist.name + "]");
sql = "SELECT * FROM artists WHERE id=1";
ps = connection.prepareStatement(sql);
resultSet = ps.executeQuery();
if (resultSet.next()) {
artist = (Artist) resultSet.getObject(2);
System.out.println("Retrieved artist, name = " + artist.name);
} else {
System.out.println("Error retrieving artist");
}
} catch (Exception e) {
e.printStackTrace();
} finally {
ConnectionFactory.close(resultSet);
ConnectionFactory.close(ps);
ConnectionFactory.close(connection);
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -