你能帮忙解决这个问题吗?
我正在尝试创建然后使用名为TIGER的数据库.
如果我在MySQL中创建数据库并且运行完美,我没有问题.
我想做的是从Java创建它.因此,当代码第一次运行时,它会在初始启动时创建数据库.如果可能的话,我想用一个干净的方法把它装箱.
有人可以告诉我你实际定位代码的位置
这是代码
private String jdbcDriver = "com.mysql.jdbc.Driver";
private String dbAddress = "jdbc:mysql://localhost:3306/";
private String dbName = "TIGER";
private String userName = "root";
private String password = "";
private PreparedStatement statement;
private ResultSet result;
private Connection con;
public DbStuff() {
try {
Class.forName(jdbcDriver);
con = DriverManager.getConnection(dbAddress + dbName, userName, password);
}
catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
Run Code Online (Sandbox Code Playgroud)
这是创建数据库的代码
con = …Run Code Online (Sandbox Code Playgroud) 首先要感谢那些以前帮助过我的人。
我目前遇到的问题是这行代码
statement.executeUpdate(myTableName);
Run Code Online (Sandbox Code Playgroud)
或使用这些代码行
String myTableName = "CREATE TABLE AgentDetail ("
+ "idNo INT(64) NOT NULL AUTO_INCREMENT,"
+ "initials VARCHAR(2),"
+ "agentDate DATE,"
+ "agentCount INT(64))";
Run Code Online (Sandbox Code Playgroud)
当代码达到这些点时,它将生成一个错误,该错误将被SQLException块捕获。
它非常简单或非常复杂
任何人都可以指出Java MySQL编程的新手在哪里出错了,希望不会出错,在此先感谢
这是完整的其余代码
public class DbStuff {
private String jdbcDriver = "com.mysql.jdbc.Driver";
private String dbAddress = "jdbc:mysql://localhost:3306/";
private String userPass = "?user=root&password=";
private String dbName = "TIGER19";
private String userName = "root";
private String password = "";
private PreparedStatement preStatement;
private Statement statement;
private ResultSet result;
private Connection con;
public DbStuff() { …Run Code Online (Sandbox Code Playgroud) 感谢迄今为止我在用MySQL学习Java的史诗般的战斗中获得的所有支持.我想要做的是检查表是否存在.目前发生的是,如果创建了数据库并且也创建了表.但是如果我运行相同的代码,我会得到一个表已经存在的错误.
最大的问题是我如何检查表存在?这是我工作过的一些代码
if (tabCrea.createTable(dbDef, con, preStatement, tabStruct.getAgentDetail(), "Agent Details"))
System.out.println("Passed Here");
else
System.out.println("Failed Here");
Run Code Online (Sandbox Code Playgroud)
其中称之为以下内容
protected boolean createTable(DataBaseDefaults dbDef, Connection con, Statement statement, String myTableName, String tableName) {
try {
Class.forName(dbDef.getJdbcDriver());
con = DriverManager.getConnection(dbDef.getDbAddress() + dbDef.getPortAddress() + dbDef.getDbName(),
dbDef.getDbUserName(), dbDef.getDbPassword());
statement = con.createStatement();
statement.executeUpdate(myTableName);
System.out.println("Table Sucessfully Created : " + tableName);
return true;
}
catch (SQLException e ) {
//Problem is caught here;
System.out.println("An error has occured on Table Creation with Table " + tableName);
return false;
}
catch (ClassNotFoundException …Run Code Online (Sandbox Code Playgroud) 我遇到过这个小问题.
String fileAdress = "c:\red\";
System.out.println("Peach " + fileAdress);
fileAdress = fileAdress.replaceAll("\", "\\\\");
System.out.println("Steel " + fileAdress);
Run Code Online (Sandbox Code Playgroud)
要么
String fileAdress = "c:\\red\\";
System.out.println("Peach " + fileAdress);
fileAdress = fileAdress.replaceAll("\\", "\\\\");
System.out.println("Steel " + fileAdress);
Run Code Online (Sandbox Code Playgroud)
我想将fileAddress转换为以下内容
String fileAdress = "c:\\\\red\\\\";
Run Code Online (Sandbox Code Playgroud)
是否有可能建议我在哪里犯错误以及如何解决?